快速入门:用xml启动Quartz任务
1.下载Quartz
将quartz放到 yourappl/WEB-INF/lib 里面。
2.配置web.xml,启动Quartz服务.
在 yourappl/WEB-INF/web.xml 里添加以下内容。
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<display-name>Quartz Initializer Servlet</display-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
3.配置您的任务文件 quartz_reminder.xml ,建立job 和 trigger.
在 yourappl/WEB-INF/web.xml 里新建文件 quartz_reminder.xml
以下例子建立两个日程安排,一个是每隔15分钟执行scheduling.QuartzEmail任务,
一个是星期1-5 8:30AM执行scheduling.QuartzDailyReminder任务。您可以自建QuartzEmail.class,QuartzDailyReminder.class放置在 yourappl/WEB-INF/classes/scheduling 里进行测试。
注意一个日程是由一个job和一个trigger组成,代表任务定义和时间定义。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE quartz PUBLIC
"-//Quartz Enterprise Job Scheduler//DTD Job Scheduling Data 1.0//EN"
"http://www.quartzscheduler.org/dtd/job_scheduling_data_1_0.dtd">
<quartz>
<job>
<job-detail>
<name>Job_Email</name>
<group>DEFAULT</group>
<job-class>scheduling.QuartzEmail</job-class>
</job-detail>
<trigger>
<cron>
<name>Trigger_Email</name>
<group>DEFAULT</group>
<job-name>Job_Email</job-name>
<job-group>DEFAULT</job-group>
<cron-expression>0 0/15 * * * ?</cron-expression>
</cron>
</trigger>
</job>
<job>
<job-detail>
<name>Job_Daily_Reminder</name>
<group>DEFAULT</group>
<job-class>scheduling.QuartzDailyReminder</job-class>
</job-detail>
<trigger>
<cron>
<name>Trigger_Daily_Reminder</name>
<group>DEFAULT</group>
<job-name>Job_Daily_Reminder</job-name>
<job-group>DEFAULT</job-group>
<cron-expression>0 30 8 ? * MON-FRI</cron-expression>
</cron>
</trigger>
</job>
</quartz>
4. 配置 quartz.properties ,建立Quartz实例
在 yourappl/WEB-INF/web.xml 里新建文件 quartz.properties
#
# Configure Main Scheduler Properties
#
org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = one
#
# Configure ThreadPool
#
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 4
#
# Configure JobStore
#
org.quartz.jobStore.misfireThreshold = 5000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
# ===========================================================================
# Configure SchedulerPlugins ===============================================
# ===========================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin
org.quartz.plugin.triggHistory.triggerFiredMessage = Trigger {1}.{0} fired job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy}
org.quartz.plugin.triggHistory.triggerCompleteMessage = Trigger {1}.{0} completed firing job {6}.{5} at {4, date, HH:mm:ss MM/dd/yyyy} with resulting trigger instruction code: {9}
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = /quartz_reminder.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true
好了,现在重起jsp服务器,留意logs里面的输出,就可以测试您的日程有无生效了。