我是靠谱客的博主 鲜艳睫毛,最近开发中收集的这篇文章主要介绍Quartz的冲突处理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

说明一下如何处理好quartz的调度关系

首先查看一下quartz的配置,有设置thread factory的方法,具体没有试过。还有thread factory的大小的设置

org.quartz.scheduler.jobFactory.class:com.yh.bigdata.silkworm.api.scheduler.JobFactory
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 95
org.quartz.threadPool.threadPriority: 5
org.quartz.jobStore.misfireThreshold: 1
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.useProperties=false
org.quartz.jobStore.dataSource=dev
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval: 1000

而默认来讲同类型Task的触发是允许并发,quartz内部的后一个Task实例并不会管前一个实例有没有运行完成

需要再Task之前添加注解

@DisallowConcurrentExecution

Misfire

而Task的冲突是用misfire来处理的。misfire一共可能是由三种可能性引起的:

  • 不允许并发的任务冲突
  • 任务抛出Exception
  • scheduler停止

鉴于第三种情况在我们的场景当中比较少见,着重分析一下前两种情况:

https://www.cnblogs.com/skyLogin/p/6927629.html

根据上文的说明,在配置Schedule的时候有三种策略,配置两种场景来说:

如果发生冲突的时候丢弃后一次task

@DisallowConcurrentExecution
public class DemoTrialJob implements Job {
    private static Logger LOG = LoggerFactory.getLogger(DemoTrialJob.class);

    public void execute(JobExecutionContext context) throws JobExecutionException {
        LOG.info(String.format("%s",context.getTrigger().getMisfireInstruction()));
        LOG.info(String.format("%s,Plan %s, time %s",Thread.currentThread(),
                context.getScheduledFireTime() ,java.util.Calendar.getInstance().getTime()));
//        throw  new JobExecutionException();
        try{
            Thread.sleep(4000L);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        LOG.info(String.format("End %s,Plan %s , time %s",Thread.currentThread(),
                context.getScheduledFireTime() ,java.util.Calendar.getInstance().getTime()));

    }
}


        CronScheduleBuilder csb = CronScheduleBuilder.cronSchedule("*/2 * * * * ?");
        MutableTrigger triggerCron = csb.withMisfireHandlingInstructionDoNothing().build();
        CronTriggerImpl cronTrigger = (CronTriggerImpl)triggerCron;
        cronTrigger.setName("cron trigger");
        System.err.println(triggerCron.getClass());


quartz会跳过发生冲突的任务不做处理

这里需要注意的是,quartz对于冲突的判定有一个容忍期,见配置文件当中的 org.quartz.jobStore.misfireThreshold。 如果冲突时间在这个范围以内不会被判为冲突,依然会执行

2019-01-31 20:27:48.343  INFO 9032 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:27:50.411  INFO 9032 --- [eduler_Worker-1] c.y.b.s.a.s.j.demotrialjob.DemoTrialJob  : 2
2019-01-31 20:27:50.413  INFO 9032 --- [eduler_Worker-1] c.y.b.s.a.s.j.demotrialjob.DemoTrialJob  : Thread[SpringQrtzScheduler_Worker-1,5,main],Plan Thu Jan 31 20:27:50 CST 2019, time Thu Jan 31 20:27:50 CST 2019
2019-01-31 20:27:54.414  INFO 9032 --- [eduler_Worker-1] c.y.b.s.a.s.j.demotrialjob.DemoTrialJob  : End Thread[SpringQrtzScheduler_Worker-1,5,main],Plan Thu Jan 31 20:27:50 CST 2019 , time Thu Jan 31 20:27:54 CST 2019
2019-01-31 20:27:54.679  INFO 9032 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:27:56.368  INFO 9032 --- [eduler_Worker-2] c.y.b.s.a.s.j.demotrialjob.DemoTrialJob  : 2
2019-01-31 20:27:56.374  INFO 9032 --- [eduler_Worker-2] c.y.b.s.a.s.j.demotrialjob.DemoTrialJob  : Thread[SpringQrtzScheduler_Worker-2,5,main],Plan Thu Jan 31 20:27:56 CST 2019, time Thu Jan 31 20:27:56 CST 2019
2019-01-31 20:28:00.379  INFO 9032 --- [eduler_Worker-2] c.y.b.s.a.s.j.demotrialjob.DemoTrialJob  : End Thread[SpringQrtzScheduler_Worker-2,5,main],Plan Thu Jan 31 20:27:56 CST 2019 , time Thu Jan 31 20:28:00 CST 2019
2019-01-31 20:28:00.671  INFO 9032 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:28:02.368  INFO 9032 --- [eduler_Worker-3] c.y.b.s.a.s.j.demotrialjob.DemoTrialJob  : 2
2019-01-31 20:28:02.369  INFO 9032 --- [eduler_Worker-3] c.y.b.s.a.s.j.demotrialjob.DemoTrialJob  : Thread[SpringQrtzScheduler_Worker-3,5,main],Plan Thu Jan 31 20:28:02 CST 2019, time Thu Jan 31 20:28:02 CST 2019

对于报错的任务立即重试

再来看一个场景,当任务执行失败抛出Exception的时候:

withMisfireHandlingInstructionFireAndProceed 我们把misfire策略设为这个模式

public void execute(JobExecutionContext context) throws JobExecutionException {
        LOG.info(String.format("%s",context.getTrigger().getMisfireInstruction()));
        LOG.info(String.format("%s,Plan %s, time %s",Thread.currentThread(),
                context.getScheduledFireTime() ,java.util.Calendar.getInstance().getTime()));
        throw  new JobExecutionException();
//        try{
//            Thread.sleep(4000L);
//        }catch (InterruptedException e){
//            e.printStackTrace();
//        }
//        LOG.info(String.format("End %s,Plan %s , time %s",Thread.currentThread(),
//                context.getScheduledFireTime() ,java.util.Calendar.getInstance().getTime()));

    }

2019-01-31 20:41:40.490  INFO 16796 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:41:40.910  INFO 16796 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:41:41.333  INFO 16796 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:41:41.758  INFO 16796 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:41:42.190  INFO 16796 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:41:42.609  INFO 16796 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:41:43.022  INFO 16796 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:41:43.434  INFO 16796 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.
2019-01-31 20:41:43.855  INFO 16796 --- [_MisfireHandler] org.quartz.impl.jdbcjobstore.JobStoreTX  : Handling 1 trigger(s) that missed their scheduled fire-time.

那么,当任务报错的时候,quartz不会等待下一次task的启动,会立即尝试重试

最后

以上就是鲜艳睫毛为你收集整理的Quartz的冲突处理的全部内容,希望文章能够帮你解决Quartz的冲突处理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部