In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what are the @ Scheduled restrictions in Spring, which can be used for reference. Interested friends can refer to them. I hope you will gain a lot after reading this article.
Spring @ Scheduled restrictions
@ Scheduled has certain restrictions, after all, it is not quartz, just a simple timing, than jdk Timer to join the thread pool
@ Scheduled does not support year timing
@ Scheduled does not support the letters W L
No way. If you have to use it, you have no choice but to abandon the annotations and use XML.
Spring multi-timed task @ Scheduled executes blocking one. Problem description
Recently, I found a problem in the project. I plan to execute a scheduled task at 4:40 every morning, using the annotation method: @ Scheduled (cron = "0404 * *?"). There is obviously nothing wrong with the cron expression, but this scheduled task is always not executed on time, sometimes after 8: 00 and sometimes after 9: 00. Later, I checked that the original timing method is executed by single thread by default. I happen to have multiple scheduled tasks here, and one of the scheduled tasks before 4:40 is quite time-consuming. As a result, the 4:40 task can only be triggered after waiting for the completion of the previous task execution, so you have to manually set the scheduled task to multithreading.
two。 Scene reproduction
Project description: using Springboot for development
Set up two scheduled tasks, execute them every 5s, and print out their execution
The code is as follows:
@ Component@Log4j2public class ScheduledTask {@ Scheduled (cron = "0can5 *?") Public void task1 () throws InterruptedException {log.info ("I am task11111111, current thread: {}", Thread.currentThread ()); while (true) {/ / simulate a time-consuming task, blocking 10s Thread.sleep (10000); break;}} @ Scheduled (cron = "0ram 5 *?") Public void task2 () {log.info ("I am task22222222, current thread: {}", Thread.currentThread ());}}
The implementation results are as follows:
2019-04-24 17 com.example.demo.task.ScheduledTask 11V 15.008 INFO 16868-[scheduling-1] com.example.demo.task.ScheduledTask: I am task22222222, current thread: Thread [scheduling-1 am task22222222 5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 11V 15.009 INFO 16868-[scheduling-1] com.example.demo.task.ScheduledTask: I am task11111111, current thread: Thread [scheduling-1 am task11111111 5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 11V 25.009 INFO 16868-[scheduling-1] com.example.demo.task.ScheduledTask: I am task22222222, current thread: Thread [scheduling-1 am task22222222 5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 1115 30.002 INFO 16868-[scheduling-1] com.example.demo.task.ScheduledTask: I am task22222222, current thread: Thread [scheduling-1 am task22222222 5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 1115 30.003 INFO 16868-[scheduling-1] com.example.demo.task.ScheduledTask: I am task11111111, current thread: Thread [scheduling-1 am task11111111 5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 1115 40.004 INFO 16868-[scheduling-1] com.example.demo.task.ScheduledTask: I am task22222222, current thread: Thread [scheduling-1 am task22222222 5 main]
From the results, it can be seen that task1 and task2 are executed by the same thread Thread, that is, the scheduled task uses a single thread by default, and because the task1 blocks for 10 seconds, the scheduled task that should be executed once in 5 seconds is only executed once in 10 seconds.
three。 Solution
There are a variety of solutions online, two of which are listed below
Scenario 1: use @ Async annotations to implement asynchronous tasks
This method is relatively simple. Add @ Async to the scheduled task. Note: you need to start the class and add @ EnableAsync to take effect.
The code is as follows:
@ Component@Log4j2public class ScheduledTask {@ Async @ Scheduled (cron = "0max 5 *?") Public void task1 () throws InterruptedException {log.info ("I am task11111111, current thread: {}", Thread.currentThread ()); while (true) {/ / simulate a time-consuming task, blocking 10s Thread.sleep (10000); break;}} @ Async @ Scheduled (cron = "0max 5 *?") Public void task2 () {log.info ("I am task22222222, current thread: {}", Thread.currentThread ());}}
Running result:
2019-04-24 17 com.example.demo.task.ScheduledTask 03V 00.024 INFO 2152-[task-1] com.example.demo.task.ScheduledTask: I am task22222222, current thread: thread [task-1 Magi 5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 03V 00.024 INFO 2152-[task-2] com.example.demo.task.ScheduledTask: I am task11111111, current thread: thread [task-2 Magi 5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 03VR 05.001 INFO 2152-[task-3] com.example.demo.task.ScheduledTask: I am task11111111, current thread: thread [task-3meme 5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 03V 05.001 INFO 2152-[task-4] com.example.demo.task.ScheduledTask: I am task22222222, current thread: thread [task-4Magi 5Jing main]
2019-04-24 17 com.example.demo.task.ScheduledTask 03V 10.002 INFO 2152-[task-5] com.example.demo.task.ScheduledTask: I am task22222222, current thread: thread [task-5 Magi 5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 03V 10.003 INFO 2152-[task-6] com.example.demo.task.ScheduledTask: I am task11111111, current thread: thread [task-6 Magi 5 main]
As can be seen from the running log, timing execution every 5 seconds has taken effect, and the thread used for each task is different, that is, multi-thread execution of scheduled tasks is realized, and there is no task waiting phenomenon. It is said that the default thread pool size is 100, which is a little overqualified if there are not many tasks, so I think the second way is better.
Scenario 2: manually set the thread pool size for scheduled tasks
The scheduled task code is partially restored, and the @ Async annotation is not used, and the startup code configuration is added:
@ Configurationpublic class AppConfig implements SchedulingConfigurer {@ Bean public Executor taskExecutor () {/ / specify the number of scheduled task threads, which can be adjusted by return Executors.newScheduledThreadPool (3);} @ Override public void configureTasks (ScheduledTaskRegistrar scheduledTaskRegistrar) {scheduledTaskRegistrar.setScheduler (taskExecutor ());}}
The running results are as follows:
2019-04-24 17 com.example.demo.task.ScheduledTask 26 am task22222222 15.008 INFO 2164-[pool-1-thread-2] Thread: I am task22222222, current thread: Thread [pool-1 Muhammad threadmuri 2jin5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 26 am task11111111 15.008 INFO 2164-[pool-1-thread-1] Thread: I am task11111111, current thread: Thread [pool-1 Muhammad threadmeret 1pm 5main]
2019-04-24 17 com.example.demo.task.ScheduledTask 2620.002 INFO 2164-[pool-1-thread-2] Thread: I am task22222222, current thread: Thread [pool-1 Muhammad threadmuri 2jin5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 2625 25 001 INFO 2164-[pool-1-thread-2] Thread: I am task22222222, current thread: Thread [pool-1 Muhammad threadmer2jin5 main]
2019-04-24 17 com.example.demo.task.ScheduledTask 26 com.example.demo.task.ScheduledTask 30.001 INFO 2164-[pool-1-thread-1] Thread: I am task11111111, current thread: Thread [pool-1 Muhammad threadmeret 1pm 5my main]
2019-04-24 17 com.example.demo.task.ScheduledTask 26 com.example.demo.task.ScheduledTask 30.001 INFO 2164-[pool-1-thread-3] Thread: I am task22222222, current thread: Thread [pool-1 Muhammad threadmeret 3jin5 main]
2019-04-24 17 Swiss 26 INFO 35.001 INFO 2164-[pool-1-thread-3] com.example.demo.task.ScheduledTask: I am task22222222, current thread: Thread [pool-1 Muhammad threadmuri 3jin5 main]
As can be seen from the results, the second method also implements multithreaded task scheduling.
Thank you for reading this article carefully. I hope the article "what are the restrictions on @ Scheduled in Spring" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.