当前位置:网站首页>Quartz explanation and use
Quartz explanation and use
2022-06-28 23:35:00 【Encounter in the evening wind】
Catalog
2.1 Five types of Trigger( Timer )
4.Quartz Create a scheduled task
4.3 Specific business implementation
5 Quartz Detailed explanation of core classes
1.Quartz Introduce
Task scheduling framework “Quartz” yes OpenSymphony Open source organizations in Job scheduling Another open source project in the field , It's all about java Developed an open source task schedule management system ,“ Task progress Manager ” It's a pre-determined ( Be put on the agenda ) When the time arrives , Responsible for the execution of ( Or notice ) Other software components of the system .
To put it simply, to realize “ plan ( Or timing ) Mission ” The system of , for example : No payment after order placed ,15 Automatically cancel the order in minutes , And automatically unlock the locked goods
2. Quartz The trigger of
Triggers are used to tell the scheduler when a job is triggered . The framework provides 5 Trigger types , But the two most commonly used SimpleTrigger and CronTrigger.
2.1 Five types of Trigger( Timer )
SimpleTrigger,CronTirgger,DateIntervalTrigger,NthIncludedDayTrigger and Calendar class ( org.quartz.Calendar).
scene :
SimpleTrigger: perform N Time , repeat N Time
CronTrigger: Seconds A little bit When? What day Which month Which week Which year , perform
3. storage
RAMJobStore( Memory job storage type ) and JDBCJobStore( Database job storage type ), The two methods are compared as follows :
3.1 RAMJobStore Don't external databases , Easy to configure , Fast running speed Because the scheduler information is stored in the assigned to JVM In the memory of , therefore , When the application stops running , All scheduling information will be lost . And because of the storage in JVM In memory , So how many can be stored Job and Trigger There will be restrictions
3.2 JDBCJobStore Support clusters , Because all the task information will be saved The speed of running depends on the speed of connecting to the database , Can control things , And if the application server is shut down or restarted , Mission information will not be lost , And you can recover tasks that failed to execute due to server shutdown or restart
3.2 cron expression

4.Quartz Create a scheduled task
4.1 Import Quartz rely on
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>4.2job The task class
package com.wyy.quartz.Text;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@Slf4j
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext Context) throws JobExecutionException {
Object name = Context.getJobDetail().getJobDataMap().get("name");
log.warn(name+" Doing hygiene ");
}
}
4.3 Specific business implementation
package com.wyy.quartz.Text;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;
import java.util.Date;
import static org.quartz.JobBuilder.newJob;
@Component
public class scheduler {
public void getScheduler() {
// Create a dispatch factory class instance StdSchedulerFactory
StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
try {
// Create a scheduler instance Scheduler
Scheduler scheduler = stdSchedulerFactory.getScheduler();
// Create task details JobDetail
JobDetail jobDetail = newJob(MyJob.class)
.withIdentity("detail01", "group01") // Define task PK Make up a unique logo
.withDescription("this is a jobDetail")// describe
.usingJobData("name", " Zhang San ")// Method 1 of transferring values to the specific operation class
.build();// Build tasks
// The second method is to transfer values to the specific operation class
// JobDataMap jobDataMap = jobDetail.getJobDataMap();
// jobDataMap.put("name", " Zhang San ");
// Create trigger
Trigger trigger = (Trigger) TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group01") // Make up a unique logo
.startAt(new Date()) // Starting time
.withDescription("this is a trigger")
// Simple trigger
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
// Repeat every two seconds 3 Time
.withIntervalInSeconds(2).withRepeatCount(3))
// Expression scheduler ( Generate scheduled task expressions online :http://cron.qqe2.com/)
// Every time 5 Execute the job class logic code every second
//.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ? "))
.build();
// Inject job classes and triggers into the scheduler
scheduler.scheduleJob(jobDetail, trigger);
// Start the scheduler
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
The above business codes mainly involve Scheduler Scheduler 、Job Timing task 、Trigger trigger Three categories (quartz Core class )
5 Quartz Detailed explanation of core classes
5.1 Scheduler Scheduler
The main program interface used to interact with the scheduler . Scheduler One was maintained JobDetails and Triggers The registry .
Once in a Scheduler Registered , When the trigger time of the scheduled task arrives , The scheduler will be responsible for executing predefined Job After the scheduler is created , be in “ standby ” state , Must call scheduler Of start() Method to enable the scheduler
have access to shutdown() Method to close the scheduler , Use isShutdown() Method to determine whether the scheduler is closed . adopt Scheduler.scheduleJob(…) Method to include the task in the scheduler , When the task trigger time is up , The task will be performed .
5.2 Job Timing task
Generate task details through task classes ( A task class can have multiple task details ), Pass parameters to the task class .
5.3 Trigger trigger
Trigger use TriggerBuilder To instantiate , There is one TriggerKey relation , In a Scheduler It has to be the only one . Multiple triggers can point to the same job , But a trigger can only point to one job . Triggers can transmit data to job, By putting data into the trigger JobDataMap.
边栏推荐
- Yyds dry goods count 【 vs code work record III 】 set vs code format
- Junior, it's not easy!
- C# 面试题目_20220627记录一下
- mysql-5.7.30-winx64免安装版下载安装教程
- 2022-06-28: what does the following golang code output? A:true; B:false; C:panic; D: Compilation failed. package main import “fm
- frameworks/base/core/res/res/values/symbols.xml:3915: error: no definition for declared symbol解决办法
- 是使用local_setup.bash 还是 setup.bash
- Edge extraction based on Halcon learning [2] circles Hdev routine
- Form verification problem - El select (solution to automatically trigger verification on initialization page)
- Scrapy使用xlwt实现将数据以Excel格式导出的Exporter
猜你喜欢

第二章 经典同步练习作业

【软件分析】软件分析、设计与建模迭代式详解

CIN at QT (the clearest tutorial in the whole network)

MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数

笔记

Association line exploration, how to connect the two nodes of the flow chart

移动端异构运算技术 - GPU OpenCL 编程(基础篇)

stm32F407-------外部中断

Matlab learning notes (6) upsample function and downsample function of MATLAB

Rongyun communication solution solves the pain points of enterprise communication
随机推荐
One card can sell tens of millions, and the business expansion is faster than that of players: you don't understand the Jianghu of star cards
Picture 64base transcoding and decoding
With a monthly salary of 60000 yuan, such people began to be robbed after the Internet "reduced costs and increased efficiency"
2022-06-28:以下golang代码输出什么?A:true;B:false;C:panic;D:编译失败。 package main import “fm
Edge extraction based on Halcon learning [2] circles Hdev routine
Junior, it's not easy!
Rongyun communication solution solves the pain points of enterprise communication
ctfshow XSS
Machine learning 4-dimension reduction technology
[flutter issues Series title 71] Mutual Conversion between uint8list and Image in flutter
Cs5463 code module analysis (including download link)
Basic operation of MySQL database: import hellodb SQL and query as required; Create account and authorize
[matlab] function definition and use
[opencv] - linear filtering: box filtering, mean filtering, Gaussian filtering
C语言-单词分析解析
Cmake tutorial (I)
【狀態機設計】Moore、Mealy狀態機、三段式、二段式、一段式狀態機書寫規範
Yyds dry goods count 【 vs code work record III 】 set vs code format
PHP uses curl to download Excel files after logging in to the website
第三章 处理机调度练习