为什么要池无状态会话bean? [英] Why pool Stateless session beans?

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

问题描述

Java中的无状态bean不会在客户端的两个调用之间保持状态。所以简单来说,我们可以认为它们是商业方法的对象。每个方法都需要参数和返回结果。当调用该方法时,在执行堆栈中正在创建一些局部变量。当方法返回时,本地人将从堆栈中移除,如果某些临时对象被分配,则无论如何都是垃圾回收。



从我的角度来看,与调用方法没有区别的单独实例由单独的线程。那么为什么一个容器不能使用一个bean的一个实例而不是汇集一个bean呢?

解决方案

池化有几件事情。 / p>

一,通过每个实例拥有一个bean,您将确保线程安全(Servlet,例如,不是线程安全的)。



二,减少一个bean可能具有的任何潜在的启动时间。虽然会话Bean是无状态的,但它们只需要对客户端是无状态的。例如,在EJB中,可以将多个服务器资源注入到Session Bean中。该状态对于bean是私有的,但没有理由不能将其从调用保留到调用。因此,通过汇总bean,您可以将这些查找减少到仅在创建bean时才会发生。



三,您可以使用bean池作为遏制流量的一种手段。如果你只有一个泳池里有10个豆,那么你最多只能收到10个同时工作的请求,其余的将排队等待。


Stateless beans in Java do not keep their state between two calls from the client. So in a nutshell we might consider them as objects with business methods. Each method takes parameters and return results. When the method is invoked some local variables are being created in execution stack. When the method returns the locals are removed from the stack and if some temporary objects were allocated they are garbage collected anyway.

From my perspective that doesn’t differ from calling method of the same single instance by separate threads. So why cannot a container use one instance of a bean instead of pooling a number of them?

解决方案

Pooling does several things.

One, by having one bean per instance, you're guaranteed to be threads safe (Servlets, for example, are not thread safe).

Two, you reduce any potential startup time that a bean might have. While Session Beans are "stateless", they only need to be stateless with regards to the client. For example, in EJB, you can inject several server resources in to a Session Bean. That state is private to the bean, but there's no reason you can't keep it from invocation to invocation. So, by pooling beans you reduce these lookups to only happening when the bean is created.

Three, you can use bean pool as a means to throttle traffic. If you only have 10 Beans in a pool, you're only going to get at most 10 requests working simultaneously, the rest will be queued up.

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

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