簡體   English   中英

使用spring-boot @scheduled注釋同時執行兩個調度程序

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

我有2個調度程序,它以5s的fixedDelay執行。


我有2個用例:

  1. 如果If - condition BusinessLogic類為true,那么我想讓兩個調度程序都休眠 3秒 ,這意味着兩個調度程序應該在8秒 [5秒+ 3秒]后立即執行
  2. 如果代碼限定了else條件,那么兩個調度程序應該繼續以5秒的固定延遲執行。


代碼
調度程序類:

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);
    }
}



業務邏輯類:

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;
    }

}



問題

  • 兩個調度程序都不是同時啟動的。
  • 當if部分正在執行時,只有一個調度程序再睡3秒鍾,但我希望兩個調度程序都這樣做。



記錄參考:

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



請幫忙..

在每個調度程序中, if(randomGen() < 100)彼此獨立調用。 因此,對於一個調度程序,它可以給結果> 100,對於其他<100或兩者,它可以是相同的。 您需要做的是在調度程序之外運行randomGen()並以一種兩個調度程序都可以訪問它的方式存儲單個結果,然后它們將依賴於if(randomGenValue < 100)語句中的相同值並且將表現方式相同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM