Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目。
是完全由java开发的一个开源的任务日程管理系统
简单来说就是实现“计划(或定时)任务”的系统
quartz官网
如果想要更加深度的了解quartz的话,可以去它的官网看看,下图框起来的地方,有关于quartz知识的所有讲解
它有五种类型的Trigger(定时器)
SimpleTrigger
CronTirgger
DateIntervalTrigger
NthIncludedDayTrigger
Calendar类( org.quartz.Calendar)
今天要介绍的就是SimpleTrigger和CronTrigger。
应用 场景:
SimpleTrigger:执行N次,重复N次
CronTrigger:几秒 几分 几时 哪日 哪月 哪周 哪年,执行
RAMJobStore(内存作业存储类型)和JDBCJobStore(数据库作业存储类型)
优点 | 缺点 | |
---|---|---|
RAMJobStore | 不要外部数据库,配置容易,运行速度快 | 因为调度程序信息是存储在被分配给JVM的内存里面,所以,当应用程序停止运行时,所有调度信息将被丢失。另外因为存储到JVM内存里面,所以可以存储多少个Job和Trigger将会受到限制 |
JDBCJobStore | 支持集群,因为所有的任务信息都会保存 | 运行速度的快慢取决与连接数据库的快慢 到数据库中,可以控制事物,还有就是如果应用服务器关闭或者重启,任务信息都 不会丢失,并且可以恢复因服务器关闭或者重启而导致执行失败的任务 |
在线生成表达式网址
2、导入jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
package com.zking.quartz01.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* @author 小七
* @create 2020-12-03 11:16
*/
public class RamJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("定时任务具体执行的业务逻辑代码。。。");
}
}
package com.zking.quartz01.quartz;
import com.zking.quartz01.job.RamJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
/**
* @author 小七
* @create 2020-12-03 11:07
*/
public class Demo1 {
public static void main(String[] args) throws Exception{
//调度器
SchedulerFactory schedulerFactory=new StdSchedulerFactory();
Scheduler scheduler=schedulerFactory.getScheduler();
//类
JobDetail jobDetail=newJob(RamJob.class)
.withIdentity("job1","group1")
.withDescription("this is job1")
.build();
//触发器
Trigger trigger =(Trigger) TriggerBuilder.newTrigger()
// 一共执行3次,每6秒执行一次
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(3,6))
.withIdentity("trigger1","group1")
.withDescription("this is trigger1")
.build();
//调度器组装类以及触发器
scheduler.scheduleJob(jobDetail,trigger);
scheduler.start();
}
}
package com.zking.quartz01.quartz;
import com.zking.quartz01.job.RamJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
/**
* @author 小七
* @create 2020-12-03 11:07
*
* 表达式触发器
*
* 定时定点执行
*
*/
public class Demo2 {
public static void main(String[] args) throws Exception{
//调度器
SchedulerFactory schedulerFactory=new StdSchedulerFactory();
Scheduler scheduler=schedulerFactory.getScheduler();
//类
JobDetail jobDetail=newJob(RamJob.class)
.withIdentity("job1","group1")
.withDescription("this is job1")
.build();
//触发器
Trigger trigger =(Trigger) TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("0 56 18 * * ?"))
.withIdentity("trigger1","group1")
.withDescription("this is trigger1")
.build();
//调度器组装类以及触发器
scheduler.scheduleJob(jobDetail,trigger);
scheduler.start();
}
}
package com.zking.quartz01.quartz;
import com.zking.quartz01.job.RamJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
/**
* @author 小七
* @create 2020-12-03 11:07
*
* 表达式触发器
*
* 遵循某一规律循环执行,不限次数
*
*/
public class Demo3 {
public static void main(String[] args) throws Exception{
//调度器
SchedulerFactory schedulerFactory=new StdSchedulerFactory();
Scheduler scheduler=schedulerFactory.getScheduler();
//类
JobDetail jobDetail=newJob(RamJob.class)
.withIdentity("job1","group1")
.withDescription("this is job1")
.build();
//触发器
Trigger trigger =(Trigger) TriggerBuilder.newTrigger()
//每十秒执行一次
.withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
.withIdentity("trigger1","group1")
.withDescription("this is trigger1")
.build();
//调度器组装类以及触发器
scheduler.scheduleJob(jobDetail,trigger);
scheduler.start();
}
}
package com.zking.quartz01.job;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* @author 小七
* @create 2020-12-03 11:16
*/
public class RamJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("定时任务具体执行的业务逻辑代码。。。");
JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
System.out.println(jobDataMap.get("name").toString() + ":" + jobDataMap.get("level").toString() + ":" + jobDataMap.get("job").toString());
}
}
package com.zking.quartz01.quartz;
import com.zking.quartz01.job.RamJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
/**
* @author 小七
* @create 2020-12-03 11:07
*
*定时任务参数传递问题
*/
public class Demo4 {
public static void main(String[] args) throws Exception{
//调度器
SchedulerFactory schedulerFactory=new StdSchedulerFactory();
Scheduler scheduler=schedulerFactory.getScheduler();
//类
JobDetail jobDetail=newJob(RamJob.class)
.withIdentity("job1","group1")
.withDescription("this is job1")
.build();
JobDataMap jobDataMap =jobDetail.getJobDataMap();
jobDataMap.put("name","小七");
jobDataMap.put("level","可爱");
jobDataMap.put("job","学生");
//触发器
Trigger trigger =(Trigger) TriggerBuilder.newTrigger()
//每十秒执行一次
.withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
.withIdentity("trigger1","group1")
.withDescription("this is trigger1")
.build();
//调度器组装类以及触发器
scheduler.scheduleJob(jobDetail,trigger);
scheduler.start();
}
}
package com.zking.quartz01.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author 小七
* @create 2020-12-03 19:18
*/
@Component
public class SpringTask {
@Scheduled(cron = "0/10 * * * * ?")
public void xiaoqi(){
System.out.println("spring 框架自带的定时任务.....");
}
}
运行结果
优点 | 缺点 | |
---|---|---|
Spring task | 无需整合spring,作业类中就可以调用业务service | 单线程;不能做数据存储型的定时任务 |
Quartz | 多线程;可以做数据存储型的定时任务,维护性高; | 需要整合spring,不能直接调用业务层service; |
springtask单线程
运行结果
quartz多线程 案例
package com.zking.quartz01.job;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author 小七
* @create 2020-12-03 11:16
*/
public class RamJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.err.println(format.format(new Date())+" : 基于RAM的quartz调度框架定时任务...");
try {
Thread.sleep(20*1000);
System.out.println("模拟正在处理大数据....");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.zking.quartz01.quartz;
import com.zking.quartz01.job.RamJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
/**
* @author 小七
* @create 2020-12-03 20:06
*/
public class Demo5 {
public static void main(String[] args) throws SchedulerException {
SchedulerFactory factory = new StdSchedulerFactory();
// 调度器创建
Scheduler scheduler = factory.getScheduler();
// 具体定时任务需要执行的代码
JobDetail jobDetail = newJob(RamJob.class)
.withIdentity("job2", "group1")
.withIdentity("这是一个作业类案例")
.build();
Trigger trigger = (Trigger) TriggerBuilder.newTrigger()
// 每10s执行一次
.withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
// 触发器标识
.withIdentity("trigger2", "group1")
.withDescription("这是一个触发器")
.build();
// 调度工厂绑定作业类及触发器
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
}
}
运行结果