`

Quartz之JobExecutionException

阅读更多
问题1 如果你的任务执行发生错误了怎么办呀!
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;
        }

请注意其中作者写的注释:
// 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;
        }

请注意其中作者写的注释:
// 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());
    }
}

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();
    }
}
分享到:
评论

相关推荐

    Android studio下的quartz工程

    import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 在这里编写你的任务逻辑 //...

    SpringBoot 整合Quartz(集群)实现定时任务调度

    import org.quartz.JobExecutionException; @DisallowConcurrentExecution public class MyTask implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { ...

    springboot2.0整合quartz

    import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 在这里编写你的定时任务逻辑...

    springboot整合quartz定时任务yml文件配置方式

    import org.quartz.JobExecutionException; import org.springframework.stereotype.Component; @Component @DisallowConcurrentExecution public class MyJob implements Job { @Override public void execute...

    Quartz_框架快速入门

    调度器不关注 `execute()` 方法的返回结果,除非 Job 在执行过程中抛出 `org.quartz.JobExecutionException` 异常。 以下是一个简单的 Quartz Job 示例,这个 Job 会扫描指定目录中的文件并打印文件的详细信息: `...

    Quartz框架快速入门.pdf

    import org.quartz.JobExecutionException; public class FileScannerJob implements Job { private static final Log log = LogFactory.getLog(FileScannerJob.class); @Override public void execute...

    spring注解Quartz定时执行功能

    import org.quartz.JobExecutionException; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @DisallowConcurrentExecution @...

    springboot2.3集成quartz定时任务持久化数据库,支持集群

    import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 任务逻辑代码 } } ``` ...

    定时器quartz的使用方法

    import org.quartz.JobExecutionException; public class HelloJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello,...

    quartz 实现按天、按周、按月定时任务的简单demo

    import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 这里编写你的任务逻辑 ...

    spring boot集成quartz定时器

    import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 这里编写你的任务逻辑 } } ...

    spring java 定时器 执行两次 quartz

    import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 这里编写你的任务逻辑 } } ...

    spring定时任务之Quartz

    import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 在这里编写你要执行的业务...

    quartz2.1.6

    import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("My Job is...

    quartz-2.2.2-distribution.rar

    import org.quartz.JobExecutionException; @Component public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 执行你的业务...

    maven-quartz(定时任务)最简单版本

    import org.quartz.JobExecutionException; public class SimpleJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 在这里编写你的任务逻辑...

    Java_Spring与Quartz的整合

    - 通过实现`org.springframework.scheduling.quartz.JobExecutionException`,可以捕获Job执行过程中的异常,进行统一处理。 8. **事务支持** - 如果Job需要在数据库事务中执行,可以利用Spring的事务管理功能,...

    关于spring中quartz的配置

    public void execute(JobExecutionContext context) throws JobExecutionException { // 在这里编写你的任务逻辑 } } ``` 3. **定义Trigger** Trigger是触发Job执行的时间规则,可以是简单触发器...

    quartz使用cron表达式的实例

    import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 这里写你的任务代码 } } ``...

    Quartz框架快速入门

    import org.quartz.JobExecutionException; public class ScanDirectoryJob implements Job { static Log logger = LogFactory.getLog(ScanDirectoryJob.class); public void execute(JobExecutionContext ...

Global site tag (gtag.js) - Google Analytics