gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:13:49 24 4
gpt4 key购买 nike

我有 2 个调度程序,它们以 5 秒的 fixedDelay 执行。


我有 2 个用例:

  1. 如果If - 条件 BusinessLogic 类为真,那么我想要 sleep 两个调度程序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 部分执行时,只有一个调度程序会额外 hibernate 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) 中的相同值声明并会以相同的方式表现

关于java - 使用 spring-boot @scheduled 注解同时执行两个调度程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56311500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com