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);
}
*/
}
}