什么时候使用有状态会话 bean 而不是无状态会话 bean? [英] When to use Stateful session bean over Stateless session bean?

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

问题描述

有状态会话 bean 定义如下:

A stateful session bean is defined as follows:

Stateful Session Beans 对象的状态由值组成其实例变量.在有状态会话 bean 中,实例变量表示唯一客户端-bean 会话的状态.因为客户端与其 bean 交互(对话"),这种状态通常是称为会话状态.

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.

无状态会话 bean 定义如下:

A stateless session bean is defined as follows:

无状态会话 Bean 无状态会话 Bean 不维护与客户端的会话状态.当客户端调用无状态 bean 的方法,bean 的实例变量可能包含特定于该客户端的状态,但仅限于持续时间调用.方法完成后,客户端特定状态不应保留.但是,客户端可以更改状态池化无状态 bean 中的实例变量,并保持此状态转到下一次调用池化无状态 bean.除了在方法调用期间,无状态 bean 的所有实例都是等效,允许 EJB 容器将实例分配给任何客户.也就是说,无状态会话 bean 的状态应该适用覆盖所有客户.

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.

与有状态会话 bean 相比,使用无状态会话 bean 的优点如下:

The advantage of using a stateless session bean over stateful session bean is as follows:

因为无状态会话 bean 可以支持多个客户端,所以它们可以为需要大量数据的应用程序提供更好的可扩展性的客户.通常,应用程序需要较少的无状态会话bean 比 stateful session bean 支持的数量相同客户.

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.

所以我想到的问题是什么时候应该使用有状态会话 bean?就我对此事的天真理解而言,应该尽可能坚持使用无状态会话 bean.

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 a stateless session bean as he can.

在哪些情况下应该使用有状态会话 bean?有什么好的例子吗?

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

会话 Bean

推荐答案

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

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

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

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.

另一方面,有状态会话 bean 与客户端紧密相连.每个实例都被创建并绑定到单个客户端,并且仅服务于来自该特定客户端的请求.碰巧的是,如果您对一个有状态的 bean 执行两个后续请求,您的请求将始终从 bean 的同一个实例中得到服务.这意味着您可以在请求之间保持对话状态.在生命周期结束时,客户端调用 remove 方法,bean 将被销毁/准备好进行垃圾回收.

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 lifecycle, the client calls a remove method and the bean is being destroyed/ready for garbage collection.

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

When to use stateless or stateful?

这主要看你是否想保持对话状态.例如,如果您有一个将两个数字相加并返回结果的方法,您将使用无状态 bean,因为它是一次性操作.如果您用其他数字第二次调用此方法,您就不再对前一次加法的结果感兴趣.

That mainly depends on whether you want to maintain the conversational state. For example, if you have a method that adds up two numbers and return the result you use a stateless bean because it's 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.

但是,例如,如果您想计算客户端已完成的请求数,则必须使用有状态的 bean.在这种情况下,了解客户端之前请求 bean 方法的频率很重要,因此您必须在 bean 中维护会话状态(例如,使用变量).如果您在这里使用无状态 bean,客户端的请求每次都会从不同的 bean 中得到服务,这会弄乱您的结果.

But if you want, for example, to 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 a 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, which messes up your results.

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

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