gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:00:48 24 4
gpt4 key购买 nike

我正在配置计划任务以在不同线程中运行。下面是配置代码

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

}

我正在让线程 hibernate 10 秒,固定速率为 2 秒。所以我希望在日志中看到不同的线程但我只看到一个

日志在这里

{"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 and @Scheduled

调度器配置

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

}

调度任务

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

测试结果

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

关于java - spring-boot @scheduled 没有在不同的线程中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48233445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com