1、添加工程依赖
org.quartz-scheduler
quartz
2.3.0
org.quartz-scheduler
quartz-jobs
2.3.0
org.springframework
spring-context-support
2、SchedulerConfig
import java.io.IOException;
import java.util.Properties;
import org.quartz.Scheduler;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import javax.sql.DataSource;
/**
*
* @author yunfei
* @date 2018/7/30 11:06
*/
@Configuration
public class SchedulerConfig {
@Qualifier("dataSource")
@Autowired
private DataSource dataSource;
@Bean(name="SchedulerFactory")
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
// 设置dataSource 跟项目使用同一个dataSource,会覆盖quartz.properties里面的配置
factory.setDataSource(dataSource);
factory.setQuartzProperties(quartzProperties());
return factory;
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
//在quartz.properties中的属性被读取并注入后再初始化对象
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
/**
* quartz初始化监听器
*/
@Bean
public QuartzInitializerListener executorListener() {
return new QuartzInitializerListener();
}
/**
* 通过SchedulerFactoryBean获取Scheduler的实例
*/
@Bean(name="Scheduler")
public Scheduler scheduler() throws IOException {
return schedulerFactoryBean().getScheduler();
}
}
3、quartz.properties
# 固定前缀org.quartz
# 主要分为scheduler、threadPool、jobStore、plugin等部分
org.quartz.scheduler.instanceName=DefaultQuartzScheduler
org.quartz.scheduler.rmi.export=false
org.quartz.scheduler.rmi.proxy=false
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false
# 实例化ThreadPool时,使用的线程类为SimpleThreadPool
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
# threadCount和threadPriority将以setter的形式注入ThreadPool实例
# 并发个数
org.quartz.threadPool.threadCount=5
# 优先级
org.quartz.threadPool.threadPriority=5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
org.quartz.jobStore.misfireThreshold=5000
# 默认存储在内存中
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#持久化
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
#持久化方式配置数据驱动,MySQL数据库
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.dataSource=qzDS
org.quartz.dataSource.qzDS.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.qzDS.URL=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
org.quartz.dataSource.qzDS.user=root
org.quartz.dataSource.qzDS.password=root
org.quartz.dataSource.qzDS.maxConnections=10
management.security.enabled=false
官方github里有建表脚本:
https://github.com/quartz-scheduler/quartz/tree/master/quartz-core/src/main/resources/org/quartz/impl/jdbcjobstore
4、SchedulerJob 任务执行实现
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
/**
* @author yunfei
* @date 2018/7/26 11:09
*/
public class SchedulerJob implements Job {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//这里可以获取控制器绑定的值,实际应用中可以设置为某个活动的id,以便进行数据库操作
Object jobName = jobExecutionContext.getJobDetail().getKey();
logger.info("定时任务[{}]开始执行>{}", jobName, new Date());
}
}
5、使用例子
public interface JobService {
/**
* 开启任务
*
* @author yunfei
* @method
* @date 2018/7/26 10:24
* @param jobName
* @param groupName
* @param corn
* @return
*/
Object jobStart(String jobName, String groupName, String corn);
/**
* 暂停任务
*
* @author yunfei
* @method
* @date 2018/7/26 11:08
* @param jobName
* @param groupName
* @return
*/
void jobPause(String jobName, String groupName) ;
/**
* 删除任务
*
* @author yunfei
* @method
* @date 2018/7/26 14:30
* @param jobName
* @param groupName
* @return
*/
void jobDelete(String jobName, String groupName);
/**
* 任务重置
*
* @author yunfei
* @method
* @date 2018/7/26 16:48
* @param jobName
* @param groupName
* @return
*/
void jobResume(String jobName, String groupName);
/**
* 任务更新
*
* @author yunfei
* @method
* @date 2018/7/26 16:50
* @param jobName
* @param groupName
* @param cron
* @return
*/
void jobReschedule(String jobName, String groupName, String cron);
/**
* 任务列表
*
* @author yunfei
* @method
* @date 2018/7/26 16:54
* @param
* @return
*/
Object getJobs();
/**
* 获取任务状态
*
* @author yunfei
* @method
* @date 2018/7/30 9:20
* @param jobName
* @param groupName
* @return
*/
String getJobStatus(String jobName,String groupName);
/**
* 获取任务列表和状态
*
* @author yunfei
* @method
* @date 2018/7/30 9:41
* @param
* @return
*/
ReturnDto getJobsAndStatus();
/**
* 获取当前正在执行任务
*
* @author yunfei
* @method
* @date 2018/7/30 10:14
* @param
* @return
*/
ReturnDto getCurrentExecutingJobs();
}
@Service
public class JobServiceImpl implements JobService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
@Qualifier("Scheduler")
private Scheduler scheduler;
@Override
public Object jobStart(String jobName, String groupName, String corn) {
//配置定时任务对应的Job,这里执行的是ScheduledJob类中定时的方法
JobDetail jobDetail = JobBuilder
.newJob(SchedulerJob.class)
.usingJobData("jobName", jobName)
.withIdentity(jobName, groupName)
.build();
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(corn);
CronTrigger cronTrigger = TriggerBuilder.newTrigger()
.withIdentity("trigger-" + jobName, groupName)
.withSchedule(scheduleBuilder)
.build();
try {
scheduler.scheduleJob(jobDetail, cronTrigger);
} catch (SchedulerException e) {
logger.error("任务启动失败>{}", e);
}
return jobName;
}
@Override
public void jobPause(String jobName, String groupName) {
try {
scheduler.pauseJob(JobKey.jobKey(jobName, groupName));
} catch (SchedulerException e) {
logger.error("任务暂停失败>{}", e);
}
}
@Override
public void jobDelete(String jobName, String groupName) {
try {
scheduler.pauseTrigger(TriggerKey.triggerKey(jobName, groupName));
scheduler.unscheduleJob(TriggerKey.triggerKey(jobName, groupName));
scheduler.deleteJob(JobKey.jobKey(jobName, groupName));
} catch (SchedulerException e) {
logger.error("任务删除失败>{}", e);
}
}
@Override
public void jobResume(String jobName, String groupName) {
try {
scheduler.resumeJob(JobKey.jobKey(jobName, groupName));
} catch (SchedulerException e) {
logger.error("任务重置失败>{}", e);
}
}
@Override
public void jobReschedule(String jobName, String groupName, String cron) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, groupName);
// 表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
// 按新的cronExpression表达式重新构建trigger
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
// 按新的trigger重新设置job执行
scheduler.rescheduleJob(triggerKey, trigger);
} catch (SchedulerException e) {
logger.error("更新任务失败>{}", e);
}
}
@Override
public Object getJobs() {
Set jobKeys = new HashSet();
try {
Set triggerKeys = scheduler.getTriggerKeys(GroupMatcher.anyTriggerGroup());
//获取所有的job集合
jobKeys = scheduler.getJobKeys(GroupMatcher.anyJobGroup());
} catch (SchedulerException e) {
logger.error("获取任务列表失败>{}", e);
}
return jobKeys;
}
@Override
public String getJobStatus(String jobName, String groupName) {
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, groupName);
String name = null;
try {
Trigger.TriggerState state = scheduler.getTriggerState(triggerKey);
name = state.name();
logger.info(state.name());
} catch (SchedulerException e) {
logger.error(e.getMessage(), e);
}
return name;
}
@Override
public ReturnDto getJobsAndStatus() {
List