Quartz之JobExecutionException

问题1 如果你的任务执行发生错误了怎么办呀! 
Quartz提供了二种解决方法 
1 立即重新执行任务 
2 立即停止所有相关这个任务的触发器
 
问题2 怎么去执行呢 
Quartz的解决方式是 
在你的程序出错时,用Quartz提供的JobExecutionException类相关方法很好的解决 
1  立即重新执行任务 

Java代码    收藏代码
  1. try {  
  2.            int zero = 0;  
  3.            @SuppressWarnings("unused")  
  4.            int calculation = 4815 / zero;  
  5.        } catch (Exception e) {  
  6.            _log.error("执行任务出错了...");  
  7.            JobExecutionException e2 =   
  8.                new JobExecutionException(e);  
  9.            // this job will refire immediately  
  10.            e2.setRefireImmediately(true);  
  11.            throw e2;  
  12.        }  


请注意其中作者写的注释: 
// this job will refire immediately 
e2.setRefireImmediately(true);
 
2 立即停止所有相关这个任务的触发器 

Java代码    收藏代码
  1. try {  
  2.             int zero = 0;  
  3.             @SuppressWarnings("unused")  
  4.             int calculation = 4815 / zero;  
  5.         } catch (Exception e) {  
  6.             _log.info("--- Error in job!");  
  7.             JobExecutionException e2 =   
  8.                 new JobExecutionException(e);  
  9.             // Quartz will automatically unschedule  
  10.             // all triggers associated with this job  
  11.             // so that it does not run again  
  12.             e2.setUnscheduleAllTriggers(true);  
  13.             throw e2;  
  14.         }  


请注意其中作者写的注释: 
// Quartz will automatically unschedule 
// all triggers associated with this job 
// so that it does not run again 
e2.setUnscheduleAllTriggers(true);
 
具体代码如下: 
BadJob1.java 

Java代码    收藏代码
  1. package org.quartz.examples.example6;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.quartz.DisallowConcurrentExecution;  
  6. import org.quartz.Job;  
  7. import org.quartz.JobExecutionContext;  
  8. import org.quartz.JobExecutionException;  
  9. import org.quartz.JobKey;  
  10. import org.quartz.PersistJobDataAfterExecution;  
  11. import org.slf4j.Logger;  
  12. import org.slf4j.LoggerFactory;  
  13.   
  14. /** 
  15.  * <p> 
  16.  * A job dumb job that will throw a job execution exception 
  17.  * </p> 
  18.  *  
  19.  * @author Bill Kratzer 
  20.  */  
  21. @PersistJobDataAfterExecution  
  22. @DisallowConcurrentExecution  
  23. public class BadJob1 implements Job {  
  24.      
  25.     private static Logger _log = LoggerFactory.getLogger(BadJob1.class);  
  26.   
  27.      
  28.     public BadJob1() {  
  29.     }  
  30.   
  31.      
  32.     public void execute(JobExecutionContext context)  
  33.         throws JobExecutionException {  
  34.         JobKey jobKey = context.getJobDetail().getKey();  
  35.         _log.error("任务key: " + jobKey + " ,执行时间: " + new Date());  
  36.   
  37.         try {  
  38.             int zero = 0;  
  39.             @SuppressWarnings("unused")  
  40.             int calculation = 4815 / zero;  
  41.         } catch (Exception e) {  
  42.             _log.error("执行任务出错了...");  
  43.             JobExecutionException e2 =   
  44.                 new JobExecutionException(e);  
  45.             // this job will refire immediately  
  46.             e2.setRefireImmediately(true);  
  47.             throw e2;  
  48.         }  
  49.   
  50.         _log.error("---" + jobKey + " completed at " + new Date());  
  51.     }  
  52. }  


BadJob2 .java 

