我正在使用spring-boot和@Scheduled注释来执行某些任务.
如何在spring-boot中找出默认的计划任务的默认池大小?
原因:以下类不并行执行作业,而是一个接一个地执行.也许默认情况下只配置一个线程执行程序?
@Service
public class ZipFileTesterAsync {
@Scheduled(fixedDelay = 60000, initialDelay = 500)
public void run() throws Exception {
System.out.println("import 1");
TimeUnit.MINUTES.sleep(1);
System.out.println("import 1 finished");
}
@Scheduled(fixedDelay = 60000, initialDelay = 1000)
public void run2() throws Exception {
System.out.println("import 2");
TimeUnit.MINUTES.sleep(1);
}
}
Run Code Online (Sandbox Code Playgroud)
结果:第一个完成后执行第二个作业.
是否可以像health在“主”应用程序的单独线程池中那样处理执行器请求?
我为什么要问?我有一个应用程序,有时可能会用完所有可用线程,并且 Kubernetes 运行状况检查由于线程不可用来计算运行状况端点请求而失败。
我想确保无论应用程序承受多少负载,都会处理每个运行状况请求。
我正在考虑为执行器定义一个单独的线程池来操作,但我不知道如何做到这一点。
我有一份每小时运行一次的工作,我正在使用Spring的@scheduled cron来安排它.
如果工作需要一个多小时,我从如何防止Spring中的重叠计划中了解到?第一份工作正在运行时,下一份工作不会启动.
但这是否意味着它将在第一份工作完成后启动,或者是否错过了机会?
如果我有一个需要10个小时的工作,那么所有错过的cron作业是否会排队,然后在第一个作业在10个小时后完成时逐个执行,或者只是第一个作业运行?
谢谢!
我有多个带@Scheduled注释的组件,我看到Spring一次只启动一个,即使它们被安排在同一时间运行.
我的用例如下.我希望每个@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);
}
Run Code Online (Sandbox Code Playgroud)
请注意,该任务所需的时间比计划的调度程序运行时间长.这很关键.我不希望调度程序在完成运行之前重新启动.
从开箱即用运行此代码为我提供了以下输出:
Running method A
Finished method A
Running method B
Finished method B
Running method A
Finished method A …Run Code Online (Sandbox Code Playgroud) 我正在配置计划的任务以在不同的线程中运行。这是配置代码
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是使用它的代码
@Scheduled(fixedRateString = "2000" )
public void testaMethod() {
log.debug("here is the message");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我将线程固定时间为2s的线程休眠了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"}
Run Code Online (Sandbox Code Playgroud)