Quartz2-Quartz与tomcat集成
Quartz只与tomcat简单集成,不用其他框架。
- 写一个要需要执行任务的类DumbJob,实现Job接口
- package com.lll;
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- public class DumbJob implements Job {
- public void execute(JobExecutionContext arg0) throws JobExecutionException {
- System.err.println("DumbJob is executing.");
- }
- }
- 建两个配置文件quartz.properties和quartz_job.xml,放在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
- quartz_job.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <quartz>
- <job>
- <job-detail>
- <name>helloworld</name>
- <group>group1</group>
- <job-class>com.lll.DumbJob</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/1 * * * * ?</cron-expression>
- </cron>
- </trigger>
- </job>
- </quartz>
- 配置web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>
- QuartzInitializer
- </servlet-name>
- <servlet-class>
- org.quartz.ee.servlet.QuartzInitializerServlet
- </servlet-class>
- <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>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
转自:http://stranger2008.iteye.com/blog/852258