quartz任务可以随tomcat启动而启动调度,不需要手动启动调度进程。
1.修改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>
<init-param>
<param-name>config-file</param-name>
<param-value>/quartz.properties</param-value>
</init-param>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
2.在webapps/axis/WEB-INF/classes目录下建立文件quartz.properties
org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = one org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 2
org.quartz.threadPool.threadPriority = 4 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_job.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
3.在同目录建立文件 quartz_job.xml
< ?xml version="1.0" encoding="UTF-8"?>
< quartz>
< job>
<job-detail>
<name>helloworld</name>
<group>group1</group>
<job-class>HelloWorld</job-class>
</job-detail>
<trigger>
<cron>
<name>test</name>
<group>group1</group>
<job-name>helloworld</job-name>
<job-group>group1</job-group>
<cron-expression>0 0/1 * * * ?</cron-expression>
</cron>
</trigger>
< /job>
< /quartz>
4.在同目录下增加java文件并编译:
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class Helloworld implements Job{
public Helloworld() { }
private static Log _log = LogFactory.getLog(Helloworld.class);
//调度方法
public void execute(JobExecutionContext context) throws JobExecutionException {
_log.info("Hello World! - " + new Date());
}
}