zoukankan      html  css  js  c++  java
  • java quartz 设置定时任务串行执行

    下面以一个简单的quartz定时任务为例说明如何设置定时任务串行执行(在很多场景下我们是想让一个定时任务跑完后再跑下一个任务的),首先看默认的定时任务如何执行:

    Job类:

    package quartzDemo;
    
    import cn.hutool.core.date.DateUtil;
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    public class JobDemo implements Job {
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            System.out.println(DateUtil.now() + ": " + "into job" + "---thread: " + Thread.currentThread().getId());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(DateUtil.now() + ": " + "finish job" + "---thread: " + Thread.currentThread().getId());
    
        }
    }

    执行类:

    package quartzDemo;
    
    import org.quartz.*;
    import org.quartz.impl.StdSchedulerFactory;
    
    public class Demo {
    
        public static void main(String[] args) throws SchedulerException {
    
            JobDetail jobDetail = JobBuilder.newJob(JobDemo.class).withIdentity("myJob", "myGroup").build();
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "myGroup").startNow().withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ? ")).build();
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.scheduleJob(jobDetail, trigger);
            scheduler.start();
        }
    }

    运行结果:

    2021-10-10 21:43:56: into job---thread: 12
    2021-10-10 21:43:58: into job---thread: 13
    2021-10-10 21:44:00: into job---thread: 14
    2021-10-10 21:44:02: into job---thread: 15
    2021-10-10 21:44:02: finish job---thread: 12
    2021-10-10 21:44:03: finish job---thread: 13
    2021-10-10 21:44:04: into job---thread: 16
    2021-10-10 21:44:05: finish job---thread: 14
    2021-10-10 21:44:06: into job---thread: 17
    2021-10-10 21:44:07: finish job---thread: 15
    2021-10-10 21:44:08: into job---thread: 18
    2021-10-10 21:44:09: finish job---thread: 16

    结论:

    默认情况下,每个定时任务都会开启一个新的线程(线程池中的线程,损耗并不大),而且如果任务执行时间过长(5s),超过定时任务的间隔(2s)时,任务是会并行执行的(即不会等上一个任务执行完成后再执行下一个任务)。

    那如何设置任务串行执行呢,加上@DisallowConcurrentExecution注解,job类如下:

    package quartzDemo;
    
    import cn.hutool.core.date.DateUtil;
    import org.quartz.DisallowConcurrentExecution;
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    @DisallowConcurrentExecution
    public class JobDemo implements Job {
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            System.out.println(DateUtil.now() + ": " + "into job" + "---thread: " + Thread.currentThread().getId());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(DateUtil.now() + ": " + "finish job" + "---thread: " + Thread.currentThread().getId());
    
        }
    }

    运行结果:

    2021-10-10 21:49:50: into job---thread: 14
    2021-10-10 21:49:55: finish job---thread: 14
    2021-10-10 21:49:55: into job---thread: 15
    2021-10-10 21:50:00: finish job---thread: 15
    2021-10-10 21:50:00: into job---thread: 16
    2021-10-10 21:50:05: finish job---thread: 16
    2021-10-10 21:50:05: into job---thread: 17

    结论:

    各任务是串行执行的(虽然不同任务跑在不同的线程中),即上一个任务执行完后再执行下一个任务。

    另外:

    @PersistJobDataAfterExecution 加在Job上,表示当正常执行完Job后, JobDataMap中的数据应该被改动, 以被下一次调用时用。当使用@PersistJobDataAfterExecution 注解时, 为了避免并发时, 存储数据造成混乱, 建议把@DisallowConcurrentExecution注解也加上。

  • 相关阅读:
    (转) Nova是如何统计OpenStack资源
    (转) 一次批量重启引发的Neutron网络故障
    (转)理解Keystone的四种Token
    neutron 虚拟机网络问题调试
    转 Nova: 虚机的块设备总结 [Nova Instance Block Device]
    kvm 客户机系统的代码是如何运行的
    12 while循环
    11猜拳游戏
    10 if 嵌套
    09 if...elif..else语句
  • 原文地址:https://www.cnblogs.com/silenceshining/p/15390887.html
Copyright © 2011-2022 走看看