如何将 Quartz 与 QuartzInitializerListener 一起使用?
我无法理解如何将 Quartz 与 QuartzInitializerListener 一起使用。
首先,我在部署描述符中声明该侦听器。那么,我该如何添加我的职位呢?看一下 QuartzInitializerListener 实现,我看到它创建了 SchedulerFactory
和 Scheduler
,但我没有看到任何添加作业的方法。工厂收到一个 配置文件,但同样没有与作业相关的内容那里。
我从搜索中只找到了非常简单的示例,所有这些都是关于在 main 方法中实例化所有内容。
谁能给我举一个更真实的例子吗?如果重要的话我正在使用 JBoss 5。谢谢。
I'm having trouble to understand how to use Quartz with QuartzInitializerListener
.
First I declare that listener in deployment descriptor. But then, how to I add my jobs? Taking a look at QuartzInitializerListener implementation, I see it creates the SchedulerFactory
and Scheduler
, but I don't see any way to add jobs. The factory receives a configuration file, but again there's nothing related to the jobs there.
I only found very simples examples from my searches, all about instantiating everything in main method.
Can anyone point me to a more real example? I'm using JBoss 5 if that matters. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您好,这是您查询的答案:
1)第 1 步:编写作业:
2)编写 web.xml:
如您所见,有两个侦听器。一个属于 Quartz API,另一个属于您的 API。第一个 Quartz API 监听器将按顺序首先执行。此时我们就已经有了现成的调度器工厂。如果相应的属性“quartz:start-on-load”未指定或指定为 true,则此侦听器也将启动调度程序。
3) 编写你的 Quartz 监听器:
该监听器将在 Quartz 监听器执行后执行。这意味着我们已经准备好了 Scheduler Factory 和一个已启动的调度程序。所以你只需要将作业添加到调度程序即可。正如您所看到的,contextDestroyed 方法是空的,因为调度程序关闭工作将由 Quartz API schedluer 执行。
Hi Here is the answer to your query:
1) Step 1: Write Job:
2) Write web.xml:
As you can see there are two listeners. One belongs to Quartz API and other to your API. First Quartz API listener will execute as it comes first in order. At this moment we will have ready made scheduler factory. This listener will start scheduler also if corresponding property "quartz:start-on-load" is either not specified or specified as true.
3) Write your Quartz Listener :
This listener will execute after Quartz listener has executed. This means we have ready made Scheduler Factory with us and a started scheduler. So you only need to add job to scheduler. As you can see the contextDestroyed method is empty as scheduler shutdown work will be carried out by Quartz API schedluer.
一切都在 源代码您引用的Javadoc:
编辑:这意味着当您使用此侦听器时,您可以在每个 servlet/Spring MVC 控制器/...内获取
SchedulerFactory
通过运行:请注意,上下文侦听器保证在使用任何 servlet 处理传入请求之前执行。这意味着调度程序在使用之前始终会被正确初始化。
下面评论中的讨论摘要,讨论实际上回答了所提出的问题:
如果你想在应用程序启动时添加作业,请编写另一个侦听器(类似于 Quartz 提供的侦听器),查找 StdSchedulerFactory (ServletContext 很容易获得)并执行你想要的操作。监听器保证按照 web.xml 中声明的顺序执行,因此将监听器放在 Quartz 后面。
Everything is described in the source code Javadoc you quote:
EDIT: This means that when you are using this listener you can obtain
SchedulerFactory
inside every servlet/Spring MVC controller/... by running:Note that context listeners are guaranteed to be executed before any servlet is used to handle incoming requests. This means that the scheduler will always be properly initialized prior to its usage.
Summary of the discussion in the comments below, the discussion actually answers the question being asked:
If you want to add jobs at application startup, write another listener (similar to the one provided by Quartz), lookup StdSchedulerFactory (ServletContext is easily available) and do what you want. The listeners are guaranteed to execute in the same order as they are declared in web.xml, so put your listener after Quartz one.