当前位置:网站首页>Learning and using quartz scheduling framework
Learning and using quartz scheduling framework
2022-06-23 07:11:00 【From meow star】
Quartz Learning and using scheduling framework
Relatively complete blog:
https://blog.csdn.net/ScholarTang/article/details/118891297
introduction 1 (SimpleTrigger trigger )
- Add dependency configuration
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
- Create a class , Realization Job Interface , Realization execute Method
- Create scheduler Scheduler
- Use Job Implementation classes create task instances JobDetail
- Create trigger Trigger, It uses SimpleTrigger trigger
- Bind task instance JobDetail And triggers Trigger
- Turn on timed tasks
- Specific code :
@Slf4j
//@PersistJobDataAfterExecution //Job Set to stateful
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
log.info(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + " | The mission was carried out ");
}
}
public class HelloSchedulerDemo {
public static void main(String[] args) throws SchedulerException {
// Get the scheduler instance from the scheduling factory
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// adopt JobBuilder Build a task instance
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
// Set the unique instance name and task group name of the task
.withIdentity("job1", "group1")
// Build instance
.build();
// adopt TriggerBuilder Instance building triggers
SimpleTrigger trigger = TriggerBuilder.newTrigger()
// Set the unique instance name of the trigger and the group name of the trigger
.withIdentity("trigger1", "group1")
// Implementation plan , Every five seconds
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5))
// Execute now
.startNow()
// Build instance
.build();
// The scheduler binds task instances and triggers
scheduler.scheduleJob(jobDetail,trigger);
// Turn on timed tasks
scheduler.start();
}
}
introduction 2 (CronTrigger trigger )
- Create a class , Realization Job Interface , Realization execute Method
- Create scheduler Scheduler
- Use Job Implementation classes create task instances JobDetail
- Create trigger Trigger, It uses CronTrigger trigger , Support Cron expression
- Bind task instance JobDetail And triggers Trigger
- Turn on timed tasks
- Specific code
public class HelloSchedulerDemo {
public static void main(String[] args) throws SchedulerException {
// Get the scheduler instance from the scheduling factory
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// adopt JobBuilder Build a task instance
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
// Set the unique instance name and task group name of the task
.withIdentity("job1", "group1")
// Build instance
.build();
// adopt TriggerBuilder Instance building triggers
Trigger trigger = TriggerBuilder.newTrigger()
// Set the unique instance name of the trigger and the group name of the trigger
.withIdentity("trigger1", "group1")
// Implementation plan , Every five seconds
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ? *"))
// Build instance
.build();
// The scheduler binds task instances and triggers
scheduler.scheduleJob(jobDetail,trigger);
// Turn on timed tasks
scheduler.start();
}
}
quartz The use of listeners in
Mainly used JobListener、TriggerListener、SchedulerListener Listen to the status in the task
- Create a class , Realization Job Interface , Realization execute Method
- Create a class , Implement the global listener interface JobListener
- Create scheduler Scheduler object
- Use Job Implementation classes create task instances JobDetail object
- Create trigger Trigger object , It uses CronTrigger trigger , Support Cron expression
- Bind task instance JobDetail And triggers Trigger, Bind task instances and listeners JobListener
- Turn on timed tasks
- Specific code
Job Implementation class :
@Slf4j
//@PersistJobDataAfterExecution //Job Set to stateful
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
log.info("[Job Executive class ]"+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + " | The mission was carried out ");
}
}
JobListener Implementation class :
@Slf4j
public class MyJobListener implements JobListener {
@Override
public String getName() {
String name = this.getClass().getSimpleName();
log.info("[ Monitor ] At present JobListener For the name of the :" + name);
return name;
}
@Override
public void jobToBeExecuted(JobExecutionContext context) {
String name = context.getJobDetail().getJobClass().getName();
log.info("[ Monitor ] At present Job For the name of the :" + name + ",JobDetail Will be carried out ...");
}
@Override
public void jobExecutionVetoed(JobExecutionContext context) {
String name = context.getJobDetail().getJobClass().getName();
log.info("[ Monitor ] At present Job For the name of the :" + name + ",JobDetail To be executed , But being TriggerListener veto ...");
}
@Override
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
String name = context.getJobDetail().getJobClass().getName();
log.info("[ Monitor ] At present Job For the name of the :" + name + ",JobDetail Execution completed ...");
}
}
Task startup class :
public class HelloSchedulerDemo {
public static void main(String[] args) throws SchedulerException {
// Get the scheduler instance from the scheduling factory
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// adopt JobBuilder Build a task instance
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
// Set the unique instance name and task group name of the task
.withIdentity("job1", "group1")
// Build instance
.build();
// adopt TriggerBuilder Instance building triggers
Trigger trigger = TriggerBuilder.newTrigger()
// Set the unique instance name of the trigger and the group name of the trigger
.withIdentity("trigger1", "group1")
// Implementation plan , Every five seconds
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ? *"))
// Build instance
.build();
// The scheduler binds task instances and triggers
scheduler.scheduleJob(jobDetail,trigger);
scheduler.getListenerManager().addJobListener(new MyJobListener(), KeyMatcher
//jobKey Medium name Parameters and group The parameter corresponds to the task instance 【JobDetail】 Of name and group
.keyEquals(JobKey.jobKey("job1","group1")));
// Turn on timed tasks
scheduler.start();
}
}

Other listeners are TriggerListener、SchedulerListener
边栏推荐
- Why can't the index of JS array use negative numbers
- Verilog syntax explanation
- C language operator priority formula
- 如何达到高效的网络信息传播
- 小白投资理财必看:图解基金买入与卖出规则
- Concepts and differences of DQL, DML, DDL and DCL
- GINet
- Analysis of personalized learning progress in maker Education
- 306. 累加数
- 406-双指针(27. 移除元素、977.有序数组的平方、15. 三数之和、18. 四数之和)
猜你喜欢

宝塔忘记密码

GloRe

小白投资理财必看:图解基金买入与卖出规则

junit单元测试报错org.junit.runners.model.InvalidTestClassError: Invalid test class ‘xxx‘ .No runnable meth

MySQL重做日志 redo log

Regular expression graph and text ultra detailed summary without rote memorization (Part 1)

聚焦行业,赋能客户 | 博云容器云产品族五大行业解决方案发布

Eureka

Interpreting the spirit of unity and cooperation in maker Education
![[project training] multi segment line expanded to parallel line](/img/f2/ee4985fd2454bf00d600e34a818f2d.png)
[project training] multi segment line expanded to parallel line
随机推荐
MySQL optimization
[project training] multi segment line expanded to parallel line
TP6+Redis+think-queue+Supervisor实现进程常驻消息队列/job任务
20220621 Three Conjugates of Dual Quaternions
JS dynamically creates a href circular download file. Only 10 or a fixed number of files can be downloaded
Summarized benefits
Xiaobai must see in investment and wealth management: illustrated fund buying and selling rules
Why can't the index of JS array use negative numbers
GIS实战应用案例100篇(七十九)-多规整合底图的制作要点
315. 计算右侧小于当前元素的个数
303. 区域和检索 - 数组不可变
Analyzing the creation principle in maker Education
【日常训练】513. 找树左下角的值
如何达到高效的网络信息传播
Using fuser to view file usage
898. 子数组按位或操作
TP6 安装拓展
js 判断两个数组增加和减少的元素
318. maximum word length product
XML schema record