何时在无状态会话bean上使用状态会话bean? [英] When to use Stateful session bean over Stateless session bean?

查看:169
本文介绍了何时在无状态会话bean上使用状态会话bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有状态会话bean定义如下:


状态会话Bean对象的状态由
的值组成其实例变量。在有状态会话bean中,实例
变量表示唯一的客户端Bean会话的状态。因为
客户端与它的bean进行交互(谈话),这种状态通常是
,称为会话状态。




无状态会话bean定义如下:


无状态会话Bean无状态会话bean不维护
会话状态与客户。当客户端调用无状态bean的
方法时,bean的实例变量可能包含
一个特定于该客户端的状态,但仅在
调用期间。方法完成后,客户端特定状态
不应该被保留。然而,客户端可能会更改池无状态bean中
实例变量的状态,并且此状态保持
到下一次调用池状态无状态bean。除方法调用之外的
外,无状态bean的所有实例都为
等价,允许EJB容器将任何实例分配给任何
客户端。也就是说,无状态会话bean的状态应该在所有客户端上应用


还提到了无状态会话bean如下:


由于无状态会话bean可以支持多个客户端,因此可以为
为需要的应用程序提供更好的可扩展性大量的
的客户。通常,一个应用程序比有状态会话bean需要更少的无状态会话
bean来支持相同数量的
客户端。


所以想到的问题是什么时候应该使用有状态会话bean?对于我天真的理解,应该尽可能地使用无状态会话bean。



应该使用有状态会话bean的候选人是什么?任何好的例子?



会话Bean

解决方案

首先,您必须了解如何在服务器上创建和处理bean。



对于无状态会话bean ,服务器可以在池中维护可变量的实例。每当客户端请求这样的无状态bean(例如通过方法)请求随机实例来提供该请求时。这意味着如果客户端执行两个后续请求,那么无状态bean的两个不同实例可能会提供请求。实际上,两个请求之间没有对话状态。此外,如果客户端消失,则无状态bean不会被破坏,并可以从另一个客户端提供下一个请求。



另一方面,有状态会话bean 与客户密切相关。每个实例都被创建并绑定到单个客户端,并且只提供来自该特定客户端的请求。所以如果在有状态的bean上执行两个后续请求,那么您的请求将始终来自同一bean实例。这意味着您可以在请求之间保持会话状态。在生命周期结束时,客户端调用一个remove方法,该bean正在被销毁/准备好进行垃圾回收。


何时使用无状态或有状态?


这主要取决于您是否要保持对话状态。例如,如果您有一个方法添加到数字并返回结果,则使用无状态bean,因为它是一次性操作。如果您再次使用其他数字调用此方法,则不再对以前添加的结果感兴趣。



但是,如果您想要计算请求数量一个客户端已经完成了,你必须使用一个有状态的bean。在这种情况下,重要的是要知道客户端以前请求多少次bean,所以你必须保持bean中的会话状态(例如使用变量)。如果你在这里使用一个无状态的bean,客户端的请求每次都会从另外一个bean那里提供给你的结果。


Stateful session bean is defined as follow:

Stateful Session Beans The state of an object consists of the values of its instance variables. In a stateful session bean, the instance variables represent the state of a unique client-bean session. Because the client interacts ("talks") with its bean, this state is often called the conversational state.

Stateless session bean is defined as follow:

Stateless Session Beans A stateless session bean does not maintain a conversational state with the client. When a client invokes the methods of a stateless bean, the bean’s instance variables may contain a state specific to that client, but only for the duration of the invocation. When the method is finished, the client-specific state should not be retained. Clients may, however, change the state of instance variables in pooled stateless beans, and this state is held over to the next invocation of the pooled stateless bean. Except during method invocation, all instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. That is, the state of a stateless session bean should apply accross all clients.

It is also mentioned the advantage of stateless session bean as follow:

Because stateless session beans can support multiple clients, they can offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.

So the question that comes to mind is when one should use stateful session beans? To my naive understanding of the matter, one should stick to use stateless session bean as he can.

What would be the candidates in which one should use stateful session bean? Any good examples?

Session Bean

解决方案

First you have to understand how the beans are created and handled on the server.

For stateless session beans the server can maintain a variable amount of instances in a pool. Each time a client requests such a stateless bean (e.g. through a method) a random instance is chosen to serve that request. That means if the client does two subsequent requests it is possible that two different instances of the stateless bean serve the requests. In fact there is no conversational state between the two requests. Also if the client disappears, the stateless bean does not get destroyed and can serve the next request from another client.

On the other hand a stateful session bean is closely connected to the client. Each instance is created and bounded to a single client and serves only requests from that particular client. So happens that if you do two subsequent requests on a stateful bean, your request will be served always from the same instance of the bean. That means you can maintain a conversational state between the requests. At the end of the lifecyle the client calls a remove method and the bean is being destroyed/ready for garbage collection.

When to use stateless or stateful?

That mainly depends on whether you want to maintain the conversational state. For example if you have a method that adds up to numbers and return the result you use a stateless bean because its a one time operation. If you call this method a second time with other numbers you are not interested in the result of the previous addition anymore.

But if you want for example count the number of requests a client has done, you have to use a stateful bean. In this scenario it is important to know how often the client has requested the bean method before, so you have to maintain conversational state in the bean (e.g. with a variable). If you would use a stateless bean here the request of the client would be served each time from a different bean what messes up your results.

这篇关于何时在无状态会话bean上使用状态会话bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