Quartz 1.8.6 执行完任务才执行下一任务 并发问题处理记录

Quartz 1.8.6 执行完当前任务才执行下一任务

公司项目比较老了,使用的是Quartz 1.8.6
这个定时任务默认是并发的,到点就执行
场景是定时任务调用供应商接口,重复下发,并发时出问题

<dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>1.8.6</version>
        </dependency>
public class QuartzTest {

    private static final SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();

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

        Class cls = Class.forName("com.example.MyJob");
        Scheduler sched = gSchedulerFactory.getScheduler();
        JobDetail jobDetail = new JobDetail("模拟操作", "DEMO_JOBGROUP_NAME", cls);
        CronTrigger trigger = new CronTrigger("模拟操作", "DEMO_TRIGGERGROUP_NAME");
        trigger.setCronExpression("0/3 * * * * ?");
        sched.scheduleJob(jobDetail, trigger);
        if (!sched.isShutdown()) {
            sched.start();
        }
    }
}
public class MyJob implements Job {
    private static  Integer count = 0;
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            try {
                //获取一个1-15的随机整数
                System.out.println("线程"+Thread.currentThread().getId()+"任务开始执行");
                int i = (int) (Math.random() * 15 + 1);
                System.out.println("线程"+Thread.currentThread().getId()+"开始执行,任务执行时间"+i+"秒");
                Thread.sleep(i*1000);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
                count++;
                // 执行具体任务
                System.out.println("线程"+Thread.currentThread().getId()+" MyJob is Running ..." + "执行第:" + count + "次" +  " Date:" + sdf.format(new Date()));
                System.out.println("线程"+Thread.currentThread().getId()+"完成执行,执行任务执行时间"+i+"秒");
                System.out.println("---------------------------------------------------------------------");

            } catch (InterruptedException e) {
                throw new JobExecutionException(e);
            }
    }
}

在这里插入图片描述

更改为实现StatefulJob接口

public class MyJob implements StatefulJob {
    private static  Integer count = 0;
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            try {
                //获取一个1-15的随机整数
                System.out.println("线程"+Thread.currentThread().getId()+"任务开始执行");
                int i = (int) (Math.random() * 15 + 1);
                System.out.println("线程"+Thread.currentThread().getId()+"开始执行,任务执行时间"+i+"秒");
                Thread.sleep(i*1000);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
                count++;
                // 执行具体任务
                System.out.println("线程"+Thread.currentThread().getId()+" MyJob is Running ..." + "执行第:" + count + "次" +  " Date:" + sdf.format(new Date()));
                System.out.println("线程"+Thread.currentThread().getId()+"完成执行,执行任务执行时间"+i+"秒");
                System.out.println("---------------------------------------------------------------------");

            } catch (InterruptedException e) {
                throw new JobExecutionException(e);
            }
    }
}

在这里插入图片描述
实现了一次任务执行完成,才执行下一次
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值