In actual development, we will more or less use some scheduled task scenarios. This article will talk about commonly used scheduled tasks.
Commonly used timing task implementation solutions include the following:
Timer
: This is the java.util.Timer class that comes with java. This class allows you to schedule a java.util.TimerTask
task. Using this method allows your program to be executed at a certain frequency, but not at a specified time. Generally used less. ScheduledExecutorService
: It is also a class that comes with jdk; it is a scheduled task class based on the thread pool design. Each scheduled task will be assigned to a thread pool Threads are executed, that is to say, tasks are executed concurrently and do not affect each other. Spring Task
:Spring3.0
For future tasks, you can regard it as a lightweight Quartz and use It's much simpler than Quartz. Quartz
: This is a relatively powerful scheduler that allows your program to be executed at a specified time or at a certain frequency. The configuration is a bit complicated. This is currently rarely used in projects, and the demo code is posted directly.
For a detailed introduction, please view the API:
public class TestTimer { public static void main(String[] args) { TimerTask timerTask = new TimerTask() { @Override public void run() { System.out.println("task run:"+ new Date()); } }; Timer timer = new Timer(); //安排指定的任务在指定的时间开始进行重复的固定延迟执行。这里是每3秒执行一次 timer.schedule(timerTask,10,3000); } }
This method is similar to Timer, just look at the demo:
public class TestScheduledExecutorService { public static void main(String[] args) { ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); // 参数:1、任务体 2、首次执行的延时时间 // 3、任务执行间隔 4、间隔时间单位 service.scheduleAtFixedRate(()->System.out.println("task ScheduledExecutorService "+new Date()), 0, 3, TimeUnit.SECONDS); } }
In the Spring Boot project, we can use annotations to implement scheduled tasks very elegantly. First, create the project and import the dependencies:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
Create a task class:
@Slf4j @Component public class ScheduledService { @Scheduled(cron = "0/5 * * * * *") public void scheduled(){ log.info("=====>>>>>使用cron {}",System.currentTimeMillis()); } @Scheduled(fixedRate = 5000) public void scheduled1() { log.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis()); } @Scheduled(fixedDelay = 5000) public void scheduled2() { log.info("=====>>>>>fixedDelay{}",System.currentTimeMillis()); } }
Use the @EnableScheduling annotation on the main class to enable support for scheduled tasks, and then start the project
You can see When all three scheduled tasks have been executed, and they are executed serially in the same thread, if there is only one scheduled task, this will definitely be fine. When the number of scheduled tasks increases, if one task is stuck, other tasks will not be able to execute.
In traditional Spring projects, we can add task configuration in the xml configuration file, while in Spring Boot projects we generally use the config configuration class to add configuration, so Create a new AsyncConfig
class
@Configuration @EnableAsync public class AsyncConfig { /* 此处成员变量应该使用@Value从配置中读取 */ private int corePoolSize = 10; private int maxPoolSize = 200; private int queueCapacity = 10; @Bean public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.initialize(); return executor; } }
@Configuration
: Indicates that this class is a configuration class@EnableAsync
: Enables support for asynchronous events
Then add @Async
to the class or method of the scheduled task. Finally restart the project, each task is in a different thread.
In the above scheduled task, we use the @Scheduled annotation on the method to set the execution time of the task, and use three A property configuration method:
initialDelay
to define the delayed execution time of the task. A cron expression has at least 6 (possibly 7) space-separated time elements. In order:
The "*" characters represent all possible values The "/" character is used to specify the increment of the value
For example: "0/15" in the subexpression (minutes) means starting from minute 0, every 15 minutes "3/20" in the subexpression (minutes) means that starting from the 3rd minute, every 20 minutes (it has the same meaning as "3, 23, 43")
"?" The character is only used in the two sub-expressions of day (month) and day (week), indicating that no value is specified. When one of the two subexpressions is assigned a value, in order to avoid conflicts, the value of the other subexpression needs to be set to "?"
The "L" character is only used for days (months) and day (week) are two subexpressions, which are the abbreviation of the word "last" If there is something specific before the "L", it has other meanings.
For example: "6L" means the sixth to last day of this month Note: When using the "L" parameter, do not specify a list or range, as this will cause problems
The W character represents weekdays (Mon-Fri) and can only be used in the day domain. It is used to specify the closest weekday to the specified date. Most business processing is based on the work week, so the W character can be very important.
For example, 15W in the day field means "the closest weekday to the 15th of the month." If the 15th is a Saturday, then the trigger will be triggered on the 14th (Friday), because Thursday is smaller than Monday is closer to the 15th.
C: stands for "Calendar". It means the date associated with the schedule, or all dates in the calendar if the date is not associated.
For example, 5C in the date field is equivalent to the first day after the 5th in the calendar. 1C in the day of the week field corresponds to the first day after Sunday.
Fields | Allowed values | Allowed special characters |
---|---|---|
0~59 | , - * / | |
0~59 | , - * / | |
0~23 | , - * / | # #Date |
, - * ? / L W C | ##month | |
, - * / | weekday | |
, - * ? / L C | #Year (optional) | |
, - * / | 在线cron表达式生成:http://qqe2.com/cron/index 整合Quartz
如果Spring Boot版本是2.0.0以后的,则在spring-boot-starter中已经包含了quart的依赖,则可以直接使用 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> Copy after login 如果是1.5.9则要使用以下添加依赖: <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> Copy after login 这里我使用Spring Boot版本是
public class TestQuartz extends QuartzJobBean { /** * 执行定时任务 * @param jobExecutionContext * @throws JobExecutionException */ @Override protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("quartz task "+new Date()); } } Copy after login
@Configuration public class QuartzConfig { @Bean public JobDetail teatQuartzDetail(){ return JobBuilder.newJob(TestQuartz.class).withIdentity("testQuartz").storeDurably().build(); } @Bean public Trigger testQuartzTrigger(){ SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) //设置时间周期单位秒 .repeatForever(); return TriggerBuilder.newTrigger().forJob(teatQuartzDetail()) .withIdentity("testQuartz") .withSchedule(scheduleBuilder) .build(); } } Copy after login
最后上面都是简单的介绍了关于Spring Boot定时任务的处理,直接使用SpringTask注解的方式应该是最方便的,而使用Quartz从2.0开始也变得很方便。对于这两种方式,应该说各有长处吧,按需选择。 The above is the detailed content of How to implement several scheduled tasks in Spring Boot. For more information, please follow other related articles on the PHP Chinese website!
Related labels:
source:Java后端技术全栈
Previous article:SpringBoot+Dubbo+Nacos development practical tutorial
Next article:Spring Boot implements MySQL read-write separation technology
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|