当前位置:网站首页>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.
边栏推荐
- 第五章 虚拟存储器 练习
- [word Tutorial Series Part 1] how to remove arrows in word tables
- Learn binary tree like this
- PHP 使用endroid/qrcode 二维码生成, GD库生成分享海报
- 【OpenCV】—线性滤波:方框滤波、均值滤波、高斯滤波
- 再次上榜!知道创宇入选2022中国网安产业竞争力50强
- Would like to ask, how to open a stock account? Is it safe to open an account online?
- stm32F407-------通用定时器
- Save data in Excel: use openpyxl to create multiple tables and set excel row limit
- 在DialogFragment的onStop(完全不可见)时调用dismiss退出界面报错解决办法
猜你喜欢

What are the virtual machine software? What are their respective roles?

Keil project, RTT cannot print after too many programs are written

【狀態機設計】Moore、Mealy狀態機、三段式、二段式、一段式狀態機書寫規範

stm32F407-------通用定时器

Online yaml to JSON tool

ctfshow XSS

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

再次上榜!知道创宇入选2022中国网安产业竞争力50强
![[state machine design] Moore, Mealy state machine, three-stage, two-stage and one-stage state machine writing specification](/img/48/e29f34aff7cc437bfb574591d54e3d.png)
[state machine design] Moore, Mealy state machine, three-stage, two-stage and one-stage state machine writing specification
![[mathematical modeling] fmincon() function of MATLAB nonlinear programming](/img/fc/46949679859b1369fcc83d0d8b637c.png)
[mathematical modeling] fmincon() function of MATLAB nonlinear programming
随机推荐
第五章 虚拟存储器 练习
How to solve the database type error in the operation of the servert project?
Scrapy使用xlwt实现将数据以Excel格式导出的Exporter
stm32F407-------通用定时器
See fengzhixia | FENGZikai, the originator of Guoman, for exclusive sale of Digital Collections
Basic operation of MySQL database: import hellodb SQL and query as required; Create account and authorize
Do you know all the wonderful functions of the vlookup function?
Puma joins hands with 10ktf shop to launch its Web3 cooperation project with the largest scale so far
ROS2中的行为树 BehaviorTree
入行数字IC验证后会做些什么?
Interviewer: what is the internal implementation of strings in redis?
融云通信解决方案 破解企业沟通痛点
VSCode里使用条件断点(基于GDB)
CIN at QT (the clearest tutorial in the whole network)
Undefined symbol main (referred from entry9a.o).
Scrapy uses xlwt to implement the exporter that exports data in Excel format
stm32F407-------电容触摸按键
ctfshow XSS
ERROR 1067 (42000): Invalid default value for ‘end_ time‘ Mysql
[conception de la machine d'état] Moore, Mealy State Machine, Three - stage, Two - stage, one - stage State Machine Writing Specification