1. Quartz介绍
任务调度框架“Quartz”是OpenSymphony开源组织在Job scheduling领域又一个开源项目,是完全由java开发的一个开源的任务日程管理系统,
“任务进度管理器”就是一个在预先确定(被纳入日程)的时间到达时,负责执行(或者通知)其他软件组件的系统。
简单来说就是实现“计划(或定时)任务”的系统,例如:订单下单后未付款,15分钟后自动撤消订单,并自动解锁锁定的商品
2. Quartz的触发器
触发器用来告诉调度程序作业什么时候触发。框架提供了5种触发器类型,但两个最常用的SimpleTrigger和CronTrigger。
五种类型的Trigger(定时器)
SimpleTrigger,CronTirgger,DateIntervalTrigger,NthIncludedDayTrigger和Calendar类( org.quartz.Calendar)。
场景:
SimpleTrigger:执行N次,重复N次
CronTrigger:几秒 几分 几时 哪日 哪月 哪周 哪年,执行
3. 存储方式
RAMJobStore(内存作业存储类型)和JDBCJobStore(数据库作业存储类型),两种方式对比如下:
优点 缺点
RAMJobStore 不要外部数据库,配置容易,运行速度快 因为调度程序信息是存储在被分配给JVM的内存里面,
所以,当应用程序停止运行时,所有调度信息将被丢失。
另外因为存储到JVM内存里面,所以可以存储多少个Job和Trigger将会受到限制
JDBCJobStor 支持集群,因为所有的任务信息都会保存 运行速度的快慢取决与连接数据库的快慢
到数据库中,可以控制事物,还有就是如
果应用服务器关闭或者重启,任务信息都
不会丢失,并且可以恢复因服务器关闭或
者重启而导致执行失败的任务
图解quartz工作流程
quartz相关表达式
在线生成表达式网址:http://cron.qqe2.com/
所需pom依赖
org.quartz-scheduler
quartz
2.2.1
org.springframework
spring-aspects
RamJob
package com.xzy.quart01.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-11-14 16:43
*/
public class RamJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("此处为quartz定时任务执行的业务内容。。。");
}
}
Demo1
package com.xzy.quart01.demo;
import com.xzy.quart01.job.RamJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-11-14 16:45
*/
public class Demo1 {
public static void main(String[] args) throws SchedulerException {
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
JobDetail jobDetail = newJob(RamJob.class)
.withIdentity("job1","group1")
.withDescription("this is a job")
.build();
Trigger trigger = (Trigger)TriggerBuilder.newTrigger()
//(3,6)重复3次,时隔6秒
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(3,6))
.withIdentity("trigger1","group1")
.withDescription("this is a trigger")
.build();
scheduler.scheduleJob(jobDetail,trigger);
scheduler.start();
}
}
Demo2
package com.xzy.quart01.demo;
import com.xzy.quart01.job.RamJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-11-14 16:49
*
* 表达式触发器(定时定点,无限循环执行某一代码)
*/
public class Demo2 {
public static void main(String[] args) throws SchedulerException {
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
JobDetail jobDetail = newJob(RamJob.class)
.withIdentity("job2","group2")
.withDescription("this is a job2")
.build();
Trigger trigger = (Trigger)TriggerBuilder.newTrigger()
//定点执行逻辑
// .withSchedule(CronScheduleBuilder.cronSchedule("0 9 17 * * ?"))
//定点执行逻辑
.withSchedule(CronScheduleBuilder.cronSchedule("0/6 * * * * ?"))
.withIdentity("trigger2","group2")
.withDescription("this is a trigger2")
.build();
scheduler.scheduleJob(jobDetail,trigger);
scheduler.start();
}
}
Demo3
package com.xzy.quart01.demo;
import com.xzy.quart01.job.RamJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-11-14 16:45
*/
public class Demo3 {
public static void main(String[] args) throws SchedulerException {
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
JobDetail jobDetail = newJob(RamJob.class)
.withIdentity("job1","group1")
.withDescription("this is a job3")
.usingJobData("name","xzy")
.build();
JobDataMap jobDataMap = jobDetail.getJobDataMap();
jobDataMap.put("job","java");
jobDataMap.put("level","javaxiaoxu");
Trigger trigger = (Trigger)TriggerBuilder.newTrigger()
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(3,6))
.withIdentity("trigger1","group1")
.withDescription("this is a trigger3")
.build();
scheduler.scheduleJob(jobDetail,trigger);
scheduler.start();
}
}
package com.xzy.quart01.job;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-11-14 16:43
*/
public class RamJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("quartz定时任务~~~~~~~~~~~~~~~~~~~~~");
// JobDataMap jobDataMap = JobExecutionContext().getJobDataMap();
JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
System.out.println(jobDataMap.get("name") + "" + jobDataMap.get("level") + "" + jobDataMap.get("job"));
}
}
Spring task
优点:无需整合spring,作业类中就可以调用业务service
缺点:单线程;不能做数据存储型的定时任务
Quartz
优点:多线程;可以做数据存储型的定时任务,维护性高;
缺点:需要整合spring,不能直接调用业务层service;
然后我们写个案例,在运行定时的时候写一个线程休眠,看看定时的时间会不会受到影响
SpringTask
package com.xzy.quart01.task;
import org.springframework.scheduling.annotation.Scheduled;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-11-15 16:50
*/
public class SpringTask {
@Scheduled(cron = "0/10 * * * * ?")
public void xxx(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.err.println("spring Task..."+format.format(new Date()));
try {
Thread.sleep(20*1000);
System.out.println("处理大数据量的耗时操作~~~~~~~~~~");
//System.out.println("每十秒执行一次 spring task");
} catch (InterruptedException e) {
e.printStackTrace();
}
//System.out.println("10 9 8 7 6 5 4 3 2 1 ");
}
}
Quart01Application中添加注解@EnableScheduling
package com.xzy.quart01;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class Quart01Application {
public static void main(String[] args) {
SpringApplication.run(Quart01Application.class, args);
}
}
Quartz
Demo4
package com.xzy.quart01.demo;
import com.xzy.quart01.job.RamJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-11-14 16:55
*
* 讲解简单触发器
*/
public class Demo4 {
public static void main(String[] args) throws SchedulerException {
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
JobDetail jobDetail = newJob(RamJob.class)
.withIdentity("job2","group2")
.withDescription("this is a job")
.build();
Trigger trigger = (Trigger)TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
.withIdentity("trigger2","group2")
.withDescription("this is a trigger")
.build();
scheduler.scheduleJob(jobDetail,trigger);
scheduler.start();
}
}
RamJob
package com.xzy.quart01.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 XZY
* @site www.Xzy.com
* @company
* @create 2019-11-14 16:43
*/
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("quartz Task 定时启动..."+format.format(new Date()));
try {
Thread.sleep(20*1000);
System.out.println("处理大数据量的好使操作~~~~~~~~~~");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}