|
查看: 1819|回复: 4
|
Java 里 Stateless vs Stateful, Remote vs Local, Persistence 的意思
[复制链接]
|
|
|
根据我读的资料, Stateless Bean 在技术上的解释是不同的JVM 上运行的Java程序是无法access 彼此的。而Stateful Bean 是可以的。
Remote 和 Local 则是会分别 这个bean 可不可以被其他的机器上Java 程序呼叫或access。
我还不是明白得非常透彻, 恳请Java 高手可以能以 Web based database app 为例子, 加少许的coding 比较, 来解释这些term 。
谢谢。 |
|
|
|
|
|
|
|
|
|
|
发表于 15-4-2008 04:12 PM
|
显示全部楼层
原帖由 jangancari 于 15-4-2008 11:32 AM 发表 
根据我读的资料, Stateless Bean 在技术上的解释是不同的JVM 上运行的Java程序是无法access 彼此的。而Stateful Bean 是可以的。
Remote 和 Local 则是会分别 这个bean 可不可以被其他的机器上Java 程序呼叫或ac ...
Stateless session beans do not maintain the conversational state associated with any client.
意思是说”Stateless session beans“ 做的东西,没有对应任何client.
例:
读取某product的price.
比如有5个client同时create "Stateless session beans", 去读取某product的price.
那return 回来的price直接(胡乱 ps*)丢给任何个client.
因为每个price是同样 value的。
ps*:
--其实不能酱”胡乱“,它是会用最后一个值,看以下例子:
Less s[] = new Less[5];
for(int i = 0 ; i < 5 ; i ++ ){
s = home.create();
s.setCount(i);
}
for(int i = 0 ; i < 5 ; i ++){
System.out.println(s.getCount());
}
那打印出来的是:
5
6
7
8
9
原因:当他setCount时,最后已经set去“4”了,所以print出来是从“5”开始。
Stateful session beans maintain the state associated with a client.
意思是说”Stateful session beans“ 做的东西,有对应任何client.
例:
购物车
比如有5个client同时create "Stateful session beans", 大家同时有了一辆购物车
那每个client所买的东西是不同的,
所以每辆购物车是有固定的client.
[ 本帖最后由 winmxaa 于 15-4-2008 09:01 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 15-4-2008 06:07 PM
|
显示全部楼层
回复 2# winmxaa 的帖子
谢谢你清楚的解释,明白了Stateful 和 Stateless Session Bean 的分别与功能了。
我还有一个疑问, 如果一个Stateful Session Bean declare 了 Stateless Session Bean 的 instance (object) , 那么那个object 是Stateful 还是stateless ? vice-versa 呢?
以下例子里 LibraryCart 是 Stateful , BookDaoImp 是 Stateless 。
例子:- @Stateless
- public class BookDaoImp implements BookDao {
- @PersistenceContext
- private EntityManager em;
- private Logger log = Logger.getLogger(this.getClass());
- public void save(Book book) {
- log.debug("Persist book: " + book);
- em.persist(book);
- }
- public void merge(Book book) {
- em.merge(book);
- }
- public List findAll() {
- log.debug("find All books");
- return em.createQuery("from Book").getResultList();
- }
- public static final String RemoteJNDIName = BookDaoImp.class
- .getSimpleName()
- + "/remote";
- public static final String LocalJNDIName = BookDaoImp.class.getSimpleName()
- + "/local";
- public Book findById(Integer id) {
- return em.find(Book.class, id);
- }
- public List findByCustomer(Customer customer) {
- log.debug("find by customer");
- return em.createQuery("from Book b where b.customer = :customer")
- .setParameter("customer", customer).getResultList();
- }
- }
- = = = = =
- @Stateful
- public class LibraryCart implements LibraryCartLocal {
- @PersistenceContext
- private EntityManager em;
- private Logger log = Logger.getLogger(this.getClass());
- private Map<Integer, Book> books = new HashMap<Integer, Book>();
- public static final String RemoteJNDIName = LibraryCart.class
- .getSimpleName()+ "/remote";
- public static final String LocalJNDIName = LibraryCart.class
- .getSimpleName()+ "/local";
- public void addBook(Book book) {
- if (books.get(book.getId()) == null)
- books.put(book.getId(), book);
- }
- public void addBook(Integer id) {
- if (books.get(id) == null)
- {
- BookDao bookDao;
- try
- {
- Context context = new InitialContext();
- bookDao = (BookDao) context.lookup(BookDaoImp.LocalJNDIName);
- } catch (NamingException e)
- {
- e.printStackTrace();
- throw new RuntimeException(e);
- }
- Book book = bookDao.findById(id);
- if (book != null)
- books.put(id, book);
- }
- }
- 略。。
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 15-4-2008 11:32 PM
|
显示全部楼层
BookDao还是Stateless Stateless Bean再Context的Stateless Pool里面只有一个Instance |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 18-4-2008 07:37 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|