JobExecutionException

问题1 如果你的任务执行发生错误了怎么办呀!
Quartz提供了二种解决方法
1 立即重新执行任务
2 立即停止所有相关这个任务的触发器
问题2 怎么去执行呢
Quartz的解决方式是
在你的程序出错时,用Quartz提供的JobExecutionException类相关方法很好的解决
1  立即重新执行任务
Java代码 复制代码  收藏代码22100127_dJ8A.gif
  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.        }  
try {
            int zero = 0;
            @SuppressWarnings("unused")
            int calculation = 4815 / zero;
        } catch (Exception e) {
            _log.error("执行任务出错了...");
            JobExecutionException e2 = 
                new JobExecutionException(e);
            // this job will refire immediately
            e2.setRefireImmediately(true);
            throw e2;
        }

请注意其中作者写的注释:
// this job will refire immediately
e2.setRefireImmediately(true);
2 立即停止所有相关这个任务的触发器
Java代码 复制代码  收藏代码22100127_dJ8A.gif
  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.         }  
try {
            int zero = 0;
            @SuppressWarnings("unused")
            int calculation = 4815 / zero;
        } catch (Exception e) {
            _log.info("--- Error in job!");
            JobExecutionException e2 = 
                new JobExecutionException(e);
            // Quartz will automatically unschedule
            // all triggers associated with this job
            // so that it does not run again
            e2.setUnscheduleAllTriggers(true);
            throw e2;
        }

请注意其中作者写的注释:
// Quartz will automatically unschedule
// all triggers associated with this job
// so that it does not run again
e2.setUnscheduleAllTriggers(true);
具体代码如下:
BadJob1.java
Java代码 复制代码  收藏代码22100127_dJ8A.gif
  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. }  
package org.quartz.examples.example6;

import java.util.Date;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * A job dumb job that will throw a job execution exception
 * </p>
 * 
 * @author Bill Kratzer
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class BadJob1 implements Job {
   
    private static Logger _log = LoggerFactory.getLogger(BadJob1.class);

   
    public BadJob1() {
    }

   
    public void execute(JobExecutionContext context)
        throws JobExecutionException {
        JobKey jobKey = context.getJobDetail().getKey();
        _log.error("任务key: " + jobKey + " ,执行时间: " + new Date());

        try {
            int zero = 0;
            @SuppressWarnings("unused")
            int calculation = 4815 / zero;
        } catch (Exception e) {
            _log.error("执行任务出错了...");
            JobExecutionException e2 = 
                new JobExecutionException(e);
            // this job will refire immediately
            e2.setRefireImmediately(true);
            throw e2;
        }

        _log.error("---" + jobKey + " completed at " + new Date());
    }
}

BadJob2 .java
Java代码 复制代码  收藏代码22100127_dJ8A.gif
  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. }  
package org.quartz.examples.example6;

import java.util.Date;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * A job dumb job that will throw a job execution exception
 * </p>
 * 
 * @author Bill Kratzer
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class BadJob2 implements Job {   

    private static Logger _log = LoggerFactory.getLogger(BadJob2.class); 

    public BadJob2() {
    }
  
    public void execute(JobExecutionContext context)
        throws JobExecutionException {
        JobKey jobKey = context.getJobDetail().getKey();
        _log.info("---" + jobKey + " executing at " + new Date());

        try {
            int zero = 0;
            @SuppressWarnings("unused")
            int calculation = 4815 / zero;
        } catch (Exception e) {
            _log.info("--- Error in job!");
            JobExecutionException e2 = 
                new JobExecutionException(e);
            // Quartz will automatically unschedule
            // all triggers associated with this job
            // so that it does not run again
            e2.setUnscheduleAllTriggers(true);
            throw e2;
        }

        _log.info("---" + jobKey + " completed at " + new Date());
    }
}

JobExceptionExample.java




官方参考:http://www.quartz-scheduler.org/api/2.0.0/org/quartz/JobExecutionException.html


转载于:https://my.oschina.net/kanlianhui/blog/170445

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值