当前位置:网站首页>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.
边栏推荐
- C interview questions_ 20220627 record
- Puma joins hands with 10ktf shop to launch its Web3 cooperation project with the largest scale so far
- 运维排查-使用hcache插件排查Buffer/cache占用过高
- ctfshow XSS
- 设计电商秒杀系统
- I can't sleep
- [数学建模]Matlab非线性规划之fmincon()函数
- ERROR 1067 (42000): Invalid default value for ‘end_time‘ Mysql
- Be on the list again! Know that Chuangyu was selected as one of the top 50 competitive enterprises in China's network security industry in 2022
- Picture 64base transcoding and decoding
猜你喜欢

再次上榜!知道创宇入选2022中国网安产业竞争力50强

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

With a monthly salary of 60000 yuan, such people began to be robbed after the Internet "reduced costs and increased efficiency"

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

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

第五章 虚拟存储器 练习

The picture display on the left of the two column layout is determined by the content height on the right

这样学习二叉树

Behaviortree in ros2

机器学习6-决策树
随机推荐
LINQ linked table query
Is it safe to open a stock account on the Internet?
Save data in Excel: use openpyxl to create multiple tables and set excel row limit
Three communication skills in software testing
【SSM】报错 Access denied for user ‘WYF‘@‘localhost‘ (using password: YES) 数据的用户名变成了电脑的用户名
[API packet capturing in selenium automation] installation and configuration of browsermobproxy
第五章 虚拟存储器 练习
[word Tutorial Series Part 1] how to remove arrows in word tables
设计电商秒杀系统
fio的IO重放功能
stm32F407-------LCD
Picture 64base transcoding and decoding
frameworks/base/core/res/res/values/symbols.xml:3915: error: no definition for declared symbol解决办法
Learning fuzzy from SQL injection to bypass the latest safe dog WAF
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
Count the number of arrays with pointers
The secondary market is full of bad news. How should the market go next? One article will show you the general trend
ctfshow XSS
油猴脚本学习