current position:Home>Yyds dry goods counting springboot timed task framework - quartz

Yyds dry goods counting springboot timed task framework - quartz

2022-01-27 03:58:32 Chasing dreams_ zx

Timing task framework : Quartz Timed task scheduling framework ,Quartz Is to schedule scheduled tasks , Set the trigger time rule , And the corresponding tasks (job) that will do .


Quartz Simple use

Import dependence

       
       
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

1、 Create a task class , Let it be the realization of Job Interface , Natural rewriting execute Method


       
       
public class JobStudent implements Job {
@Override
public void execute(JobExecutionContext Context) throws JobExecutionException {
System.out.println(" This is a hello word Mission ");
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.



2、 Create a QuartzzStudent class , And create a start method , In the start method , Create scheduler object . And triggers ,

Then encapsulate the tasks written above , Then add the task and trigger to the scheduler .

       
       
public class QuartzzStudent {
public static void main(String[] args) throws SchedulerException {
//quartzz API
// 1、 Scheduler Schedler
Scheduler scanner= StdSchedulerFactory.getDefaultScheduler();


// 2、 trigger : Describe how the task triggers
SimpleTrigger trigger=TriggerBuilder.newTrigger().withIdentity("trigger1","group1")
.startNow() // Starting time From the beginning of the program
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).withRepeatCount(10)) // Every two seconds At most 10 Time However, it will actually print 11 Time , Because the startup will print once , Not in here
.endAt(new GregorianCalendar(2021,1,8,22,7,30).getTime()) // End time
.build();


//3、JobDetail
JobDetail jobDetail=JobBuilder.newJob(JobStudent.class).withIdentity("job1","group1").build(); // Notice the group It's isolated from the one above , Each is each .


//4、 take JobDetail And triggers are added to the scheduler
scanner.scheduleJob(jobDetail,trigger);

//5、 Start the scheduler
scanner.start();
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.



The core statement

Scheduler : Scheduler All scheduling is controlled by him , yes QuartZ Brain . All tasks are managed by him ,

Job : Mission Things you want to do regularly ( Define business logic )

JobDetail : be based on job Further packing , One of them is related to job And for job Specify more detailed properties , Such as logo

Trigger: trigger . Can be assigned to a task , Specify the trigger time of the task , That is, its implementation rules


quartz Configuration of Although the following are quartz Default configuration data , But at least we know what's what


       
       
# Specify the scheduler name , Non implementation class
org.quartz.scheduler.instanceName=DefaultQuartzScheduler
# Specifies the thread implementation class
org.quartz.threadPoll.class=org.quartz.simpl.SimpleThreadPool
# The number of threads in the thread pool
org.quartz.threadPoll.threadCount=10
# priority , Default 5
org.quartz.threadPoll.threadPriority=5
# non-persistent job
org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.


Learn triggers

Two triggers

SimpleTrigger A task performed at regular intervals

Specify the start and end time ( Period of time )

Specify the time interval 、 Number of executions

Example :

       
       
SimpleTrigger trigger=TriggerBuilder.newTrigger().withIdentity("trigger1","group1")
.startNow() // Starting time From the beginning of the program
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).withRepeatCount(10)) // Every two seconds At most 10 Time
.endAt(new GregorianCalendar(2021,1,8,22,7,30).getTime()) // End time
.build();


// trigger 2
SimpleTrigger trigger1=TriggerBuilder.newTrigger().withIdentity("trigger2","group2")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(10).withRepeatCount(15)) // Every time 10 Once per minute , At most 15 Time
.endAt(new GregorianCalendar(2021,1,8,22,7,30).getTime())
.build();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.



Cron expression

For more complex tasks . He supports the type of LinuxCron The grammar of ,

Easy to use :

       
       
