关闭应用程序服务器时优雅地停止@schedule EJB
我有一个无状态的EJB,其方法应定期执行。到目前为止,这效果很好。但是,在关闭应用程序服务器时,我希望该方法在运行时优雅地停止该方法。因此,我已经实现了用@predestroy
注释的stop()
方法。
但是,stop()
方法无法正常工作。在运行schedule()
时停止我的应用程序服务器时,应用程序服务器等待schedule()
终止并调用stop> stop()
之后。
@Stateless
public class TestService {
private static final Logger logger = Logger.getLogger(TestService.class);
private volatile boolean shutdown = false;
@Schedule(hour = "*", minute = "*", second = "*/15", persistent = false)
public void schedule() throws InterruptedException {
logger.info("Start execution");
for (int i = 0; !shutdown && i < 10; ++i) {
logger.info("Step " + i);
Thread.sleep(1000);
}
logger.info("Stopped execution");
}
@PreDestroy
public void stop() {
logger.info("Trigger stop");
shutdown = false;
}
}
在这里,我只发布了一个最小的示例EJB来复制我的问题。在我真正的EJB中,情况更糟。在触发服务器关闭后执行任何事务时,应用程序服务器(JBOSS EAP 7.1带有Wildfly 11)会抛出componentionSstoppedException
。因此,我没有机会优雅地关闭我的EJB。
I have a stateless EJB with a method that should executed periodically. This works well so far. However, I want the stop the method gracefully when it is running while shutting down application server. Therefore, I have implemented a stop()
method annotated with @PreDestroy
.
However, the stop()
method does not work as expected. When stopping my application server while schedule()
is running, the application server waits until schedule()
terminates and calls stop()
only afterwards.
@Stateless
public class TestService {
private static final Logger logger = Logger.getLogger(TestService.class);
private volatile boolean shutdown = false;
@Schedule(hour = "*", minute = "*", second = "*/15", persistent = false)
public void schedule() throws InterruptedException {
logger.info("Start execution");
for (int i = 0; !shutdown && i < 10; ++i) {
logger.info("Step " + i);
Thread.sleep(1000);
}
logger.info("Stopped execution");
}
@PreDestroy
public void stop() {
logger.info("Trigger stop");
shutdown = false;
}
}
Here, I have posted only a minimal example EJB for reproducing my issue. In my real EJB, it is even worse. When executing any transaction after triggering the server shutdown, the application server (JBoss EAP 7.1 with WildFly 11) throws a ComponentIsStoppedException
. So, I have no chance to gracefully shutdown my EJB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另一种解决方案可能是使用EJB
TimerService
?这样的事情:
An alternative solution could be to use the EJB
TimerService
?Something like this: