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注解也加上。

上一篇:SpringBoot项目快速整合Quartz


下一篇:Quartz使用AutoFac依赖注入问题小结