Quartz提供了二种解决方法
1 立即重新执行任务
2 立即停止所有相关这个任务的触发器
问题2 怎么去执行呢
Quartz的解决方式是
在你的程序出错时,用Quartz提供的JobExecutionException类相关方法很好的解决
1 立即重新执行任务
- 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;
- }
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 立即停止所有相关这个任务的触发器
- 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;
- }
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
- 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());
- }
- }
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
- 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());
- }
- }
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