Spring Boot uses @Scheduled to implement timer tasks

First, we need to build a SpringBoot-based project, and then we need to enable the timer task function @EnableScheduling in Application.

Start a scheduled task

package com.ltf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class MakeaFortuneApplication {

	public static void main(String[] args) {
		SpringApplication.run(MakeaFortuneApplication.class, args);
	}

}

Among them, the function of @EnableScheduling annotation is to find the task of annotation @Scheduled and execute it in the background.

Timing task specific implementation class

Next we will create a timed task

package com.ltf.scheduler;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ltf.dao.UserDao;

/**
 * 系统定时任务类
 * @author xhz
 *
 */
@Component
public class Scheduler {

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

	@Autowired
	private UserDao userDao;

	/**
	 * 每隔5秒执行一次
	 */
	@Scheduled(fixedRate = 5000)
	public void testTask1() {
		System.out.println("定时任务1执行时间:" + dateFormat.format(new Date()));
	}

	/**
	 * 每天15:54执行
	 */
	@Scheduled(cron = "0 54 15 ? * *")
	public void testTask2() {
		System.out.println("定时任务2执行时间:" + dateFormat.format(new Date()));
	}

	/**
	 * 每天凌晨执行一次
	 */
	@Scheduled(cron = "0 0 0 * * ?")
	public void updateSignInState() {
		userDao.updateSignInStateForUser();
		System.out.println("【修改用户签到状态】定时任务执行时间:" + dateFormat.format(new Date()));
	}

}

Run Spring Boot, the output is as follows, print out the current time every 5 seconds.

Note: You  need to add a comment: @Component to the class of the timed task, and add the annotation @Scheduled to the specific timed task method to start the timed task.

@Scheduled parameter description

@Scheduled(fixedRate=5000) : execute again 5 seconds after the last execution time ;

@Scheduled(fixedDelay=5000) : execute again at 5 seconds after the last execution;

@Scheduled(initialDelay=1000, fixedDelay=5000) : The first execution is delayed by 1 second , and then executed again at 5 seconds when the last execution is completed ;

@Scheduled(cron="* * * * * ?")Execute according to cron rules;

CronTrigger

CronTriggers are often more useful than SimpleTrigger, if you need a calendar-based concept instead of SimpleTrigger's fully specified time interval, recurring launch schedule. CronTrigger, you can specify the trigger schedule such as "every Friday at noon", or "every working day at 9:30", or even "every 5 minutes at 9:00 and 10:00 every Monday morning, Wednesday and Friday" . Even so, like SimpleTrigger, CronTrigger owns the startTime when the timetable specified takes effect, and when the timetable is specified, it should stop (optional) at the end time.

Cron expression

1) The cron expression is used to configure the CronTrigger instance. The expression of cron is a string, which is actually composed of seven sub-expressions, describing the schedule of individual details. These sub-expressions are separated blanks and represent:

1. Seconds

2. Minutes

3. Hours

4. Day-of-Month

5. Month

6. Day-of-Week

7. Year (optional field)

For example, "0 0 12? * WED" is executed at 12:00 PM every Wednesday,

Individual sub-expressions can include ranges, for example, in the previous example ("WED") can be replaced with "MON-FRI", "MON, WED, FRI" or even "MON-WED,SAT". "*" means the whole period.

Each field has a set of valid values ​​that can be specified, such as

Seconds (seconds): can be represented by numbers 0-59,

Minutes (minutes): can be represented by numbers 0-59,

Hours (hour): can be represented by numbers 0-23,

Day-of-Month (day): You can use any of the numbers 1-31, but pay attention to some special months

Month (month): It can be represented by 0-11 or the string "JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC"

Day-of-Week (every week): It can be represented by numbers 1-7 (1 = Sunday) or by character string "SUN, MON, TUE, WED, THU, FRI and SAT"

"/": It is a special unit, expressed as "every" such as "0/15" means it will be executed every 15 minutes, "0" means starting from "0" minute, and "3/20" means every 20 minutes Execute once, "3" means to execute from the 3rd minute

"?": Indicates a certain day of the month or a certain day of the week

"L": Used for every month, or every week, expressed as the last day of the month, or the last day of the month. For example, "6L" means "the last Friday of the month"

"W": means the most recent working day, such as "15W" placed in the month (day-of-month) field means "the nearest working day to the 15th of this month"

""#": It is used to specify "of" the nth working day of each month. For example, the content of "6#3" or "FRI#3" in the field of "day-of-week" means " Every third Friday"

 2) Cron expression example:

Execute once every 5 seconds: */5 * * * *?

Execute once every 1 minute: 0 */1 * * *?

Execute once every day at 23:00: 0 0 23 * *?

Execute once every day at 1:00 AM: 0 0 1 * *?

Executed at 1:00 am on the 1st of every month: 0 0 1 1 *?

Executed at 23:00 on the last day of each month: 0 0 23 L *?

Executed once a week at 1:00 AM on Sunday: 0 0 1? * L

Perform once at 26, 29, and 33 points: 0 26,29,33 * * *?

Every day at 0 o'clock, 13 o'clock, 18 o'clock, and 21 o'clock are executed once: 0 0 0,13,18,21 * *?

 

Guess you like

Origin blog.csdn.net/qq_35393693/article/details/104879882