简体   繁体   English

Java bean中的线程

[英]Thread in java bean

I have a session bean in JSP, and in that bean I am starting a thread that sleeps for 10 sec and executes again. 我在JSP中有一个会话bean,在该bean中,我正在启动一个睡眠10秒并再次执行的线程。 The problem I am facing is, after logging out also this thread continues to run. 我面临的问题是,注销后此线程也继续运行。

Can someone please help how to stop that from running? 有人可以帮忙如何阻止它运行吗?

According to the EJB specification , starting threads from EJBs is illegal. 根据EJB规范 ,从EJB启动线程是非法的。 Cf. cf. section 21.2.2 (page 593): 第21.2.2节(第593页):

"The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt to start, stop, suspend, or resume a thread, or to change a thread's priority or name. The enterprise bean must not attempt to manage thread groups." “企业Bean不得尝试管理线程。企业Bean不得尝试启动,停止,挂起或恢复线程,或更改线程的优先级或名称。企业Bean不得尝试管理线程组。”

There may be a different solution using EJB or you may need to use a different framework. 使用EJB可能有不同的解决方案,或者您可能需要使用不同的框架。 This depends on what exactly you are trying to accomplish. 这取决于您要完成的工作。

Starting threads out of servlets is not a good idea because you may ran out of resources (memory and CPU) if you have many users. 从servlet中启动线程不是一个好主意,因为如果您有很多用户,则可能会耗尽资源(内存和CPU)。 Especially with database access strange behaviour will occur! 尤其是使用数据库访问时,会发生奇怪的行为!

Add a Quartz timer (http://quartz-scheduler.org) that triggers every 10secs and processes a list where your session beans have beed subscribed. 添加一个Quartz计时器(http://quartz-scheduler.org),该计时器每10秒触发一次并处理一个列表,其中已订阅了您的会话bean。 On user logout unsubscribe your bean from your service again. 在用户注销时,再次取消订阅您的bean。

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

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