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

Springboot uses timer @ scheduled solution that doesn't work

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Springboot uses timer @ scheduled solution. To solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.

Using timer @ scheduled doesn't work.

If it cannot be used in the first place, the @ EnableScheduling annotation is not written. If it is in use, it does not work because @ Scheduled is single-threaded and has a timer working or does not finish running, so the thread is blocked so that the next timer cannot run. Add a method class.

Package com.llt;import org.springframework.boot.autoconfigure.batch.BatchProperties;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;import java.lang.reflect.Method;import java.util.concurrent.Executors;@Configurationpublic class ScheduleConfig implements SchedulingConfigurer {@ Override public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {Method [] methods = BatchProperties.Job.class.getMethods (); int defaultPoolSize = 3 Int corePoolSize = 0; if (methods! = null & & methods.length > 0) {for (Method method: methods) {Scheduled annotation = method.getAnnotation (Scheduled.class); if (annotation! = null) {corePoolSize++ If (defaultPoolSize > corePoolSize) corePoolSize = defaultPoolSize;} taskRegistrar.setScheduler (Executors.newScheduledThreadPool (corePoolSize));}}

Just fine!

Multiple @ Scheduled timers do not execute

The @ Scheduled annotation is often used in recent projects. During the internal test, each thread executes very quickly because of the small amount of data (no stress test). However, after online, it is found that some functions cannot be used, and the last location is that part of the timer is not executed. After consulting the data and Springboot source code,

When ScheduledTaskRegistrar starts, if it does not specify the size of the thread pool, a default thread pool with a core thread number of 1 will be created by default, so when multiple @ scheduled threads appear in the project, they can only be executed one by one. As a result, when individual threads execute for too long (or long-term execution), other timers cannot execute in accordance with the specified rules.

Solution method

1. Specify the size of the execution thread pool for the project during initialization

Import org.springframework.boot.autoconfigure.batch.BatchProperties;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar; import java.lang.reflect.Method;import java.util.Arrays;import java.util.Objects;import java.util.concurrent.ScheduledThreadPoolExecutor;import java.util.concurrent.atomic.AtomicInteger / * Program name: ScheduledTaskConfiguration * Establishment date: 2021-02-23 9:33 * Module: Scheduled task thread pool settings * description: read the @ Scheduled annotation method in the project. By default, all methods need to be executed in accordance with the set rules when the project is created. * remarks: / / TODO *

* modification history * serial number date modifier reason * / @ Configurationpublic class ScheduledTaskConfiguration implements SchedulingConfigurer {@ Override public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {Method [] methods = BatchProperties.Job.class.getMethods (); final AtomicInteger corePoolSize = new AtomicInteger () If (Objects.nonNull (methods) & & methods.length > 0) {Arrays.stream (methods) .forEach (method-> {final Scheduled annotation = method.getAnnotation (Scheduled.class); if (Objects.nonNull (annotation)) {corePoolSize.incrementAndGet ();}}) } ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor (corePoolSize.get ()); taskRegistrar.setScheduler (executor);}}

two。 Set the timer to an asynchronous thread

/ * * Asynchronous thread timer starts 1 second later, and executes every 3 seconds after the last execution is completed * / @ Async ("taskExecutor") @ Scheduled (initialDelay = 1000L, fixedDelay = 3000L) public void test () {System.out.println ("-" + System.currentTimeMillis ()) / / Business content} this is the answer to the problem that springboot uses timer @ scheduled to solve the problem. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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