Does spring @Scheduled annotated methods runs on different threads?

前端 未结 8 2238
时光取名叫无心
时光取名叫无心 2020-11-30 01:19

I have several methods annotated with @Scheduled(fixedDelay=10000).

In the application context, I have this annotation-driven setup:

<         


        
相关标签:
8条回答
  • 2020-11-30 01:36

    default spring using a single thread for schedule task. you can using @Configuration for class implements SchedulingConfigurer . referce: https://crmepham.github.io/spring-boot-multi-thread-scheduling/

    0 讨论(0)
  • 2020-11-30 01:38

    The @EnableScheduling annotation provides the key information and how to resolve it:

    By default, will be searching for an associated scheduler definition: either a unique TaskScheduler bean in the context, or a TaskScheduler bean named "taskScheduler" otherwise; the same lookup will also be performed for a ScheduledExecutorService bean. If neither of the two is resolvable, a local single-threaded default scheduler will be created and used within the registrar.

    When more control is desired, a @Configuration class may implement SchedulingConfigurer. This allows access to the underlying ScheduledTaskRegistrar instance. For example, the following example demonstrates how to customize the Executor used to execute scheduled tasks:

     @Configuration
     @EnableScheduling
     public class AppConfig implements SchedulingConfigurer {
    
         @Override
         public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
             taskRegistrar.setScheduler(taskExecutor());
         }
    
         @Bean(destroyMethod="shutdown")
         public Executor taskExecutor() {
             return Executors.newScheduledThreadPool(100);
         }
     }
    

    (emphasis added)

    0 讨论(0)
提交回复
热议问题