public class QuartzzCron {

public static void main(String[] args) throws SchedulerException {

//quartzz API
// 1、 Scheduler Schedler
Scheduler scanner= StdSchedulerFactory.getDefaultScheduler();

// 2、 trigger : Describe how the task triggers
Trigger trigger=TriggerBuilder.newTrigger().withIdentity("trigger","group1")
// There is no need to specify the start and end times , Just one cron The expression is just , cron expression from second branch Hours Japan month week

.withSchedule(CronScheduleBuilder.cronSchedule("*/2 53 15 7 1 ? ")) // use ? Represents uncertainty

// It means 5 month 9 Japan 16 spot 5 branch 10 Once per second If you want to every hour , Or every minute . Then use the device part * Instead of just " * 50 15 7 1 ?"

// If you want to do it every two seconds , So write it like this “*/2 50 15 7 1 ?"
.build();

//3、JobDetail
JobDetail jobDetail=JobBuilder.newJob(JobStudent.class).withIdentity("job1","group1").build(); // Notice the group It's isolated from the one above , Each is each .


//4、 take JobDetail And triggers are added to the scheduler

scanner.scheduleJob(jobDetail,trigger);

//5、 start-up
scanner.start();
}


}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.


Cron The composition of the expression

Expressions make up Second minute hour day month week 【 year 】 Years are optional , In general, we do not specify

Such as : "10 20 18 3 5 ?“ Represents the 5 month 3 Japan 18 spot 20 branch 10 second . The day of the week is uncertain

Location

Time domain

permitted

Special values

1

second

0-59

, - * /

2

branch

0-59

, - * /

3

Hours

0-23

, - * /

4

date

1-31

, - * /?LW

5

month

1-12

, - * /

6

week

