Java 启动时集成tomcat和Quartz调度器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2975704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integration of tomcat and Quartz scheduler on startup
提问by Shamik
I am using tomcat version 6.0. My requirement is that when tomcat starts up, I would like to start a QuartzScheduler
which will schedule some jobs at a regular interval. I am trying to figure out the best possible way to do it. Here are the options that I could think of -
我正在使用 tomcat 6.0 版。我的要求是,当 tomcat 启动时,我想启动一个QuartzScheduler
,它会定期安排一些作业。我试图找出最好的方法来做到这一点。以下是我能想到的选项 -
- I can do this via a servlet with "
load-on-startup
" descriptor inweb.xml
file to start the scheduler and schedule the jobs inside the servlet. - Can be done using a
ContextListener
(this sounds a better approach to me than 1). This might be a clean approach to start the scheduler inside thecontextInitialized
method and shutdown the scheduler insidecontextDestroyed
method. - Using a MBean-descriptor. I develop a MBean which will get started when the server starts up with mbeans-descriptor.xml file.
- 我可以通过文件中带有“
load-on-startup
”描述符的 servletweb.xml
来启动调度程序并调度 servlet 内的作业。 - 可以使用 a 来完成
ContextListener
(这对我来说听起来比 1 更好)。这可能是一种在contextInitialized
方法内启动调度程序并在方法内关闭调度程序的干净contextDestroyed
方法。 - 使用 MBean 描述符。我开发了一个 MBean,它将在服务器启动时使用 mbeans-descriptor.xml 文件启动。
To me, it looks like second approach is better. Third one might not be a good idea as it is clearly not a MBean to be monitored by jconsole or so. My purpose is to start the scheduler and stop it when tomcat stops. Is there any better and cleaner way to do this ?
对我来说,看起来第二种方法更好。第三个可能不是一个好主意,因为它显然不是由 jconsole 左右监视的 MBean。我的目的是启动调度程序并在 tomcat 停止时停止它。有没有更好更干净的方法来做到这一点?
采纳答案by Greg Case
I would recommend the second approach as well, using a Servlet that exists only to start up some service, while a common usage, seems hacky to me.
我也推荐第二种方法,使用一个只存在于启动某些服务的 Servlet,虽然这是一个常见的用法,但对我来说似乎很棘手。
It appears that quartz already provides a ServletContextListener for this exact purpose:
看来石英已经为此确切目的提供了一个 ServletContextListener :
http://quartz-scheduler.org/documentation/quartz-2.x/cookbook/ServletInitScheduler
http://quartz-scheduler.org/documentation/quartz-2.x/cookbook/ServletInitScheduler
and
和
http://www.quartz-scheduler.org/api/2.0.0/
http://www.quartz-scheduler.org/api/2.0.0/
for details.
详情。