Spring的@Scheduled注解按照相同的时间实例顺序执行

huangapple go评论79阅读模式
英文:

Spring @Scheduled annotation execute in order for the same time instance

问题

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
public class Scheduler {

    @Scheduled(cron = "0 */1 * * * ?")
    public void method1()  {
        System.out.println("1");
    }

    @Scheduled(cron = "0 */2 * * * ?")
    public void method2(){
        System.out.println("2");
    }

    @Scheduled(cron = "0 */1 * * * ?")
    public void method3()  {
        System.out.println("3");
    }

    @Scheduled(cron = "0 */2 * * * ?")
    public void method4()  {
        System.out.println("4");
    }
}
英文:
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
public class Scheduler {

    @Scheduled(cron = "0 */1 * * * ?")
    public void method1()  {
        System.out.println("1");
    }

    @Scheduled(cron = "0 */2 * * * ?")
    public void method2(){
        System.out.println("2");
    }

    @Scheduled(cron = "0 */1 * * * ?")
    public void method3()  {
        System.out.println("3");
    }

    @Scheduled(cron = "0 */2 * * * ?")
    public void method4()  {
        System.out.println("4");
    }
}

Actual Output:

1
3

1
3
2
4

3
1

1
2
3
4

1
3

3
1
2
4

The output that I am receiving is completely random on the same instance of time. But I want to order output for the same instance of time in the following way mentioned below:

1
3

1
2
3
4

1
3

1
2
3
4

Is this possible to achieve the above scenario using the same Cron Expression?

答案1

得分: 2

您需要添加更小的时间单位

@Scheduled(cron = "0 */1 * * * ?")
public void method1()  {
    System.out.println("1");
}

@Scheduled(cron = "1 */2 * * * ?")
public void method2(){
    System.out.println("2");
}

@Scheduled(cron = "2 */1 * * * ?")
public void method3()  {
    System.out.println("3");
}

@Scheduled(cron = "3 */2 * * * ?")
public void method4()  {
    System.out.println("4");
}

编辑其他实现

public void method1()  {
    System.out.println("1");
}

public void method2(){
    System.out.println("2");
}

public void method3()  {
    System.out.println("3");
}

public void method4()  {
    System.out.println("4");
}

@Scheduled(cron = "0 */2 * * * *")
public void even() {
    method1();
    method2();
    method3();
    method4();
}

@Scheduled(cron = "0 1/2 * * * *")
public void odd() {
    method1();
    method3();
}
英文:

You would have to add smaller unit of time:

@Scheduled(cron = "0 */1 * * * ?")
public void method1()  {
System.out.println("1");
}
@Scheduled(cron = "1 */2 * * * ?")
public void method2(){
System.out.println("2");
}
@Scheduled(cron = "2 */1 * * * ?")
public void method3()  {
System.out.println("3");
}
@Scheduled(cron = "3 */2 * * * ?")
public void method4()  {
System.out.println("4");
}

Edit: other implementation:

public void method1()  {
System.out.println("1");
}
public void method2(){
System.out.println("2");
}
public void method3()  {
System.out.println("3");
}
public void method4()  {
System.out.println("4");
}
@Scheduled(cron = "0 */2 * * * *")
public void even() {
method1();
method2();
method3();
method4();
}
@Scheduled(cron = "0 1/2 * * * *")
public void odd() {
method1();
method3();
}

huangapple
  • 本文由 发表于 2020年3月17日 00:17:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/60709540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定