1-7 ( This 1 It represents Sunday 2 It's Monday )

, - * /?L#

7

year

, - * /


Special value description :

(1)*: Represents any value that matches the field . If be in, Minutes Domain use *, It means that the event will be triggered every minute .

  (2)?: Can only be used in DayofMonth and DayofWeek Two domain . It also matches any value of the field , But it doesn't . because DayofMonth and DayofWeek Will affect each other . For example, I want to be in the 20 Daily trigger dispatch , No matter 20 What day is the day of the week , You can only write it as follows : 13 13 15 20 * ?, The last one can only use ?, They can't be used *, If you use * No matter what day of the week it will trigger , It's not like that .

  (3)-: Scope of representation . For example, in Minutes Domain use 5-20, From 5 be assigned to 20 Trigger every minute  

  (4)/: Indicates that the start time starts to trigger , Then trigger every fixed time . For example, in Minutes Domain use 5/20, Means that the 5 Minutes to trigger , and 25,45 Wait for each trigger . 

  (5),: Indicates that enumeration values are listed . for example : stay Minutes Domain use 5,20, It means in 5 and 20 Minutes per minute . 

  (6)L: Said the last , Can only appear in DayofWeek and DayofMonth Domain . If in DayofWeek Domain use 5L, Means trigger on the last Thursday . 

  (7)W: Means effective working day ( Monday to Friday ), Can only appear in DayofMonth Domain , The system will trigger the event... On the most effective working day from the specified date . for example : stay DayofMonth Use 5W, If 5 It's Saturday , On the most recent working day : Friday , namely 4 Day trigger . If 5 Sunday is Sunday , It's in 6 Japan ( Monday ) Trigger ; If 5 A day from Monday to Friday , It's in 5 Day trigger . Another point ,W Our latest search will not cross the month .

  (8)LW: These two characters can be used together , On the last working day of a month , The last Friday . 

  (9)#: Used to determine the week ordinal of each month , Can only appear in DayofWeek Domain . For example, in 4#2, The second Wednesday of a month .


Common Columns

       
       
(0)0/20 * * * * ? each 20 second Adjust tasks
  (1)0 0 2 1 * ? In the month of 1 Early in the morning 2 Adjust the task
  (2)0 15 10 ? * MON-FRI From Monday to Friday every morning 10:15 Perform operation
  (3)0 15 10 ? 6L 2002-2006 Express 2002-2006 Last Friday morning of each month of the year 10:15 Executive work
  (4)0 0 10,14,16 * * ? Every morning 10 spot , Afternoon 2 spot ,4 spot
  (5)0 0/30 9-17 * * ? Every half hour during 9 to 5 working hours
  (6)0 0 12 ? * WED Every Wednesday at noon 12 spot
  (7)0 0 12 * * ? Every day at noon 12 Some trigger
  (8)0 15 10 ? * * Every morning 10:15 Trigger
  (9)0 15 10 * * ? Every morning 10:15 Trigger
  (10)0 15 10 * * ? * Every morning 10:15 Trigger
  (11)0 15 10 * * ? 2005 2005 Every morning in 1986 10:15 Trigger
  (12)0 * 14 * * ? Every afternoon 2 Point to the afternoon 2:59 During each of the 1 Minutes to trigger
  (13)0 0/5 14 * * ? Every afternoon 2 Point to the afternoon 2:55 During each of the 5 Minutes to trigger
  (14)0 0/5 14,18 * * ? Every afternoon 2 Point to 2:55 During and in the afternoon 6 Point to 6:55 During each of the 5 Minutes to trigger
  (15)0 0-5 14 * * ? Every afternoon 2 Point to the afternoon 2:05 During each of the 1 Minutes to trigger
  (16)0 10,44 14 ? 3 WED Wednesday afternoon in March every year 2:10 and 2:44 Trigger
  (17)0 15 10 ? * MON-FRI Monday to Friday morning 10:15 Trigger
  (18)0 15 10 15 * ? monthly 15 The morning of 10:15 Trigger
  (19)0 15 10 L * ? The morning of the last day of each month 10:15 Trigger
  (20)0 15 10 ? * 6L Last Friday morning of the month 10:15 Trigger
  (21)0 15 10 ? * 6L 2002-2005 2002 - 2005 Last Friday morning of every month in 2006 10:15 Trigger
  (22)0 15 10 ? * 6#3 The third Friday morning of each month 10:15 Trigger
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.








Spring Integrate Quartz


Import dependence

       
       
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.16.RELEASE</version>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.


First create a package , Create a task class in the package , Then create another Quartz.xml Configuration of spring The configuration file .

       
       
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Quartzz The configuration file -->

<!-- spring Integrate Quartz The following steps need to be followed for configuration -->
<!-- 1、 Defining tasks Job-->
<!-- 2、 Defining triggers trigger And bind the trigger to the work task -->
<!-- 3、 Define the scheduler , And will trigger Sign up to scheduler-->


<!-- Defining tasks bean-->
<bean name="lxJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- Appoint job The name of -->
<property name="name" value="job1"/>
<!-- Appoint job The grouping -->
<property name="group" value="job_group1"/>
<!-- Specify specific job class -->
<property name="jobClass" value="work.dj.job.JobStudent"/>
</bean>

<!-- Defining triggers Define a cron The trigger of A trigger can only be bound to one task -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- Define trigger name -->
<property name="name" value="trigger1"/>
<!-- Define trigger groups -->
<property name="group" value="trigger_group1"/>
<!-- Appoint Cron The binding of JobDetail -->
<property name="jobDetail" ref="lxJob"/>
<!-- Appoint Cron The expression of -->
<property name="cronExpression" value="*/5 * * * * ?"/>
</bean>

<!-- Define the scheduler -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<!-- Register triggers , You can see that this is a list -->
<list>
<ref bean="cronTrigger"/>
</list>
</property>

<!-- add to quartz To configure , There are two ways -->
<!-- <property name="configLocation" value="classpath: quartz.properties"/> Use this to import external configuration -->

<!-- The internal configuration -->
<property name="quartzProperties">
<value>
# Specify the scheduler name , Non implementation class
org.quartz.scheduler.instanceName=DefaultQuartzScheduler
# Specifies the thread implementation class
org.quartz.threadPoll.class=org.quartz.simpl.SimpleThreadPool
# The number of threads in the thread pool
org.quartz.threadPoll.threadCount=10
# priority , Default 5
org.quartz.threadPoll.threadPriority=5
# non-persistent job
org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
</value>
</property>
</bean>
</beans>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.

And then again spring In the comprehensive configuration file , Register this file and start this file

copyright notice
author[Chasing dreams_ zx],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270358267390.html

Random recommended