Spring整合Quartz实现动态任务调度定时器

时间:2020-12-19 23:25:12

1、spring配置

     <bean id="schedulerFactoryBean" 
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
lazy-init="false">
<!-- <property name="triggers"> <list> <ref bean="indexingInformationJobTrigger"
/> </list> </property> -->
<property name="configLocation" value="classpath:quartz.properties">
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>


2、quartz.properties配置

在src目录下建立quartz.properties代码如下

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#

#cluster configuration
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.instanceId: AUTO

org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false

#threadPool configuration
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

#jobStore configuration
org.quartz.jobStore.misfireThreshold: 60000
org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.tablePrefix: QRTZ_
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#org.quartz.jobStore.dataSource = quartzDS

#dataSource configuration
#org.quartz.dataSource.quartzDS.driver = com.mysql.jdbc.Driver
#org.quartz.dataSource.quartzDS.URL=jdbc:mysql://10.1.84.109:3306/jeefw?useUnicode=true&characterEncoding=utf8
#org.quartz.dataSource.quartzDS.user = jeefw
#org.quartz.dataSource.quartzDS.password = jeefw
#org.quartz.dataSource.quartzDS.maxConnections =100


3、定时器工作类实现

package com.wangru.qrtz;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdScheduler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@SuppressWarnings("resource")
public class TestWangRu {


public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("config/spring/spring-common.xml");//SchedulerFactoryBean的配置文件地址
StdScheduler SCHEDULER = (StdScheduler)context.getBean("schedulerFactoryBean");//根据schedulerFactoryBean名字获取bean

JobDataMap jdm = new JobDataMap();
JobKey jobKey = new JobKey("wangru", "ru");
TriggerKey triggerKey = new TriggerKey("wangru","ru");
JobDetail jobDetail = null;
Trigger trigger = null;
// 验证任务是否已存在,若不存在创建新任务,若存在更新新定时规则
if (!SCHEDULER.checkExists(jobKey)) {
jobDetail = JobBuilder.newJob(Wangru.class)
.withIdentity(jobKey)
.setJobData(jdm)
.build();
trigger = TriggerBuilder.newTrigger()
.withDescription(java.net.URLEncoder.encode("wangruWithDescription", "UTF-8"))
.withIdentity(triggerKey)
.withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ? *"))
.build();
SCHEDULER.scheduleJob(jobDetail, trigger);
SCHEDULER.getJobDetail(jobKey).getJobDataMap();
} else {
deleteTimer("wangru","ru");
Thread.sleep(1000);
jobDetail = JobBuilder.newJob(Wangru.class)
.withIdentity(jobKey)
.setJobData(jdm)
.build();
trigger = TriggerBuilder.newTrigger()
.withDescription(java.net.URLEncoder.encode("ModfywangruWithDescription", "UTF-8"))
.withIdentity(triggerKey)
.withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ? *"))
.build();
SCHEDULER.scheduleJob(jobDetail, trigger);
}
}


/**
* 移除定时
* @param name
* @param group
* @throws SchedulerException
*/
public static void deleteTimer(String name,String group) throws SchedulerException {
ApplicationContext context = new ClassPathXmlApplicationContext("config/spring/spring-common.xml");
StdScheduler SCHEDULER = (StdScheduler)context.getBean("schedulerFactoryBean");
JobKey jobKey = new JobKey(name, group);
TriggerKey triggerKey = new TriggerKey(name,group);
if (SCHEDULER.checkExists(jobKey)){
SCHEDULER.deleteJob(jobKey);
SCHEDULER.pauseTrigger(triggerKey);// 停止触发器
SCHEDULER.unscheduleJob(triggerKey);// 移除触发器
SCHEDULER.deleteJob(jobKey);// 删除任务
}
}


/**
* 暂停定时
* @throws SchedulerException
*/
public static void pauseTime(String jobName,String jobGroup) throws SchedulerException{
ApplicationContext context = new ClassPathXmlApplicationContext("config/spring/spring-common.xml");
StdScheduler SCHEDULER = (StdScheduler)context.getBean("schedulerFactoryBean");
JobKey jobKey = new JobKey(jobName, jobGroup);

if (SCHEDULER.checkExists(jobKey)) {
SCHEDULER.pauseJob(jobKey);
}
}

/**
* 恢复定时
* @throws SchedulerException
*/
public static void resumeTime(String jobName,String jobGroup) throws SchedulerException {
ApplicationContext context = new ClassPathXmlApplicationContext("config/spring/spring-common.xml");
StdScheduler SCHEDULER = (StdScheduler)context.getBean("schedulerFactoryBean");;
JobKey jobKey = new JobKey(jobName, jobGroup);
if (SCHEDULER.checkExists(jobKey)){
SCHEDULER.resumeJob(jobKey);
}
}

}


4、定时器任务类实现

package com.wangru.qrtz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Wangru implements Job{

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("hello f------f quartz ! ---定时任务 ---------");

}
}