Quartz之JobExecutionException

12 篇文章 0 订阅
[b]问题1 如果你的任务执行发生错误了怎么办呀![/b]
[i]Quartz提供了二种解决方法[/i]
[color=red]1 立即重新执行任务
2 立即停止所有相关这个任务的触发器[/color]
[b]问题2 怎么去执行呢[/b]
[i]Quartz的解决方式是[/i]
[b]在你的程序出错时,用Quartz提供的JobExecutionException类相关方法很好的解决[/b]
[u]1 立即重新执行任务[/u]

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;
}

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

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;
}

[b]请注意其中作者写的注释:[/b]
[color=red] // Quartz will automatically unschedule
// all triggers associated with this job
// so that it does not run again
e2.setUnscheduleAllTriggers(true);[/color]
具体代码如下:
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());
}
}

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());
}
}

JobExceptionExample.java

package org.quartz.examples.example6;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.DateBuilder.*;

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* This job demonstrates how Quartz can handle JobExecutionExceptions that are
* thrown by jobs. *
* @author Bill Kratzer
*/
public class JobExceptionExample {

public void run() throws Exception {
Logger log = LoggerFactory.getLogger(JobExceptionExample.class);


SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();

Date startTime = nextGivenSecondDate(null, 15);


JobDetail job = newJob(BadJob1.class)
.withIdentity("badJob1", "group1")
.build();

SimpleTrigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startAt(startTime)
.withSchedule(simpleSchedule())
.build();

Date ft = sched.scheduleJob(job, trigger);
log.error(job.getKey() + " will run at: " + ft + " and repeat: "
+ trigger.getRepeatCount() + " times, every "
+ trigger.getRepeatInterval() / 1000 + " seconds");


job = newJob(BadJob2.class)
.withIdentity("badJob2", "group1")
.build();

trigger = newTrigger()
.withIdentity("trigger2", "group1")
.startAt(startTime)
.withSchedule(simpleSchedule()
.withIntervalInSeconds(3)
.repeatForever())
.build();

ft = sched.scheduleJob(job, trigger);
log.error(job.getKey() + " will run at: " + ft + " and repeat: "
+ trigger.getRepeatCount() + " times, every "
+ trigger.getRepeatInterval() / 1000 + " seconds");

sched.start();

try {
// sleep for 60 seconds
Thread.sleep(60L * 1000L);
} catch (Exception e) {

}

sched.shutdown(true);

SchedulerMetaData metaData = sched.getMetaData();
log.error("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
}

public static void main(String[] args) throws Exception {

JobExceptionExample example = new JobExceptionExample();
example.run();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值