hncdyj的gravatar头像
hncdyj 2014-08-08 14:03:41

spring动态配置定时器quartz2.x教程详解

废话不多说,大家在项目中肯定会遇到动态配置quartz,我这里就抛砖引玉的说说,我喜欢和实在的打交道,喜欢装自己很牛X的人请勿喷。

简单job配置上代码:(一般配置job,一个task,一个myTrigger,一个调度工厂)


	<!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法 -->
	<bean id="taskJob" class="com.dynamic.job.task.MyTask" />
	<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="group" value="job_work" />
		<property name="name" value="job_work_name" />
		<!--false表示等上一个任务执行完后再开启新的任务 -->
		<property name="concurrent" value="false" />
		<property name="targetObject" ref="taskJob" />
		<property name="targetMethod" value="run" />
	</bean>

	<!-- 调度触发器 -->
	<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="name" value="work_default_name" />
		<property name="group" value="work_default" />
		<property name="jobDetail">
			<ref bean="jobDetail" />
		</property>
		<property name="cronExpression">
			<value>* 37 10 * * ?</value>
		</property>
	</bean>

	<!-- 调度工厂 -->
	<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="myTrigger" />
			</list>
		</property>
	</bean>

复杂的job 应该是可以自己配置job启动时间,启动,暂停,恢复,更新表达式:

public static void main(String[] args) throws SchedulerException {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext-1.xml");
		scheduler = (Scheduler) context.getBean("scheduler");
		startJob();
	}

	public static void startJob() throws SchedulerException {
		// schedulerFactoryBean 由spring创建注入
		// 这里获取任务信息数据
		List<ScheduleJob> jobList = DataWorkContext.getAllJob();
		for (ScheduleJob job : jobList) {
			TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
			// 获取trigger,即在spring配置文件中定义的 bean id="myTrigger"
			CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
			// 不存在,创建一个
			if (null == trigger) {
				JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class).withIdentity(job.getJobName(), job.getJobGroup()).build();
				jobDetail.getJobDataMap().put("scheduleJob", job);
				// 表达式调度构建器
				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
				// 按新的cronExpression表达式构建一个新的trigger
				trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup()).withSchedule(scheduleBuilder).build();
				scheduler.scheduleJob(jobDetail, trigger);
			} else {
				// Trigger已存在,那么更新相应的定时设置
				// 表达式调度构建器
				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
				// 按新的cronExpression表达式重新构建trigger
				trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
				// 按新的trigger重新设置job执行
				scheduler.rescheduleJob(triggerKey, trigger);
			}
		}
	}

	/**
	 * 获取所有的Job
	 * 
	 * @throws SchedulerException
	 */
	public static void getJobAll() throws SchedulerException {
		// schedulerFactoryBean 由spring创建注入
		GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
		Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
		List<ScheduleJob> jobList = new ArrayList<ScheduleJob>();
		for (JobKey jobKey : jobKeys) {
			List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
			for (Trigger trigger : triggers) {
				ScheduleJob job = new ScheduleJob();
				job.setJobName(jobKey.getName());
				job.setJobGroup(jobKey.getGroup());
				job.setDesc("触发器:" + trigger.getKey());
				Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
				job.setJobStatus(triggerState.name());
				if (trigger instanceof CronTrigger) {
					CronTrigger cronTrigger = (CronTrigger) trigger;
					String cronExpression = cronTrigger.getCronExpression();
					job.setCronExpression(cronExpression);
				}
				logger.info("all job is " + job.getJobName());
				jobList.add(job);
			}
		}
	}

	/**
	 * 获取正在运行的job
	 * 
	 * @throws SchedulerException
	 */
	public static void isRunJob() throws SchedulerException {
		List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
		List<ScheduleJob> jobList = new ArrayList<ScheduleJob>(executingJobs.size());
		for (JobExecutionContext executingJob : executingJobs) {
			ScheduleJob job = new ScheduleJob();
			JobDetail jobDetail = executingJob.getJobDetail();
			JobKey jobKey = jobDetail.getKey();
			Trigger trigger = executingJob.getTrigger();
			job.setJobName(jobKey.getName());
			job.setJobGroup(jobKey.getGroup());
			job.setDesc("触发器:" + trigger.getKey());
			Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
			job.setJobStatus(triggerState.name());
			if (trigger instanceof CronTrigger) {
				CronTrigger cronTrigger = (CronTrigger) trigger;
				String cronExpression = cronTrigger.getCronExpression();
				job.setCronExpression(cronExpression);
			}
			logger.info("runing job is " + job.getJobName());
			jobList.add(job);
		}
	}

	/**
	 * 暂停Job
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	public static void pauseJob(ScheduleJob scheduleJob) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
		scheduler.pauseJob(jobKey);
	}

	/**
	 * 恢复Job
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	public static void resumeJob(ScheduleJob scheduleJob) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
		scheduler.resumeJob(jobKey);
	}

	/**
	 * 立即运行任务一次
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	public static void triggerJob(ScheduleJob scheduleJob) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
		scheduler.triggerJob(jobKey);
	}

	/**
	 * 更新任务表达式
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	public static void rescheduleJob(ScheduleJob scheduleJob) throws SchedulerException {
		TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
		// 获取trigger,即在spring配置文件中定义的 bean id="myTrigger"
		CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
		// 表达式调度构建器
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
		// 按新的cronExpression表达式重新构建trigger
		trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
		// 按新的trigger重新设置job执行
		scheduler.rescheduleJob(triggerKey, trigger);
	}

大概也就这些方法  其余的可以看项目中的配置,主要看ScheduleJobMain类。

项目是maven结构。


打赏

文件名:dynamic-job.zip,文件大小:11.16K 下载
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友