Timing task: create a static regular tasks, dynamically scheduled task

There are three scheduled task to achieve
. 1 jdk own timer task
2 Quartz plug scheduled task to achieve, need to introduce additional packet
3 SpringTask the timing schedule is once again in the jdk package, without introducing other package, with the package to self spring with
SpringTask there are three ways to achieve timing task

1 comment realization (Static Timing task)

3 annotated by
@Scheduled: arranged on a specific task of the timing methods and configuration time expression
@EnableScheduling: disposed on the boot class is the class SpringBootApplication
@Component: disposed on the specific timing task class, the class register as a component

Implementation:
1.1 annotated @EnableScheduling on the start of class SpringBootApplication
1.2 to create a scheduled task class

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
//将该定时任务类注册为一个组件
@Component
public class MyTask {
//cron = "0/5 * * * * ? "是时间表达式
//@Scheduled5秒执行一次taskTest方法
@Scheduled(cron = "0/5 * * * * ? ")
public void taskTest(){
    System.out.println(new Date());
}
}

Results of the
Here Insert Picture Description

2 xml achieved (static regular tasks, is to replace the notes in the xml tag)

Dynamically add timer task 3

Add dynamic task scheduling mechanism using the thread pool of
3.1 dynamically add annotations is not in use, if any, the following three notes on the comments, if not directly configure dynamic timed task
@Scheduled
@EnableScheduling
@Component

3.2

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

//将该类注册为组件
@Component
public class MyTaskUtil {
//注入线程池的调度任务类
@Autowired
ThreadPoolTaskScheduler threadPoolTaskScheduler;
//创建一个时间表达式方法
public void addTask(String cron){
    //用触发器new一个时间表达式,创建的构造方法的入参就是时间表达式
    Trigger trigger=new CronTrigger(cron);
    //将触发任务放在线程池的任务调度里(第二个参数),第一个参数是定时任务要执行的目标类
    threadPoolTaskScheduler.schedule(new TaskRunnable(),trigger );
}
}

3.3
Create a class (the class is the time to perform the task really) multi-threaded Runnable interface, and override the run method

import java.util.Date;

public class TaskRunnable implements Runnable{
@Override
public void run(){
    System.out.println(new Date());
}
}

3.4

controller里

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyTask {
//注入工具类,就是定时任务的类
@Autowired
MyTaskUtil taskUtil;
@RequestMapping("/addTask")
//传入参数,与下一行的时间表达式做拼接,参数传入几就是几秒执行一次目标任务
//threadPoolTaskScheduler实例调用schedule方法就是创建执行任务
public void addTask(String cron){
    String s="0/"+cron+" * * * * ? ";
    //调用执行定时任务的方法
    taskUtil.addTask(s);
}
}

Request Address: http://127.0.0.1:8080/addTask?cron=5

Here Insert Picture Description
Scheduled tasks can be created simultaneously, in the case of the requested task is not stopped
http://127.0.0.1:8080/addTask?cron=3 is then created a timed task
the step of creating a dynamic timer task
1 Create a thread pool task scheduling bean

@Autowired
ThreadPoolTaskScheduler threadPoolTaskScheduler;

2 to implement a multi-threaded interface (the Runnable), and override the run method is performed as a target

public class TaskRunnable implements Runnable{
@Override
public void run(){
    System.out.println(new Date());
}
}

3 Create a timed trigger trigger

Trigger trigger=new CronTrigger(cron);

4 task scheduler tasks: trigger, a timing goal

threadPoolTaskScheduler.schedule(new TaskRunnable(),trigger );

Static timer, the timer using different dynamic scenes, not good or bad

cron expression , its structure is:
from left to right (separated by spaces): Date year month day hour minute second week of the month

Common examples of expression

每隔5秒执行一次:*/5 * * * * ?
每隔1分钟执行一次:0 */1 * * * ?
每天23点执行一次:0 0 23 * * ?
每天凌晨1点执行一次:0 0 1 * * ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * * * ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?

http://qqe2.com/cron/index online expression

Guess you like

Origin blog.csdn.net/qq_41767337/article/details/89400854