在自己的线程中运行每个 Spring 调度程序

我有多个带有注释的组件,我看到Spring一次只启动一个,即使它们被安排在同一时间运行。@Scheduled

我的用例如下。我希望每个@Scheduled注释在自己的线程中运行,但每个线程只能运行一次。

给定这个伪代码和两个调度程序:

@Scheduled(cron = "0 * * * * *") //run every minute
public void methodA() {
   log.info("Running method A");
   executeLongRunningJob("Finished method A");
}

@Scheduled(cron = "0 * * * * *") //run every minute
public void methodB() {
   log.info("Running method B");
   executeLongRunningJob("Finished method B");       
}

private void executeLongRunningJob(String msg) {
    Thread.sleep(70 seconds);
    System.out.println(msg);
}

请注意,该任务花费的时间比计划程序计划运行的时间要长。这一点至关重要。我不希望调度程序在完成运行之前再次启动。

开箱即用地运行此代码会给我以下输出:

Running method A
Finished method A
Running method B
Finished method B
Running method A
Finished method A
Running method B
Finished method B
... and so on

因此,显然它在单个线程中运行两个调度程序。


当我使用昂贵的方法时,我几乎得到了正确的行为,除了昂贵的方法在新的调度程序启动之前没有完成。@Async

Running method A
Running method B
Running method A
Running method B
Finished method A
Finished method B
... and so on

我想要的是这个输出:

Running method A  
Running method B
Finished method A 
Finished method B 
Running method A 
Running method B 
Finished method A 
Finished method B
... and so on

我该如何做到这一点?我希望每个计划程序同时运行,但要等到它完成,然后才允许再次运行。请记住,我有两个以上的计划程序在同一时间运行,有时甚至不同。


答案 1

你是对的 - 默认情况下,调度程序使用大小为 1 的线程池,因此每个任务都是按顺序处理的。您可以通过配置具有所需池大小的 TaskScheduler Bean 来实现它。请考虑以下示例:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.Date;

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public TaskScheduler taskScheduler() {
        final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(10);
        return scheduler;
    }


    @Scheduled(fixedDelay = 2 * 1000L, initialDelay = 3 * 1000L)
    public void scheduled1() throws InterruptedException {
        System.out.println(new Date() + " " + Thread.currentThread().getName() + ": scheduled1");
        Thread.sleep(1000);
    }

    @Scheduled(fixedDelay = 3 * 1000L, initialDelay = 3 * 1000L)
    public void scheduled2() throws InterruptedException {
        System.out.println(new Date() + " " + Thread.currentThread().getName() + ": scheduled2");
        Thread.sleep(1000);
    }
}

它将在单独的线程中运行每个计划任务,例如:

Tue Jul 18 20:21:50 CEST 2017 taskScheduler-1: scheduled2
Tue Jul 18 20:21:50 CEST 2017 taskScheduler-2: scheduled1
Tue Jul 18 20:21:53 CEST 2017 taskScheduler-1: scheduled1
Tue Jul 18 20:21:54 CEST 2017 taskScheduler-3: scheduled2
Tue Jul 18 20:21:56 CEST 2017 taskScheduler-2: scheduled1
Tue Jul 18 20:21:58 CEST 2017 taskScheduler-4: scheduled2
Tue Jul 18 20:21:59 CEST 2017 taskScheduler-1: scheduled1

答案 2

推荐