ScheduledExecutorService Java EE 如何在 Web 服务(JBoss)中启动它? [英] ScheduledExecutorService Java EE how to start it in web service (JBoss)?

查看:26
本文介绍了ScheduledExecutorService Java EE 如何在 Web 服务(JBoss)中启动它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java SE 中,当我运行 main() 时,我有一个计划任务等待执行.

但在 Java EE (JBoss webservice) 中,这些方法会在有 Web 请求时运行.

有没有办法在应用程序部署后开始调度任务?

我想使用

具体来说,将调用两种方法中的任何一种.部署 Web 应用程序时调用一种方法,关闭 Web 应用程序时调用另一种方法.使用一个来启动你的 ScheduledExecutorService.使用另一个来终止您的 ScheduledExecutorService.终止非常重要,因为您的执行程序的线程池可能会在您的 Web 应用程序停止后很长时间甚至在您的 Web 容器关闭后继续运行.

当你编写实现这个接口的类时,你可以使用不同的方法来部署它.最简单的方法是使用

ServletContextListener 架构是对 Servlet 的明智补充,对于更严肃的 Web 应用程序至关重要.该设计通常不错,但缺少一项重要功能:如果在设置过程中出现问题,您的侦听器将无法停止 Web 应用程序的部署.如果您的数据库已关闭,或者无法访问所需的 Web 服务,或者其他重要资源不可用,您可能不希望您的 Web 应用程序启动.Servlet 规范未能满足这一需求.一种解决方法是以某种方式在您的侦听器和 servlet 之间进行通信,也许在 上下文属性" 并在您的 servlet 中检查该标志.

<小时>

我就ServletContextListener<的这个主题进行了演讲/code> 在 Devoxx 摩洛哥 2017.我的一些幻灯片如上所示,整个幻灯片是以 PDF 格式提供.

In Java SE I have a scheduled tasks waiting to be executed when I run main().

But in Java EE (JBoss webservice) the methods are run when there's a web request.

Is there a way to start scheduling tasks once the application is deployed?

I was looking to use ScheduledExecutorService.

解决方案

Caveat: This Answer addresses the app servers such as Apache Tomcat and Eclipse Jetty that do not implement the Jakarta Concurrency specification. That spec defines "managed" executor services and the annotation @Schedule. Users of "Full Platform" Jakarta such as JBoss/WildFly, Glassfish/Payara, and so on (list) should use that facility for a much simpler and easier approach to scheduling tasks.


ServletContextListener

A Servlet runs only when a Request arrives from a user. Set-up tasks such as your scheduler must be done before that first Request is handled.

That is the purpose of the ServletContextListener interface. The term "Servlet context" simply means your web app. The "listener" part means a class implementing this interface will be called automatically.

Specifically, either of two methods will be called. One method is called when your web app is deployed, another when your web app is being shutdown. Use one to launch your ScheduledExecutorService. Use the other to terminate your ScheduledExecutorService. Very important to terminate, as your executor’s thread pool may continue running, long after your web app has stopped, and even after your web container closed.

When you write your class implementing this interface, you can use different avenues for its deployment. The easiest avenue is to mark the class with the @WebListener annotation. Your web container will automatically detect such a marked class and load it. After the Web container has finished initializing your web app, but before the first incoming Request is handled, your ServletContextListener will be invoked. This behavior is guaranteed by the Servlet spec version 2.3 and later. So it works across all Web containers: Tomcat, Jetty, TomEE, Glassfish, JBoss, WildFly, etc.

The ServletContextListener architecture was a wise addition to the Servlet, crucial for more serious web apps. The design is generally good but lacks one important feature: If something fails during your setup, there is no way for your listener to halt the deployment of your web app. If your database is down, or needed web services unreachable, or other important resources unavailable, you may not want your Web app to commence. The Servlet spec fails to address this need. A workaround is to somehow communicate between your listener and your servlet, perhaps set some flag in the context "attributes" and check for that flag within your servlet.


I gave a talk on this subject of ServletContextListener at Devoxx Morocco 2017. Some of my slides are shown above, the entire slide deck is available as a PDF.

这篇关于ScheduledExecutorService Java EE 如何在 Web 服务(JBoss)中启动它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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