当前位置:网站首页>Quartz创建定时任务(入门)
Quartz创建定时任务(入门)
2022-07-21 05:01:00 【adadapter】
Quartz可以创建简单或者复杂的执行计划,它支持数据库,集群,插件以及邮件,并且支持 cron 表达式,具有极高的灵活性,提供 3 个 Bean 组件,JobDetail、Trigger以及 ScheduledFactory。
整合 Spring Boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>2.7.0</version>
</dependency>
创建 Job
Job 有两种表述方式,其一是作为一个普通的 JavaBean,通过添加@Component注解将其注册到 Spring 容器中。Job 也可以通过继承抽象类 QuartzJobBean,若继承自 QuartzJobBean,则需要实现该类中的 executeInternal 方法,该方法在任务被调用时使用
方法 1:普通的 JavaBean
@Component
public class MyFirstJob {
public void sayHello(){
System.out.println("MyFirstJob:sayHello"+new Date());
}
}
方法 2:继承抽象类 QuartzJobBean
public class MySecondJob extends QuartzJobBean {
private String name;
public void setName(String name){
this.name = name;
}
@Override
protected void executeInternal(JobExecutionContext context){
System.out.println("hello:"+name+":"+new Date());
}
}
接下来创建QuartzConfig 对 JobDetail和 Trigger 进行配置
@Configuration
public class QuartzConfig {
@Bean
MethodInvokingJobDetailFactoryBean jobDetail_1(){
MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
bean.setTargetBeanName("myFirstJob");
bean.setTargetMethod("sayHello");
return bean;
}
@Bean
JobDetailFactoryBean jobDetail_2(){
JobDetailFactoryBean bean = new JobDetailFactoryBean();
bean.setJobClass(MySecondJob.class);
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("name","heyu");
bean.setJobDataMap(jobDataMap);
bean.setDurability(true);
return bean;
}
@Bean
SimpleTriggerFactoryBean simpleTrigger(){
SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean();
bean.setJobDetail(jobDetail_1().getObject());
bean.setRepeatCount(3);
bean.setRepeatInterval(2000);
return bean;
}
@Bean
CronTriggerFactoryBean cronTrigger(){
CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
bean.setJobDetail(jobDetail_2().getObject());
bean.setCronExpression("* * * * * ?");
return bean;
}
@Bean
SchedulerFactoryBean schedulerFactory(){
SchedulerFactoryBean bean = new SchedulerFactoryBean();
SimpleTrigger simpleTrigger = simpleTrigger().getObject();
CronTrigger cronTrigger = cronTrigger().getObject();
bean.setTriggers(simpleTrigger,cronTrigger);
return bean;
}
}
解释本段代码的重点:
- JobDetail的配置有两种方式:
一、通过 MethodInvokingJobDetailFactoryBean 类配置 JobDetail,这种方法需要指定 Job 的实例名和需要调用的方法,注册这种方式无法在创建 JobDetial 时传递参数
二、通过 JobDetailFactoryBean来实现,只需指定 JobClass,通过 JobDataMap传递参数到 Job 中,Job 只提供对应的属性名,使用一个 set 方法即可接到参数
- Trigger 有多种实现方法,本段代码实现 SimpleTrigger和 CronTrigger,这两种 Trigger 分别使用SimpleTriggerFactoryBean和CronTriggerFactoryBean来创建。在SimpleTriggerFactoryBean对象中,首先设置 JobDetail,然后通过 setRepeatCount 配置任务循环次数,setStartDelay 配置任务启动延迟时间,setRepeatInterval 配置任务的时间间隔。
- 最后需要使用 SchedulerFactoryBean 创建 SchedulerFactory,然后配置 Trigger 即可
运行结果
边栏推荐
- 常用测试用例设计方法之边界值分析法
- Momenta "flywheel L4" accepted the challenge of "pixel level" in the night long tail scene, and its performance was comparable to that of an old driver
- Learning notes introduction to explain
- 解决:Connections could not be acquired from the underlying database!
- NR modulation 4-AM
- 【无标题】
- C -- compilation preprocessing
- STM32——ADC读取光敏传感器控制LED灯,看门狗中断
- 3D数学之三角公式
- ES6的数组、对象拷贝
猜你喜欢
How Jenkins sends e-mail? The automation veteran will teach you
【To .NET】.NET Core Web API开发流程知识点整理[进阶]
NXP i.MX 8M Mini 开发板规格参数,四核ARM Cortex-A53 + ARM Cortex-M4
Trigonometric formula of 3D mathematics
Uniapp develops apps to solve uni Pagescrollto does not take effect
大厂面经分享,速来
Getting started with the gradle project construction tool
如何成功拿到蚂蚁,京东,小米,腾讯等大厂的offer
Introduction to testing -- using scenario method to design ATM test cases
Sequence model (I) - cyclic sequence model
随机推荐
14. 你能说说进程与线程的区别吗
866. 试除法判定质数
STL list输出和增加
MyCat的介绍与安装以及基本使用
3-6月面经总结,200多页真题笔记和详解(含核心考点及6家大厂)
如何确定你访问的网站的真实性——证书体系
Gradually understand the deep belief network
Odoo God operation background call routing interface
Read yaml configuration file based on Hydra Library (support command line parameters)
ctfshow MengXIn misc1
867. Decomposition prime factor
通讯录实现
元宇宙iwemeta:《时代》杂志新封面,元宇宙将改变一切
解决:Connections could not be acquired from the underlying database!
Bluetooth intelligent nutrition electronic scale solution
Json工具 将对象转换为json格式字符串
How to find out whether there is the same user name in the database during user registration in PHP
NXP i.MX 8m Mini development board specifications, quad core arm cortex-a53 + arm cortex-m4
In depth interpretation of the investment logic of the consortium's participation in the privatization of Twitter
Pycharm创建SQLite数据库