[英]spring-boot @scheduled not running in different threads?
我正在配置一個計划任務以在不同的線程中運行。 這是配置代碼
@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();
}
}
我正在以 2 秒的固定速率使線程休眠 10 秒。 所以我希望在日志中看到不同的線程,但我只看到一個
日志在這里
{"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"}
@Scheduled(fixedRateString = "2000" )
不保證testaMethod
每2秒執行一次,如果時間成本超過2s,如Thread.sleep(10000)
,則新任務將被放入隊列中。 只有在舊的執行時,調度程序才會從隊列中獲取新任務並執行它。 由於新任務現在是調度程序中的唯一任務,因此可以在不創建新Thread
情況下運行它。
要解決此問題,您可以組合@Async和@Scheduled
SchedulerConfig
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;
}
}
的SchedulerTask
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();
}
}
的AsyncTask
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();
}
}
}
檢測結果
11:20:54.682 [poolScheduler1] DEBUG com.test.AsyncTask - here is the message
11:20:56.668 [poolScheduler3] DEBUG com.test.AsyncTask - here is the message
11:20:58.668 [poolScheduler2] DEBUG com.test.AsyncTask - here is the message
11:21:00.669 [poolScheduler6] DEBUG com.test.AsyncTask - here is the message
11:21:02.669 [poolScheduler7] DEBUG com.test.AsyncTask - here is the message
11:21:04.669 [poolScheduler4] DEBUG com.test.AsyncTask - here is the message
spring.task.scheduling.pool.size=20
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.