Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of configuring scheduled tasks and thread pool and multi-thread pool execution in SpringBoot

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

SpringBoot configuration of scheduled tasks and thread pool and multi-thread pool execution method is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Configure basic scheduled tasks

The most basic configuration method, and this configuration timing task is executed in a single thread, that is, only one scheduled task can be executed at a time, you can try to declare two methods and write an endless loop in the method. You will find that you have been stuck on one task and the other has not been executed.

1. Startup class

Add @ EnableScheduling to enable support for scheduled tasks

@ EnableScheduling@SpringBootApplicationpublic class TestScheduledApplication extends SpringBootServletInitializer {@ Override protected SpringApplicationBuilder configure (SpringApplicationBuilder builder) {return builder.sources (this.getClass ());} public static void main (String [] args) {new SpringApplicationBuilder (TestScheduledApplication.class) .web (true) .run (args);}}

2. Configure classes that perform scheduled tasks

Add @ Component to scan this class, add @ Scheduled annotation to the method to declare timing tasks, and configure time periods

@ Componentpublic class TestTask1 {private static final Logger logger = LogManager.getLogger (); / / execute @ Scheduled once every 1 second (cron = "0can1 *?") Public void method1 () {logger.info ("- method1 start-"); logger.info ("- method1 end-");}}

Configure the thread pool to perform scheduled tasks

Because sometimes there are a lot of scheduled tasks that need to be executed, serial execution will bring some problems, such as a time-consuming task is blocked, and some tasks that need to be executed in a short cycle will be stuck, so you can configure a thread pool to execute scheduled tasks in parallel.

1. Configure thread pool

Add @ EnableAsync to enable support for async

Configuration@EnableAsyncpublic class ExecutorConfig {@ Bean public Executor executor1 () {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor (); executor.setThreadNamePrefix ("test-schedule2-"); executor.setMaxPoolSize (20); executor.setCorePoolSize (15); executor.setQueueCapacity (0); executor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ()); return executor;}}

2. Configure timing tasks to execute asynchronously

Add @ Async annotation to indicate that the timed task is executed asynchronously. Because the thread pool is configured with the name above, you can see that the printed log is that the threads in the thread pool are executing the task. If no thread pool is configured, SimpleAsyncTaskExecutor will be used by default. This asynchronous executor will open a child thread for execution each time, which consumes a lot of performance, so it is best to configure the thread pool yourself.

@ Async@Scheduled (cron = "0ram 1 *?") public void method1 () {logger.info ("- method1 start-"); logger.info ("- method1 end-");}

Configure multiple thread pools to perform different scheduled tasks

Because some scheduled tasks are more important and some are less important, it is also possible to put scheduled tasks in different thread pools.

1. Configure multiple thread pools

Configure two thread pools respectively

@ Configuration@EnableAsyncpublic class ExecutorConfig1 {@ Bean public Executor executor1 () {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor (); executor.setThreadNamePrefix ("test-schedule1-"); executor.setMaxPoolSize (20); executor.setCorePoolSize (15); executor.setQueueCapacity (0); executor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ()); return executor;} @ Bean public Executor executor2 () {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor (); executor.setThreadNamePrefix ("test-schedule2-"); executor.setMaxPoolSize (20); executor.setCorePoolSize (15); executor.setQueueCapacity (0) Executor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ()); return executor;}

2. Scheduled tasks display the specified calling thread pool

Because two thread pools are initialized in the configuration class above, two thread pools called executor1 and executor1 will be generated into the container, because the object generated by the @ Bean annotation is the same as the method name by default, while the @ Async annotation can specify which thread pool to use. This allows you to perform different scheduled tasks in different thread pools

/ / execute @ Async ("executor1") @ Scheduled (cron = "0go 1 *?") public void method1 () {logger.info ("- method1 start-"); logger.info ("- method1 end-") once every 1 second } / / execute @ Scheduled (cron = "0go 1 *?") @ Async ("executor2") public void method2 () {logger.info ("- method2 start-"); logger.info ("- method2 end-") once every 1 second;}

Note:

SimpleAsyncTaskExecutor is used by default when you do not configure your own thread pool. If only one thread pool is configured in the project, there is no need to display the specified thread pool, and spring will automatically use the user-configured thread pool, but if multiple thread pools are configured, the specified thread pool must be displayed, otherwise the default will be used. If you want to specify which thread pool to use, you can use @ Async ("executor2") to display the assignment.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report