关闭应用程序服务器时优雅地停止@schedule EJB

发布于 2025-02-05 08:21:31 字数 1164 浏览 2 评论 0原文

我有一个无状态的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裸钻 2025-02-12 08:21:31

另一种解决方案可能是使用EJB TimerService

这样的事情:

@Startup
public class TestService {

    @Resource
    private TimerService ejbTimerService;

    private Timer timer = null;

    @PostConstruct
    public void setup() {
        ScheduleExpression schedule = new ScheduleExpression().hour("*").minute("*").second("15");
        timer = ejbTimerService.createCalendarTimer(schedule);
        System.out.println("Timer " + timer.toString() + " created");
    }

    @Timeout
    public void onTimeout(Timer timer) {
        System.out.println("Timer schedule: " + timer.getSchedule());
    }

    @PreDestroy
    public void tearDown() {
        logger.info("Trigger stop");        
    }

    // Method to call when stopping application server
    public void smoothShutdown() {
       if (timer!=null) timer.cancel();
    }
}

An alternative solution could be to use the EJB TimerService ?

Something like this:

@Startup
public class TestService {

    @Resource
    private TimerService ejbTimerService;

    private Timer timer = null;

    @PostConstruct
    public void setup() {
        ScheduleExpression schedule = new ScheduleExpression().hour("*").minute("*").second("15");
        timer = ejbTimerService.createCalendarTimer(schedule);
        System.out.println("Timer " + timer.toString() + " created");
    }

    @Timeout
    public void onTimeout(Timer timer) {
        System.out.println("Timer schedule: " + timer.getSchedule());
    }

    @PreDestroy
    public void tearDown() {
        logger.info("Trigger stop");        
    }

    // Method to call when stopping application server
    public void smoothShutdown() {
       if (timer!=null) timer.cancel();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文