In daily projects, there is always a need to periodically perform an action at a certain time. For example, the report should be exported at a certain time every day, or the number of current online users should be counted every time. The java.util.Timer class is available in Java, the powerful Quartz scheduler, and Scheduled is available in SpringBoot. Scheduled is the focus of today’s discussion.

V Timer comparison

Name of the framework Cron expression Fixed interval execution Fixed frequency execution Task persistence Difficulty level
TimerTask Does not support support support Does not support general
schedule support support support Does not support simple
Quartz support support support support difficult

In practice, schedule works better with Spring if you don’t have distributed scenarios (quartz supports distributed scenarios, schedule doesn’t (you need to implement it yourself, with distributed locks).

V Create the Schedule project

Create helloSchedule using IntelliJ IDEA

Click Finish to complete the project creation.

For demonstration purposes, use @slf4j to export logs, add lombok references, @slf4j can see SpringBoot(8) configure logback logs

Add the Export class.

package com.task.log; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by toutou on 2018/10/20. */ @Component @Slf4j public class export { @Scheduled(cron = "0 0/1 * * * ?" ) public void minuteExport(){log.debug(" minutetask: "+ getDate()); } @scheduled (fixedRate = 5000) public void fiveSecondExport(){log.debug(" every 5 seconds: "+ getDate())); } @Scheduled(cron = "0/2 * * * * ?" ) public void twoSecondExport(){log.debug(" every 2 seconds: "+ getDate()); } @Scheduled(cron = "0 55 14 ? * *") public void regularTimeExport(){log.debug(" + getDate()); } private String getDate(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date()); }}Copy the code

Add the @enablesCheduling annotation to the startup class and run it.

View IntelliJ IDEA console logs and physical file logs

As shown in the figure above, the simple log output setup of scheduled tasks is complete.

Vcron details

Cron expressions have special syntax, and feel a bit convoluted, but in short, you can remember some common usage, special syntax can be looked up separately.

Cron has 7 bits in total, but the last bit is year, so we can leave it blank, so we can write 6 bits:

The first digit is seconds. The value ranges from 0 to 59

The second value is the minute, ranging from 0 to 59

The third digit indicates the hour. The value ranges from 0 to 23

The fourth digit is day/day. The value ranges from 1 to 31

The fifth digit is the date and month. The value ranges from 1 to 12

Sixth place, week, value 1-7, Monday, Tuesday… Week 1, week 2, Week 1, Week 2, Week 1, Week 2, Week 1, Week 2, Week 2

The seventh digit, the year, can be left blank, 1970-2099

There are also some special symbols in cron that have the following meanings:

(*) asterisk: can be understood to mean every second, every minute, every day, every month, every year... (?) Question mark: The question mark can only appear in the two positions of date and week, indicating that the value of this position is uncertain. It is executed at 3 o 'clock every day, so the position of the sixth week, which we do not need to pay attention to, is an uncertain value. Also: Date and week are mutually exclusive elements, indicated by a question mark without specifying a value. For example, January 10, say Monday, would be inconsistent if the location of the week was designated Tuesday. (-) minus: indicates a range. For example, "10-12" in the hour field indicates the range from 10 to 12, i.e. 10,11,12 (,) comma: indicates a list value. For example, "1,2,4" in the week field indicates Monday, Tuesday, Thursday. X /y, x is the starting value, y is the step size, for example, in the first (second) 0/15 is, starting at 0 seconds, every 15 seconds, and ending at 0, 15, 30, 45, 60 and */y is the same as 0/yCopy the code

Schedule:

"0, 0, 12 * *? "0, 15, 10? * * triggers "0 15 10 * *?" at 10:15 am every day. "0 15 10 * *? *" Trigger every day at 10:15 am "0 15 10 * *? 2005 - "0 * 14 * *?" triggered at 10:15 am every day in 2005. Trigger "0/5 14 * *?" every minute between 2pm and 2:59pm every day. Trigger "0/5 14,18 * *?" every 5 minutes between 2pm and 2:55pm every day. Trigger "0-5 14 * *?" every 5 minutes between 2 p.m. and 2:55 p.m. and 6 p.m. to 6:55 p.m. every day. Trigger "0, 10,44, 14? 3 WED" Triggered at 2:10 PM and 2:44 PM on Wednesdays of March every year "0 15 10? * Mon-fri "triggers "0 15 10 15 *?" Monday to Friday at 10:15 am. Trigger "0 15 10 L *?" at 10:15 am on 15th of each month. "0, 15, 10? "triggered at 10:15 a.m. on the last day of each month. * 6L" Last Friday of every month triggered at 10:15 am "0 15 10? * 6L 2002-2005" Last Friday of each month 2002 to 2005 10:15 am Trigger "0 15 10? * 6#3" triggers every 5 seconds on the third Friday of each month at 10:15 am: */5 * * * *? Run the following command every minute: 0 */1 * * *? Run this command at 23:00 every day: 0 0 23 * *? Every day at 1 am: 0 0 1 * *? Run this command at 1 am on the first day of each month: 0 0 1 1 *? Run at 23:00 on the last day of each month: 0 0 23 L *? Every Sunday at 1 am: 0, 0, 1? * LCopy the code

Crontab expression generated online tool.lu/crontab/

Other references:

  • Getting Started · Scheduling Tasks Spring. IO /guides/ GS/S…
  • Advantages of Java-Quartz over SpringBoot Schedule? Segmentfault.com/q/101000001…
  • Spring Boot Quartz Scheduler Example: Building an Email Scheduling app | CalliCoderwww.callicoder.com/spring-boot…
  • SpringBoot trip – timing task two (Spring Schedule with Quartz integration) implementation www.cnblogs.com/javanoob/p/…

V Source code address

Github.com/toutouge/ja…

About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \