spring-boot @scheduled没有在不同的线程中运行?

问题描述 投票:4回答:1

我正在配置一个计划任务以在不同的线程中运行。这是配置代码

@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {
    private final int POOL_SIZE = 10;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
        threadPoolTaskScheduler.setThreadNamePrefix("my-sched-pool-");
        threadPoolTaskScheduler.initialize();
        scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
    }
}

这是使用它的代码

@Scheduled(fixedRateString = "2000" )
    public void testaMethod() {

         log.debug("here is the message");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

我正在使用fixedRate为2s睡眠10s。所以我希望在日志中看到不同的线程,但我只看到一个

日志在这里

{"thread":"my-sched-pool-1","level":"DEBUG","description":"here is the message"}
{"thread":"my-sched-pool-1","level":"DEBUG","description":"here is the message"}
{"thread":"my-sched-pool-1","level":"DEBUG","description":"here is the message"}
{"thread":"my-sched-pool-1","level":"DEBUG","description":"here is the message"}
java spring multithreading spring-boot spring-scheduled
1个回答
5
投票

@Scheduled(fixedRateString = "2000" )不保证testaMethod每2秒执行一次,如果时间成本超过2s,如Thread.sleep(10000),新任务将被排入队列。只有在旧的执行时,调度程序才会从队列中获取新任务并执行它。由于新任务现在是调度程序中的唯一任务,因此可以在不创建新的Thread的情况下运行它。

要解决这个问题,你可以结合使用qazxsw poi

SchedulerConfig

@Async and @Scheduled

的SchedulerTask

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {
    private final int POOL_SIZE = 10;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setTaskScheduler(poolScheduler());
    }

    @Bean
    public TaskScheduler poolScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setThreadNamePrefix("poolScheduler");
        scheduler.setPoolSize(POOL_SIZE);
        return scheduler;
    }

}

的AsyncTask

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SchedulerTask {

    @Autowired
    private AsyncTask asyncTask;

    @Scheduled(fixedRateString = "2000" )
    public void testaMethod() {
        asyncTask.asyncMethod();
    }
}

检测结果

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

@Component
@EnableAsync
public class AsyncTask {

    private Log log = LogFactory.getLog(AsyncTask.class);

    @Async
    public void asyncMethod() {
        log.debug("here is the message");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.