Java代码    收藏代码
  1. package org.quartz.examples.example6;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.quartz.DisallowConcurrentExecution;  
  6. import org.quartz.Job;  
  7. import org.quartz.JobExecutionContext;  
  8. import org.quartz.JobExecutionException;  
  9. import org.quartz.JobKey;  
  10. import org.quartz.PersistJobDataAfterExecution;  
  11. import org.slf4j.Logger;  
  12. import org.slf4j.LoggerFactory;  
  13.   
  14. /** 
  15.  * <p> 
  16.  * A job dumb job that will throw a job execution exception 
  17.  * </p> 
  18.  *  
  19.  * @author Bill Kratzer 
  20.  */  
  21. @PersistJobDataAfterExecution  
  22. @DisallowConcurrentExecution  
  23. public class BadJob2 implements Job {     
  24.   
  25.     private static Logger _log = LoggerFactory.getLogger(BadJob2.class);   
  26.   
  27.     public BadJob2() {  
  28.     }  
  29.     
  30.     public void execute(JobExecutionContext context)  
  31.         throws JobExecutionException {  
  32.         JobKey jobKey = context.getJobDetail().getKey();  
  33.         _log.info("---" + jobKey + " executing at " + new Date());  
  34.   
  35.         try {  
  36.             int zero = 0;  
  37.             @SuppressWarnings("unused")  
  38.             int calculation = 4815 / zero;  
  39.         } catch (Exception e) {  
  40.             _log.info("--- Error in job!");  
  41.             JobExecutionException e2 =   
  42.                 new JobExecutionException(e);  
  43.             // Quartz will automatically unschedule  
  44.             // all triggers associated with this job  
  45.             // so that it does not run again  
  46.             e2.setUnscheduleAllTriggers(true);  
  47.             throw e2;  
  48.         }  
  49.   
  50.         _log.info("---" + jobKey + " completed at " + new Date());  
  51.     }  
  52. }  


JobExceptionExample.java 

Java代码    收藏代码
  1. package org.quartz.examples.example6;  
  2.   
  3. import static org.quartz.JobBuilder.newJob;  
  4. import static org.quartz.SimpleScheduleBuilder.simpleSchedule;  
  5. import static org.quartz.TriggerBuilder.newTrigger;  
  6. import static org.quartz.DateBuilder.*;  
  7.   
  8. import java.util.Date;  
  9.   
  10. import org.quartz.JobDetail;  
  11. import org.quartz.Scheduler;  
  12. import org.quartz.SchedulerFactory;  
  13. import org.quartz.SchedulerMetaData;  
  14. import org.quartz.SimpleTrigger;  
  15. import org.quartz.impl.StdSchedulerFactory;  
  16. import org.slf4j.Logger;  
  17. import org.slf4j.LoggerFactory;  
  18.   
  19. /** 
  20.  *  
  21.  * This job demonstrates how Quartz can handle JobExecutionExceptions that are 
  22.  * thrown by jobs. *  
  23.  * @author Bill Kratzer 
  24.  */  
  25. public class JobExceptionExample {  
  26.   
  27.     public void run() throws Exception {  
  28.         Logger log = LoggerFactory.getLogger(JobExceptionExample.class);  
  29.   
  30.        
  31.         SchedulerFactory sf = new StdSchedulerFactory();  
  32.         Scheduler sched = sf.getScheduler();  
  33.        
  34.         Date startTime = nextGivenSecondDate(null15);  
  35.   
  36.         
  37.         JobDetail job = newJob(BadJob1.class)  
  38.             .withIdentity("badJob1""group1")  
  39.             .build();  
  40.           
  41.         SimpleTrigger trigger = newTrigger()   
  42.             .withIdentity("trigger1""group1")  
  43.             .startAt(startTime)  
  44.             .withSchedule(simpleSchedule())  
  45.             .build();  
  46.   
  47.         Date ft = sched.scheduleJob(job, trigger);  
  48.         log.error(job.getKey() + " will run at: " + ft + " and repeat: "  
  49.                 + trigger.getRepeatCount() + " times, every "  
  50.                 + trigger.getRepeatInterval() / 1000 + " seconds");  
  51.   
  52.         
  53.         job = newJob(BadJob2.class)  
  54.             .withIdentity("badJob2""group1")  
  55.             .build();  
  56.           
  57.         trigger = newTrigger()   
  58.             .withIdentity("trigger2""group1")  
  59.             .startAt(startTime)  
  60.             .withSchedule(simpleSchedule()  
  61.                     .withIntervalInSeconds(3)  
  62.                     .repeatForever())  
  63.             .build();  
  64.   
  65.         ft = sched.scheduleJob(job, trigger);  
  66.         log.error(job.getKey() + " will run at: " + ft + " and repeat: "  
  67.                 + trigger.getRepeatCount() + " times, every "  
  68.                 + trigger.getRepeatInterval() / 1000 + " seconds");      
  69.           
  70.         sched.start();  
  71.           
  72.         try {  
  73.             // sleep for 60 seconds  
  74.             Thread.sleep(60L * 1000L);  
  75.         } catch (Exception e) {  
  76.               
  77.         }  
  78.   
  79.         sched.shutdown(true);  
  80.   
  81.         SchedulerMetaData metaData = sched.getMetaData();  
  82.         log.error("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");  
  83.     }  
  84.   
  85.     public static void main(String[] args) throws Exception {  
  86.   
  87.         JobExceptionExample example = new JobExceptionExample();  
  88.         example.run();  
  89.     }  
  90. }  

猜你喜欢

转载自zxb1985.iteye.com/blog/1841408