简体   繁体   English

在自己的线程中运行每个Spring Scheduler

[英]Running each Spring Scheduler in its own thread

I have multiple components with @Scheduled annotations, and I see that Spring only starts one at a time, even if they are scheduled to run on the same time. 我有多个带有@Scheduled注释的组件,我发现Spring一次只启动一个,即使它们被安排在同一时间运行。

My use case is as follows. 我的用例如下。 I want each @Scheduled annotation to run in its own thread, but only once for each thread. 我希望每个@Scheduled注释都在自己的线程中运行,但每个线程只运行一次。

Given this pseudo code with two schedulers: 给定这个带有两个调度程序的伪代码

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

Note that the task takes longer time than the scheduler is scheduled to run. 请注意,该任务所需的时间比计划的调度程序运行时间长。 This is crucial. 这很关键。 I don't want the scheduler to start again before its finished running. 我不希望调度程序在完成运行之前重新启动。

Running this code out of the box gives me this output: 从开箱即用运行此代码为我提供了以下输出:

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

So obviously its running both schedulers in a single thread. 显然它在一个线程中运行两个调度程序。


When I put @Async on my expensive method, then I almost get the correct behavior, except the expensive method is not finished before a new scheduler is started. 当我把@Async放在我昂贵的方法上时,我几乎得到了正确的行为,除了在新的调度程序启动之前没有完成昂贵的方法。

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

What I would like is this output: 我想要的是这个输出:

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

How can I accomplish this? 我怎么能做到这一点? I want each Scheduler to run concurrently, but wait until it is finished before allowed to run again. 我希望每个调度程序同时运行,但等到它完成后再允许再次运行。 Remember I have more than two Schedulers running in same and sometimes different times. 请记住,我有两个以上的调度程序在相同且有时不同的时间运行。

You're right - by default scheduler uses a thread pool with size 1, so every task is being processed sequentially. 你没错 - 默认情况下,调度程序使用大小为1的线程池,因此每个任务都按顺序处理。 You can it by configuring TaskScheduler bean with desired pool size. 您可以通过配置具有所需池大小的TaskScheduler bean来实现。 Consider following example: 考虑以下示例:

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

It will run every scheduled task in a separate thread, for example: 它将在单独的线程中运行每个计划任务,例如:

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

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

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