简体   繁体   English

当客户回来时,有状态会话bean如何重新获得状态?

[英]How does a stateful session bean regain state when client comes back?

If the stateful session bean is going to passivate, its state is written to harddisk and then the bean instance will be freed to serve other request (at least this is my understanding). 如果有状态会话bean要钝化,则将其状态写入硬盘,然后释放bean实例以提供其他请求(至少这是我的理解)。 When the same client is active again, bean instance will read the state from hard disk to regain the state. 当同一个客户端再次处于活动状态时,bean实例将从硬盘读取状态以重新获得状态。 But how the bean instance knows that for which client which file it has to read to maintain the state? 但是bean实例如何知道哪个客户端必须读取哪个文件来维护状态呢?

I am very new to J2EE, so please pardon me if I am asking a very naive doubt. 我是J2EE的新手,所以如果我问一个非常天真的怀疑,请原谅我。 If I need to know any other topic to understand this, please point me in the right direction. 如果我需要了解任何其他主题来理解这一点,请指出我正确的方向。

It's best to visualize a Stateful Session Bean (SfSB) as very close to an instance of a normal Java class. 最好将状态会话Bean(SfSB)可视化为非常接近普通Java类的实例。 You look up (or inject) an instance of a SfSB, and the container will create one for you and return the instance. 您查找(或注入)SfSB的实例,容器将为您创建一个并返回实例。 You then work with that instance like you would any other Java instance. 然后,您可以像处理任何其他Java实例一样使用该实例。

That means that you can store the instance in to a Session, serialize it to disk, etc. 这意味着您可以将实例存储到会话中,将其序列化为磁盘等。

The detail is that the instance you are working with is actually a proxy to the actual, underlying SfSB instance. 细节是您正在使用的实例实际上是实际的底层SfSB实例的代理。 It's not the actual SfSB itself. 这不是真正的SfSB本身。

When you make a call on your local proxy to the bean, it is the containers job to manifest that bean in to memory for you. 当您在bean的本地代理上进行调用时,将容器作业显示给内存中的bean。 The passivation and activation of the bean is done behind the scenes for you (though you can tap in to the process through the beans lifecycle). bean的钝化和激活是在幕后为您完成的(尽管您可以通过bean生命周期进入该过程)。

Any information that the container needs to find the passivated SfSB is stored in the proxy that you're working with, but this is opaque to you. 容器需要找到钝化的SfSB的任何信息都存储在您正在使用的代理中,但这对您来说是不透明的。 You needn't worry about it. 你不用担心它。

So, in a typical web based scenario, the life cycle would be that you get your bean instance, store it in a web session, and then simply use it like normal. 因此,在典型的基于Web的场景中,生命周期将是您获取bean实例,将其存储在Web会话中,然后像平常一样简单地使用它。 If the container decides it needs to passivate your bean to make room or whatever, it will passivate it automatically for you. 如果容器决定它需要钝化你的bean以腾出空间或其他什么,它会自动为你钝化它。 When your user returns, your app pulls the instance from the web session, and makes its calls. 当您的用户返回时,您的应用程序会从Web会话中提取实例并进行调用。 At that time, if the bean is passivated, the container will activate the bean for you, again automatically. 那时,如果bean被钝化,容器将再次自动为你激活bean。 This entire mechanism is dependent on the container, yet transparent to you. 整个机制取决于容器,但对您来说是透明的。 The important thing for you to recall is that you must hang on to SfSB that you get from the container, like you would any java object. 重要的是要记住,你必须坚持从容器中获取的SfSB,就像你对任何java对象一样。

The final caveat is that if you allow a SfSB to be passivated for too long, the container will automatically delete it for you. 最后需要注意的是,如果您允许SfSB钝化太长时间,容器会自动为您删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM