码迷,mamicode.com
首页 > 其他好文 > 详细

quartz动态修改时间

时间:2015-05-18 18:08:40      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

 

 //调度工具类
public   class  QuartzManage {  
    private   static  SchedulerFactory sf =  new  StdSchedulerFactory();  
    private   static  String JOB_GROUP_NAME =  "e100egroup" ;  
    private   static  String TRIGGER_GROUP_NAME =  "e100etrigger" ;  
   
  //根据作业,存储jobDetail信息,修改触发器时间的时候用
    private static HashMap<String,JobDetail> hsmpJobDetail=new HashMap<String,JobDetail>();
 
   
   
    public   static   void  startJob(String jobName, Job job, String time,HashMap hsmpParm)  
            throws  SchedulerException, ParseException {  
        Scheduler sched = sf.getScheduler();  
  
       JobDetail jobDetail = new  JobDetail();  
        jobDetail.setName(jobName);  
        jobDetail.setGroup(JOB_GROUP_NAME);  
        jobDetail.setJobClass(job.getClass());
    
        //往job里面传递参数
        jobDetail.getJobDataMap().put("hsmpParm", hsmpParm);
  
        CronTrigger trigger = new  CronTrigger(jobName, TRIGGER_GROUP_NAME);  
        trigger.setCronExpression(time);  
        sched.scheduleJob(jobDetail, trigger);  
       
        hsmpJobDetail.put(jobName, jobDetail);
  
        if  (!sched.isShutdown()) {  
            sched.start();  
        }  
    }  
  
 
    public   static   void  modifyJobTime(String jobName, Job job, String time)  
            throws  SchedulerException, ParseException{  
        Scheduler sched = sf.getScheduler();  
        Trigger trigger = sched.getTrigger(jobName,  
                TRIGGER_GROUP_NAME);  
        if  (trigger !=  null ) {  
            CronTrigger ct = (CronTrigger) trigger;  
            // 移除当前进程的Job   
            sched.deleteJob(jobName, JOB_GROUP_NAME);  
            // 修改Trigger   
            ct.setCronExpression(time);  
           JobDetail jobDetai= hsmpJobDetail.get(jobName);
           if(jobDetai!=null){
           sched.scheduleJob(jobDetai,ct);
           System.out.println("更新调度任务 "  + ct.getJobName()+" 成功");
           }else{
        System.out.println("更新调度任务 "  + ct.getJobName()+" 失败");
           }
        }  
    }   
   
}  

 

    //作业类
public   class  JobTask  implements  Job {  
  //private final static String LOG_NAME_COMM = "QUARTZCOMM";
 
    public   void  execute(JobExecutionContext context)  
            throws  JobExecutionException {  
     HashMap<String,String> hsmpParm=(HashMap<String,String>)context.getJobDetail().getJobDataMap().get("hsmpParm");
     String scheduleId=hsmpParm.get("scheduleId");
     String scheduleName=hsmpParm.get("scheduleName");
     
     System.out.println(FuncDate.toDate(new Date(), "yyyy-MM-dd HH:mm:ss")+"  执行..... "+scheduleName+" ....开始");
     
    }  
  

 

 //测试调度任务
public   class  QuartzMain {  
    public   static   void  main(String[] args)  throws  Exception {  
       
     JobTask job = new  JobTask(); 
   
     //传递到作业类的参数
     HashMap<String, String> hashMap=new HashMap<String, String>();
     hashMap.put("scheduleId", "123456789");
       hashMap.put("scheduleName", "渠道开关控制");
       
       //加载调度任务
        QuartzManage.startJob("ming" , job,  "0/5 * 16 * * ? *" ,hashMap);
       
     Thread.sleep(1000*60*2);
        //修改调度任务时间
        QuartzManage.modifyJobTime("ming" , job,  "0/10 * 16 * * ? *" );
 
    }  

 

quartz动态修改时间

标签:

原文地址:http://www.cnblogs.com/heihusnplje/p/4512247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!