SpringBoot used @Scheduled create scheduled tasks

SpringBoot used @Scheduled create scheduled tasks

Regular tasks typically will be used in many projects, we are often intermittent to complete certain tasks to reduce pressure on the server and the database. More common is the financial services system to push the callback, the general payment system orders will be sustained pullback in the absence of receipt of a successful callback to return the contents of this callback are generally timed task to complete. There is a report, we will normally be too small customer visits time to complete this operation, it is often in the early morning. Then we can also be used to complete the task timed logic. SpringBoot our built-timed task, we need only one comment @Scheduled can enable the scheduled task.

First, the addition of dependence in the pom.xml file

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Second, add annotations created on startup class @SpringBootApplicationand@EnableScheduling

@SpringBootApplication
@EnableCaching
@EnableScheduling
public class SpringApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringApplication.class, args);

    }
}

Third, create a class ScheduledTaskthat demonstrates the regular tasks

Annotations using @scheduled scheduled task to demonstrate a manner
We @scheduled annotation to configure the method up to the completion of the timing corresponding to the configuration tasks, such as performing time interval, delay time, etc., here we look in detail corresponding configuration attributes.
@Component This annotation can automatically load the class came when the program starts

fixedDelay property

Performed every 3000ms after the property is a program starts

@Component
public class ScheduledTask {

    @Autowired
    public QuartzJobService quartzJobService;

    private static int count1=1;
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Scheduled(fixedDelay = 3000)
    public void fixedDelay() {
        System.out.println(String.format("第%s次执行,当前时间为:%s", count1++, dateFormat.format(new Date(System.currentTimeMillis()))));
    }

}

cron property

This is a time expression, you can configure a variety of time can be done by a simple configuration, we can do almost any time through CRON expression match, it contains six or seven fields:
Seconds The fast-: there may be ", - * / "four characters, valid integer in the range of 0-59
Minutes: there may be" - * / "four characters, valid integer ranging from 0 to 59
Hours: there may be" - * / "four characters effective integer in the range of 0-23
dayofMonth: there may be ", - * / LW C? " eight characters, valid integer in the range 0-31
Month: there may be "- * /" four characters, the effective range 1-12 integer or DEC-JAN
DayofWeek: there may be ", - * / LC #? " four characters, valid integer in the range or two ranges of 1-7 SUN-SAT. 1 for Sunday, 2 for Monday, and so on
Year: there may be "- * /" four characters, the effective range of 1970- 2099

In a recommended line Cron Expression Builder http://cron.qqe2.com/

A few simple examples

"0012 * *?" Every day noon trigger
"01510? * " At 10:15 every morning trigger
"01510
*?" 10:15 every morning trigger
"01510 * *? " Every morning 10:15 triggering
"01510
*? 2005" 2005 10:15 every morning trigger
"0 * 14 * *?" every day from 14:00 to 2:59 start trigger once every minute
"14 0/5 0 * *? "every day from 14:00 to 2:55 o'clock the start trigger once every 5 minutes
" 0 0/5 * 14, 18 *? "the daily 14:00 to 2:55 and 6:00 to 6:55 two trigger time period once every 5 minutes
"? 00-514 * *" every day from 14:00 to 14:05 the trigger once every minute
"0 10,44 14 3 WED?" Wednesday March 14: 10 and 14:44 triggering
"0 15 10? * MON- FRI" every Monday, Tuesday, Wednesday, Thursday, Friday triggered 10:15

fixedRate property

Meaning the property is on a delayed start calling again after the call (the call is completed without waiting for the last time), so it will repeat the problem, it is not recommended to use, but the amount of time between when the data if it was not configured It can be executed within the finish can also be used.

@Scheduled(fixedRate = 1000)
    public void fixedRate() throws InterruptedException {
        Thread.sleep(2000);
        System.out.println(String.format("1第%s次执行,当前时间为:%s", count1++, dateFormat.format(new Date(System.currentTimeMillis()))));
    }
initialDelay property

This property is the first execution delay

 @Scheduled(initialDelay=1000,fixedDelay = 3000)
    public void initialDelay() {
        System.out.println(String.format("1第%s次执行,当前时间为:%s", count1++, dateFormat.format(new Date(System.currentTimeMillis()))));
    }

Note: All the above values of the time units are arranged in milliseconds

Guess you like

Origin www.cnblogs.com/mengw/p/11564338.html