java - 为什么需要无状态 EJB 池

标签 java ejb server pool

我知道:

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.

我的问题是:为什么需要池? 难道 EJB 无状态 bean 的一个实例就足以满足所有请求吗?

此外,如果给定无状态 bean 的服务器正在使用 10 个实例的池, 它可以处理这样一个 bean 的最大请求数是 10 吗?

你能帮我解开疑惑吗?

编辑:

section 4.3.14 of the ejb 3.1 spec gives the answer.

4.3.14 Serializing Session Bean Methods The following requirements apply to Stateless and Stateful session beans. See Section 4.8.5 for Singleton session bean concurrency requirements.

The container serializes calls to each stateful and stateless session bean instance. Most containers will support many instances of a session bean executing concurrently; however, each instance sees only a serialized sequence of method calls. Therefore, a stateful or stateless session bean does not have to be coded as reentrant.

The container must serialize all the container-invoked callbacks (that is, the business method interceptor methods, lifecycle callback interceptor methods, timeout callback methods, beforeCompletion, and so on), and it must serialize these callbacks with the client-invoked business method calls.

网上查了一下,我的疑问是线程池是必须的 到强加 EJB 无状态方法的规范 是线程安全的。 所以如果我们有,比如池中有 10 个 bean,则只能同时处理 10 个请求,其他请求将排队并分配给第一个空闲 bean。 如果我错了,请纠正我。

最佳答案

如果你创建一个无状态 session bean,它不关心哪个客户端正在调用,它允许你在多个客户端之间重用实例,这样你的应用程序就会有更好的性能,它是无状态 session Bean 和有状态 session Bean 之间的主要区别。

有状态 session bean 将为每个客户端创建一个实例,这将降低应用程序的性能,因为您将同时拥有许多实例。

要有一个池,您可以根据应用程序的负载增加无状态 ejb 实例的数量:)

编辑

如果您只需要一个实例来处理所有请求并且这就是您所需要的,您可以使用单例 session bean 而不是无状态 session bean 无状态 session bean 是为这个不需要状态的操作而创建的,这个操作将帮助你提高性能。

如果您有一个包含 10 个实例的池,您可以接收任意数量的请求,但只有 10 个实例会处理它们。

关于java - 为什么需要无状态 EJB 池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31012816/

相关文章:

php - 如何将 pdf/word/文本文件发送到服务器

java - Genymotion 初始化引擎 : failed in IntelliJ

java - Android处理程序缩短延迟?

java - Firefox/Chrome 和 Java GZIPOutputStream 之间的 GZIP/Deflate 错误

java - Stateless ejb 不删除池

Swift 使用 CloudKit 保存新评论

C:使用Select监听两个socket

java - 检查 FileInputStream 是否关闭的最佳方法是什么?

jpa - JBoss 部署问题 - 无法部署此 Jar

java - Glassfish v3 客户端。什么是 gf-client.jar?