简体   繁体   English

使用spring-boot @scheduled注释同时执行两个调度程序

[英]Execute two schedulers simultaneously with spring-boot @scheduled annotation

I have 2 schedulers, which executes at a fixedDelay of 5s. 我有2个调度程序,它以5s的fixedDelay执行。


I have 2 use-cases: 我有2个用例:

  1. If If - condition BusinessLogic class is true, then I want to sleep both the schedulers for a time of 3 secs , which means both the schedulers should execute now after 8 secs [5 secs + 3 secs]. 如果If - condition BusinessLogic类为true,那么我想让两个调度程序都休眠 3秒 ,这意味着两个调度程序应该在8秒 [5秒+ 3秒]后立即执行
  2. If code qualifies the else condition, then both the schedulers should continue to execute at fixed delay of 5 secs. 如果代码限定了else条件,那么两个调度程序应该继续以5秒的固定延迟执行。


Code : 代码
Scheduler class: 调度程序类:

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TestSchedulers {

    @Autowired
    private BusinessLogic businessLogic;

    @Scheduled(fixedDelay = 5000)
    public void scheduler1(){
        Date currentDate = new Date();
        System.out.println("Started Sceduler 1 at " + currentDate);
        String schedulerName = "Scheduler one";
        businessLogic.logic(schedulerName);
    }
    @Scheduled(fixedDelay = 5000)
    public void scheduler2(){
        Date currentDate= new Date();
        System.out.println("Started Sceduler 2 at " + currentDate);
        String schedulerName = "Scheduler two";
        businessLogic.logic(schedulerName);
    }
}



Business logic class: 业务逻辑类:

import java.util.Random;

import org.springframework.stereotype.Service;

@Service
public class BusinessLogic {

    public void logic(String schedulerName) {
        if(randomGen() < 100){
            System.out.println("\nExecuting If condition for [" + schedulerName + "]");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else if(randomGen() > 100){
            System.out.println("\nExecuting Else condition for [" + schedulerName + "]");
        }
    }

    //Generate random numbers

    public int randomGen(){
        Random rand = new Random();
        int randomNum = rand.nextInt((120 - 90) + 1) + 90;
        return randomNum;
    }

}



The problem 问题

  • Both the schedulers are not starting at the same time. 两个调度程序都不是同时启动的。
  • When the if part is executing, then only one schedulers sleep for extra 3 secs, but I want both theschedulers to do so. 当if部分正在执行时,只有一个调度程序再睡3秒钟,但我希望两个调度程序都这样做。



Log for reference: 记录参考:

Started Sceduler 1 at Sun May 26 12:34:53 IST 2019

Executing If condition for [Scheduler one]
2019-05-26 12:34:53.266  INFO 9028 --- [           main] project.project.App                      : Started App in 1.605 seconds (JVM running for 2.356)
Started Sceduler 2 at Sun May 26 12:34:56 IST 2019

Executing If condition for [Scheduler two]
Started Sceduler 1 at Sun May 26 12:35:01 IST 2019

Executing Else condition for [Scheduler one]
Started Sceduler 2 at Sun May 26 12:35:04 IST 2019

Executing Else condition for [Scheduler two]
Started Sceduler 1 at Sun May 26 12:35:06 IST 2019

Executing If condition for [Scheduler one]
Started Sceduler 2 at Sun May 26 12:35:09 IST 2019

Executing Else condition for [Scheduler two]
Started Sceduler 1 at Sun May 26 12:35:14 IST 2019

Executing If condition for [Scheduler one]
Started Sceduler 2 at Sun May 26 12:35:17 IST 2019

Executing If condition for [Scheduler two]
Started Sceduler 1 at Sun May 26 12:35:22 IST 2019

Executing Else condition for [Scheduler one]
Started Sceduler 2 at Sun May 26 12:35:25 IST 2019

Executing Else condition for [Scheduler two]
Started Sceduler 1 at Sun May 26 12:35:27 IST 2019



please help.. 请帮忙..

In each scheduler you invoke if(randomGen() < 100) independently of each other. 在每个调度程序中, if(randomGen() < 100)彼此独立调用。 So for one scheduler it could give result > 100 and for other < 100 or for both it could be the same. 因此,对于一个调度程序,它可以给结果> 100,对于其他<100或两者,它可以是相同的。 What you will need to do is to run randomGen() outside of the schedulers and store the single result in a way that both schedulers can access it and then they will rely on the same value in their if(randomGenValue < 100) statement and will behave the same way 您需要做的是在调度程序之外运行randomGen()并以一种两个调度程序都可以访问它的方式存储单个结果,然后它们将依赖于if(randomGenValue < 100)语句中的相同值并且将表现方式相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM