Home  >  Article  >  Backend Development  >  SpringBoot scheduling tasks and common task expressions

SpringBoot scheduling tasks and common task expressions

小云云
小云云Original
2017-12-06 09:31:481649browse

This article mainly introduces SpringBoot scheduling tasks and common task expressions. 1. First, you need to annotate *applicatin.java with @EnableScheduling to detect whether there are scheduled tasks. 2. The @Scheduled annotation is used to mark this method as a scheduled task method. Spring will automatically scan this annotation and start the scheduling task.

package com.david.translate.quartz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.david.translate.service.SysUserService;
@Component
public class TimeQuartz {
  @Autowired
  private SysUserService userService;
  /**
   * 一分钟执行一次
   */
  @Scheduled(cron="0 0/1 * * * ?") 
  public void executeFileDownLoadTask() {
    System.out.println(">>>>>>>>>>>>>>>>>>>任务执行 "+userService.findAll().size());
  }
}

The time configuration of scheduling tasks uses cron expressions. I recommend a website that generates expressions online. If you don’t know how to write or are unwilling to write it yourself, , can be generated directly using this website:

http://cron.qqe2.com/

The screenshot is as follows:

SpringBoot scheduling tasks and common task expressions

Some commonly used task expression examples:

0 * * * * ? Trigger once every 1 minute
0 0 * * * ? Trigger once every 1 hour every day
0 0 10 * * ? Triggered once every day at 10 o'clock
0 * 14 * * ? Triggered every 1 minute from 2 pm to 2:59 pm every day
0 30 9 1 * ? At 9:30 am on the 1st of every month
0 15 10 15 * ? Triggered at 10:15 am on the 15th of every month

/5 * * * ? Executed every 5 seconds
0 /1 * * ? Executed every 1 minute Once
0 0 5-15 * * ? Trigger on the hour from 5 to 15 o'clock every day
0 0/3 * * * ? Trigger once every three minutes
0 0-5 14 * * ? Every afternoon Trigger every 1 minute from 2pm to 2:05pm
0 0/5 14 * * ? Trigger every 5 minutes from 2pm to 2:55pm every day
0 0/5 14,18 * * ? Trigger every 5 minutes between 2pm and 2:55pm and every 5 minutes between 6pm and 6:55pm
0 0/30 9-17 * * ? Every half hour during 9 to 5 working hours
0 0 10,14,16 * * ? Every day at 10 am, 2 pm, 4 pm

0 0 12 ? * WED means every Wednesday at 12 noon
0 0 17 ? * TUES, THUR, SAT every Tuesday, Thursday, and Saturday at 5 pm
0 10,44 14 ? 3 WED Triggered every Wednesday in March at 2:10 and 2:44 pm
0 15 10 ? * MON -FRI triggers at 10:15 am from Monday to Friday

0 0 23 L * ? Execute once at 23:00 on the last day of each month
0 15 10 L * ? 10:00 am on the last day of each month 15 Trigger
0 15 10 ? * 6L Triggered at 10:15 am on the last Friday of each month

0 15 10 * * ? 2005 Triggered at 10:15 am every day in 2005
0 15 10 ? * 6L 2002-2005 Triggered at 10:15 am on the last Friday of each month from 2002 to 2005
0 15 10 ? * 6#3 Triggered at 10:15 am on the third Friday of each month

The above content is SpringBoot scheduling tasks and common task expressions. I hope it can help everyone.

Related recommendations:

Detailed explanation of the causes of Session timeout problem in SpringBoot

Introductory graphic tutorial about SpringBoot in Java

In-depth analysis of how springboot configures multiple redis connections

The above is the detailed content of SpringBoot scheduling tasks and common task expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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