1、把quartz-*.jar 包放到lib目录下,他还需要 slf4j-api-*.jar 、 jta-*.jar
2.web.xml
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>config-file</param-name>
<param-value>quartz.properties</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
或者
<servlet>
3、在src目录下建立 quartz.properties
<1>直接连接数据库
#============================================================
# Configure Main Scheduler Properties
#===========================================================
org.quartz.scheduler.instanceName = MyClusteredScheduler
org.quartz.scheduler.instanceId = AUTO
#===========================================================
# Configure ThreadPool
#===========================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5
#===========================================================
# Configure JobStore
#===========================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
#===========================================================
# Configure Datasources
#===========================================================
org.quartz.dataSource.myDS.driver = oracle.jdbc.driver.OracleDriver
org.quartz.dataSource.myDS.URL = jdbc:oracle:thin:@ip:1521:dbname
org.quartz.dataSource.myDS.user = QRTZ
org.quartz.dataSource.myDS.password = pwd
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery=select 0 from dual
<2>使用jndi<NewBMQrtz>连接数据库
#============================================================
# Configure Main Scheduler Properties
#===========================================================
org.quartz.scheduler.instanceName = MyClusteredScheduler
org.quartz.scheduler.instanceId = AUTO
#===========================================================
# Configure ThreadPool
#===========================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5
#===========================================================
# Configure JobStore
#===========================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
#===========================================================
# Configure Datasources
#===========================================================
org.quartz.dataSource.myDS.jndiURL = NewBMQrtz
4、在src目录下建立quartz_job.xml
其中的 <job-class> 是自己定义的实现org.quartz.Job接口,有一个空的构造函数的类
【管理使用】
import java.text.ParseException;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
/**
* @Title:Quartz管理类
*
* @Description:
*
* @Copyright:
* @author zz 2008-10-8 14:19:01
* @version 1.00.000
*
*/
public class QuartzManager {
private static SchedulerFactory sf = new StdSchedulerFactory();
private static String JOB_GROUP_NAME = "group1";
private static String TRIGGER_GROUP_NAME = "trigger1";
/**
* 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
* @param jobName 任务名
* @param job 任务
* @param time 时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName,Job job,String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, job.getClass());//任务名,任务组,任务执行类
//触发器
CronTrigger trigger =
new CronTrigger(jobName, TRIGGER_GROUP_NAME);//触发器名,触发器组
trigger.setCronExpression(time);//触发器时间设定
sched.scheduleJob(jobDetail,trigger);
//启动
if(!sched.isShutdown())
sched.start();
}
/**
* 添加一个定时任务
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param job 任务
* @param time 时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName,String jobGroupName,
String triggerName,String triggerGroupName,
Job job,String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, job.getClass());//任务名,任务组,任务执行类
//触发器
CronTrigger trigger =
new CronTrigger(triggerName, triggerGroupName);//触发器名,触发器组
trigger.setCronExpression(time);//触发器时间设定
sched.scheduleJob(jobDetail,trigger);
if(!sched.isShutdown())
sched.start();
}
/**
* 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
* @param jobName
* @param time
* @throws SchedulerException
* @throws ParseException
*/
public static void modifyJobTime(String jobName,String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler();
Trigger trigger = sched.getTrigger(jobName,TRIGGER_GROUP_NAME);
if(trigger != null){
CronTrigger ct = (CronTrigger)trigger;
ct.setCronExpression(time);
sched.resumeTrigger(jobName,TRIGGER_GROUP_NAME);
}
}
/**
* 修改一个任务的触发时间
* @param triggerName
* @param triggerGroupName
* @param time
* @throws SchedulerException
* @throws ParseException
*/
public static void modifyJobTime(String triggerName,String triggerGroupName,
String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler();
Trigger trigger = sched.getTrigger(triggerName,triggerGroupName);
if(trigger != null){
CronTrigger ct = (CronTrigger)trigger;
//修改时间
ct.setCronExpression(time);
//重启触发器
sched.resumeTrigger(triggerName,triggerGroupName);
}
}
/**
* 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
* @param jobName
* @throws SchedulerException
*/
public static void removeJob(String jobName)
throws SchedulerException{
Scheduler sched = sf.getScheduler();
sched.pauseTrigger(jobName,TRIGGER_GROUP_NAME);//停止触发器
sched.unscheduleJob(jobName,TRIGGER_GROUP_NAME);//移除触发器
sched.deleteJob(jobName,JOB_GROUP_NAME);//删除任务
}
/**
* 移除一个任务
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
* @throws SchedulerException
*/
public static void removeJob(String jobName,String jobGroupName,
String triggerName,String triggerGroupName)
throws SchedulerException{
Scheduler sched = sf.getScheduler();
sched.pauseTrigger(triggerName,triggerGroupName);//停止触发器
sched.unscheduleJob(triggerName,triggerGroupName);//移除触发器
sched.deleteJob(jobName,jobGroupName);//删除任务
}
}
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class TestMGJob implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println("★★★★★★★★★★★");
}
}
public class QuartzTest {
/** *//**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestMGJob job = new TestMGJob();
QuartzManager qmg = new QuartzManager();
String job_name ="TEST1";
try {
System.out.println("【系统启动】");
qmg .addJob(job_name,job,"0/5 * * * * ?");
Thread.sleep(10000);
System.out.println("【修改时间】");
qmg .modifyJobTime(job_name,"0/10 * * * * ?");
Thread.sleep(20000);
System.out.println("【移除定时】");
qmg .removeJob(job_name);
Thread.sleep(10000);
System.out.println("\n【添加定时任务】");
qmg .addJob(job_name,job,"0/5 * * * * ?");
} catch (Exception e) {
e.printStackTrace();
}
}
}