一、使用
1.1导入依赖
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
1.2 基本使用
package com.lang.web;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.GregorianCalendar;
public class HelloQuartz {
public static void main(String[] args) throws Exception{
/*调度器*/
Scheduler scheduled= StdSchedulerFactory.getDefaultScheduler();
/*触发器:描述任务如何触发*/
SimpleTrigger trigger=TriggerBuilder.newTrigger().withIdentity("trigger1","group1")
.startNow()/*开始时间*/
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).withRepeatCount(10))/*执行规则,没两秒执行一次,执行10此*/
.endAt(new GregorianCalendar(2020,8,8,21,51,30).getTime()).build()/*结束时间*/;
/*JobDetail基于Job做封装*/
JobDetail jobDetail=JobBuilder.newJob(HelloJob.class).withIdentity("job1","group1").build();/*重名没问题不是一个*/
/*将JobDetail和触发器增加到调度器中*/
scheduled.scheduleJob(jobDetail,trigger);
/*启动调度器*/
scheduled.start();
}
}
package com.lang.web;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/*定义Job*/
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
/*写逻辑*/
System.out.println("job");
}
}
1.3 配置
quartz.properties文件
# 指定调度器名称,非实现类
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
# 指定线程池实现类
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
# 线程池线程数量
org.quartz.threadPool.threadCount = 10
# 优先级,默认5
org.quartz.threadPool.threadPriority = 5
# 非持久化job
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
二、触发器
SimpleTrigger
CronTrigger
适合于更复杂的任务,它支持类型于Linux Cron语法(并且更强大)
- 指定Crom表达式即可
/2代表没2分钟执行一次,如果在秒的位置就是每两秒执行一次
星期是从2开始的国外7是第一天
Spring整合Quartz
依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qf</groupId>
<artifactId>quartz02</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<springframework.version>5.1.6.RELEASE</springframework.version>
<quartz.version>2.2.3</quartz.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
</dependencies>
</project>
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
Spring整合Quartz进行配置遵循下面的步骤:
1:定义工作任务的Job
2:定义触发器Trigger,并将触发器与工作任务绑定
3:定义调度器,并将Trigger注册到Scheduler
-->
<!-- 1:定义任务的bean ,这里使用JobDetailFactoryBean,也可以使用MethodInvokingJobDetailFactoryBean ,配置类似-->
<bean name="lxJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 指定job的名称 -->
<property name="name" value="job1"/>
<!-- 指定job的分组 -->
<property name="group" value="job_group1"/>
<!-- 指定具体的job类 -->
<property name="jobClass" value="com.qf.quartz.MyJob"/>
</bean>
<!-- 2:定义触发器的bean,定义一个Cron的Trigger,一个触发器只能和一个任务进行绑定 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 指定Trigger的名称 -->
<property name="name" value="trigger1"/>
<!-- 指定Trigger的名称 -->
<property name="group" value="trigger_group1"/>
<!-- 指定Tirgger绑定的JobDetail -->
<property name="jobDetail" ref="lxJob"/>
<!-- 指定Cron 的表达式 ,当前是每隔5s运行一次 -->
<property name="cronExpression" value="* * * * * ?" />
</bean>
<!-- 3.定义调度器,并将Trigger注册到调度器中 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
<!-- 添加 quartz 配置,如下两种方式均可 -->
<!--<property name="configLocation" value="classpath:quartz.properties"></property>-->
<property name="quartzProperties">
<value>
# 指定调度器名称,实际类型为:QuartzScheduler
org.quartz.scheduler.instanceName = MyScheduler
# 指定线程池
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
# 线程池线程数量
org.quartz.threadPool.threadCount = 11
# 优先级
org.quartz.threadPool.threadPriority = 5
# 不持久化job
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
</value>
</property>
</bean>
</beans>