quartz 定时器的动态加载和取消定时任务

import java.io.IOException;
import java.text.ParseException;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;




public class SechduleTaskServiceImpl{
private Scheduler scheduler;
public static void main(String[] args){
SechduleTaskServiceImpl s = new SechduleTaskServiceImpl();
try {
s.initTimers();
while(true){
int i = System.in.read();
System.out.println("i:"+i);
if(i=='1'){
System.out.println("启动新增触发器 2");
s.enableTimer(2);
}else if(i=='2'){
System.out.println("停止掉新增触发器 2");
s.disableTimer("");
}else if(i=='3'){
System.out.println("停止掉原始触发器 1");
s.disableTimer();
}else;
Thread.sleep(1000);
}
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//��ݶ�ʱ����� ����禁用某个定时器��ʱ��
public void disableTimer(String timerName)throws SchedulerException,ParseException {
// TODO Auto-generated method stub
scheduler = StdSchedulerFactory.getDefaultScheduler();
CronTrigger cronTrigger = (CronTrigger)scheduler.getTrigger("trigger_2", Scheduler.DEFAULT_GROUP);
if(null != cronTrigger){
scheduler.unscheduleJob(cronTrigger.getName(), Scheduler.DEFAULT_GROUP);
}
}
public void disableTimer()throws SchedulerException,ParseException {
// TODO Auto-generated method stub
scheduler = StdSchedulerFactory.getDefaultScheduler();
CronTrigger cronTrigger = (CronTrigger)scheduler.getTrigger("trigger_1", Scheduler.DEFAULT_GROUP);
if(null != cronTrigger){
scheduler.unscheduleJob(cronTrigger.getName(), Scheduler.DEFAULT_GROUP);
}
}
//��̬���ʱ�� ���启用某个定时器־
public void enableTimer(Integer timerId) throws SchedulerException,ParseException {
// TODO Auto-generated method stub
scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail jobDetail = new JobDetail("reportJob_3", Scheduler.DEFAULT_GROUP,TestJob2.class);
Trigger trigger = new CronTrigger("trigger_2",Scheduler.DEFAULT_GROUP,"5,15,25,35,45,55 * * * * ?");
scheduler.scheduleJob(jobDetail, trigger );
// scheduler.start();

}
//������启用quartz
public void initTimers() throws SchedulerException,ParseException{


// TODO Auto-generated method stub
scheduler = StdSchedulerFactory.getDefaultScheduler();
//����JobDetailʵ��
JobDetail jobDetail = new JobDetail("reportJob_1", Scheduler.DEFAULT_GROUP,TestJob.class);
JobDetail jobDetail2 = new JobDetail("reportJob_2", Scheduler.DEFAULT_GROUP,TestJob2.class);
//����trigger �����
//Trigger trigger = new SimpleTrigger("dd",Scheduler.DEFAULT_GROUP, 0, 30) ;
//
Trigger trigger = new CronTrigger("trigger_1",Scheduler.DEFAULT_GROUP,"0,10,20,30,40,50 * * * * ?");
Trigger trigger2 = (Trigger)trigger.clone();
trigger2.setName("trigger_2"); //clone 就是这样用的~~ 哈哈
// 启用 trigger
scheduler.scheduleJob(jobDetail, trigger );
// scheduler.scheduleJob(jobDetail2, trigger2 );
// scheduler.addjob
//��ʼ���
scheduler.start();
//�����еĶ�ʱ��Դ
// scheduler.shutdown();
//禁用 trigger
// scheduler.unscheduleJob(trigger.getName(), Scheduler.DEFAULT_GROUP);
/* 判断任务是否存在
* CronTrigger cronTrigger = (CronTrigger)scheduler.getTrigger("trigger_" + rj.getGuId(), Scheduler.DEFAULT_GROUP);
if(null != cronTrigger){
scheduler.unscheduleJob(cronTrigger.getName(), Scheduler.DEFAULT_GROUP);
}
*/

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值