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

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.66.66.66:3306/sssBCompPPPanyyy?useUnicode=true&characterEncoding=utf8
#org.quartz.dataSource.quartzDS.user = dataBaseUser
#org.quartz.dataSource.quartzDS.password = 666666
#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 !   ---定时任务  ---------");
       
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值