我需要在每10分钟安排一份工作,这项工作在简单的Web应用程序中进行,我们不能使用Spring。我浏览了一些教程,但无法理解我需要做什么,请告诉我需要遵循的步骤?
我有一些特别的怀疑:
quartz.properties
吗?quartz.xml
吗?我不能使用一些类/ servlet做同样的事情吗?我有一个执行作业的独立程序,我可以在任何servlet的init中编写它,并在容器<load-on-startup>1</load-on-startup>
的启动时启动该servlet。这是正确的事吗?
答案 0 :(得分:4)
以下是每10分钟重复一次作业的案例代码示例:
公共类HelloJob实现Job {
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
//put your code here
}
}
由于您不想使用xml并且想要java代码,您可以从ServletContext获取StdSchedulerFactory实例来配置调度程序,并且为了在初始化时调用该代码,您必须将其放在侦听器中:
public class HelloQuartzListener implements ServletContextListener {
private Scheduler scheduler;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent ctx) {
// define the job and tie it to our HelloJob class
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("myJob", "group1").build();
// Trigger the job to run now, and then every 10 minutes
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMinutes(10).repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
try {
scheduler = ((StdSchedulerFactory) ctx.getServletContext()
.getAttribute(
QuartzInitializerListener.QUARTZ_FACTORY_KEY))
.getScheduler();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
}
}
}
要从您的Web应用程序初始化quartz,您需要通过将以下内容添加到web.xml来配置QuartzInitializerListener。请注意,最后我们还添加了我们之前创建的自己的侦听器,它通过java代码配置作业。重要的是它在QuartzInitializerListener之后,因为需要首先调用QuartzInitializerListener,以便将StdSchedulerFactory放入上下文中以便HelloJobListener得到它:
<context-param>
<param-name>quartz:shutdown-on-unload</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:wait-on-shutdown</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:start-on-load</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
</listener>
<listener>
<listener-class>yourpackage.HelloQuartzListener</listener-class>
</listener>
******如果您确实想要设置自己的属性值,可以添加属性文件并将其添加到web.xml中来包含其路径:
<context-param>
<param-name>quartz:config-file</param-name>
<param-value>/WEB-INF/quartz.properties</param-value>
</context-param>
****** ..如果您决定使用xml,则可以通过添加以下内容在属性文件中指定:
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz.xml
其中“quartz.xml”将包含作业详细信息(当然在这种情况下删除HelloJobListener配置和类):
<job-scheduling-data
xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData
http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">
<schedule>
<job>
<name>HelloJob</name>
<group>group1</group>
<description>hello</description>
<job-class>yourpackage.HelloJob</job-class>
</job>
<trigger>
<cron>
<name>myTrigger</name>
<job-name>HelloJob</job-name>
<job-group>group1</job-group>
<!-- It will run every 10 minutes -->
<cron-expression>0 0/10 * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>