Java spring引导中的并发调度方法
我有一个spring应用程序,它有两个用@Component注释的类,在每个类中,我有一个用@Scheduled注释的方法,这意味着我希望以固定的间隔运行这些方法,如下所示: 这是第一个有readFirstComponent()方法的组件,这个方法从某处读取一些东西,需要一段时间才能执行, @组成部分 公营头等舱{Java spring引导中的并发调度方法,java,spring,scheduled-tasks,Java,Spring,Scheduled Tasks,我有一个spring应用程序,它有两个用@Component注释的类,在每个类中,我有一个用@Scheduled注释的方法,这意味着我希望以固定的间隔运行这些方法,如下所示: 这是第一个有readFirstComponent()方法的组件,这个方法从某处读取一些东西,需要一段时间才能执行, @组成部分 公营头等舱{ @Scheduled(fixedRate = 20000 ) public void readFirstComponent() { // body } //其他方法 } 第
@Scheduled(fixedRate = 20000 )
public void readFirstComponent() {
// body
}
//其他方法
}
第二个组件的功能与第一个组件几乎相同
@Component
公务舱二等舱{
@Scheduled(fixedRate = 20000 )
public void readSecondComponent() {
// body
}
//其他方法
}
我有一个runner类来启动应用程序
@SpringBootApplication
@EnableScheduling
@ImportResource("classpath:spring/Spring-AutoScan.xml")
public class Application {
public static void main(final String args[]) {
SpringApplication.run(Application.class);
}
}
当我启动应用程序时,FirtComp正在启动,readFirstComponent()在将近14秒结束后执行,然后从SecondComp启动readSecondComponent(),依此类推,
我的问题是我想同时启动这两个方法,请帮助我解决这个问题默认情况下只有一个线程运行调度任务 您可以阅读相关内容,了解如何配置调度程序以获得包含更多线程的池 27.4.1启用计划注释 要启用对@Scheduled和@Async注释的支持,请将@EnableScheduling和@EnableSync添加到其中一个@Configuration类中: 您可以自由选择应用程序的相关注释。例如,如果您只需要支持@Scheduled,只需省略@enablesync即可。为了实现更细粒度的控制,您还可以实现SchedulingConfigurer和/或AsyncConfigurer接口。有关详细信息,请参阅javadocs 如果您喜欢XML配置,请使用元素
默认情况下,只有一个线程运行调度任务 您可以阅读相关内容,了解如何配置调度程序以获得包含更多线程的池 27.4.1启用计划注释 要启用对@Scheduled和@Async注释的支持,请将@EnableScheduling和@EnableSync添加到其中一个@Configuration类中: 您可以自由选择应用程序的相关注释。例如,如果您只需要支持@Scheduled,只需省略@enablesync即可。为了实现更细粒度的控制,您还可以实现SchedulingConfigurer和/或AsyncConfigurer接口。有关详细信息,请参阅javadocs 如果您喜欢XML配置,请使用元素
这是一个老问题,但我自己在处理这个问题。我不能使用上面提供的解决方案,因为我需要确保每个方法一次只运行一个实例。增加默认调度线程池的大小意味着如果方法花费的时间超过shcedule间隔,我可能会遇到麻烦 因此,我创建了两个线程池,每个线程包含一个线程,然后注释每个方法以使用相关的线程池(即单线程) 创建线程池:
@SpringBootApplication
@EnableScheduling
@EnableAsync(proxyTargetClass=true)
public class MainApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MainApplication.class);
application.run(args);
}
@Bean("schedulePool1")
public Executor jobPool() {
ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor();
exec.setCorePoolSize(1);
exec.setMaxPoolSize(1);
exec.setQueueCapacity(10);
exec.setThreadNamePrefix("first-");
exec.initialize();
return exec;
}
@Bean("schedulePool2")
public Executor jobPool2() {
ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor();
exec.setCorePoolSize(1);
exec.setMaxPoolSize(1);
exec.setQueueCapacity(10);
exec.setThreadNamePrefix("second-");
exec.initialize();
return exec;
}
}
然后可以将@Async
注释添加到两个计划方法中
@Async("schedulePool1")
@Scheduled(fixedRate = 20000 )
public void readFirstComponent() {
// body
}
及
然后在您的日志中,您应该可以看到正确的[thread]
:
2020-02-21 20:47:01 [first-1] INFO sodved.JobSchedule - running readFirstComponent
...
2020-02-21 20:47:01 [second-1] INFO sodved.JobService - running readSecondComponent
这是一个老问题,但我自己在处理这个问题。我不能使用上面提供的解决方案,因为我需要确保每个方法一次只运行一个实例。增加默认调度线程池的大小意味着如果方法花费的时间超过shcedule间隔,我可能会遇到麻烦 因此,我创建了两个线程池,每个线程包含一个线程,然后注释每个方法以使用相关的线程池(即单线程) 创建线程池:
@SpringBootApplication
@EnableScheduling
@EnableAsync(proxyTargetClass=true)
public class MainApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MainApplication.class);
application.run(args);
}
@Bean("schedulePool1")
public Executor jobPool() {
ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor();
exec.setCorePoolSize(1);
exec.setMaxPoolSize(1);
exec.setQueueCapacity(10);
exec.setThreadNamePrefix("first-");
exec.initialize();
return exec;
}
@Bean("schedulePool2")
public Executor jobPool2() {
ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor();
exec.setCorePoolSize(1);
exec.setMaxPoolSize(1);
exec.setQueueCapacity(10);
exec.setThreadNamePrefix("second-");
exec.initialize();
return exec;
}
}
然后可以将@Async
注释添加到两个计划方法中
@Async("schedulePool1")
@Scheduled(fixedRate = 20000 )
public void readFirstComponent() {
// body
}
及
然后在您的日志中,您应该可以看到正确的[thread]
:
2020-02-21 20:47:01 [first-1] INFO sodved.JobSchedule - running readFirstComponent
...
2020-02-21 20:47:01 [second-1] INFO sodved.JobService - running readSecondComponent
请同时从链接中复制最相关的部分,因为链接往往会被删除。好的,非常感谢您提供的信息。我将提高我的回答技巧。请同时从链接中复制最相关的部分,因为链接往往会被删除。好的,非常感谢您提供的信息。我会提高我的答题技巧。这正是我想要的。谢谢分享!正是我想要的。谢谢分享!
2020-02-21 20:47:01 [first-1] INFO sodved.JobSchedule - running readFirstComponent
...
2020-02-21 20:47:01 [second-1] INFO sodved.JobService - running readSecondComponent