quartz+spring框架动态调整频率实践
生活随笔
收集整理的這篇文章主要介紹了
quartz+spring框架动态调整频率实践
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
?spring環境:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- *******************************cronJob start******************************* --><bean id="cronJobDetail"class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><!-- 指定具體的job類 --><property name="jobClass" value="com.chenjun.quartz_1.springenv.CronJobDetail" /><!-- 指定job的名稱 --><property name="name" value="CRON_JOB" /></bean><bean name="cronJobTrigger"class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="cronJobDetail"></property><!-- 每1秒執行一次 --><property name="cronExpression" value="0/1 * * * * ?" /></bean><!-- *******************************cronJob end******************************* --><bean id="schedulerFactory"class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="cronJobTrigger" /></list></property></bean> </beans>?JOB類
package com.chenjun.quartz_1.springenv;import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean;public class SimpleJob extends QuartzJobBean {@Overrideprotected void executeInternal(JobExecutionContext context) throws JobExecutionException{String message = context.getJobDetail().getJobDataMap().getString("message");System.out.println(message);} }JobDetail類
package com.chenjun.quartz_1.springenv;import java.text.ParseException;import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.CronExpression; import org.quartz.CronTrigger; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.StatefulJob; import org.quartz.impl.StdScheduler;public class CronJobDetail implements StatefulJob {/*** 日志 */private static final Log LOG = LogFactory.getLog(StatefulJob.class);public void execute(JobExecutionContext context) throws JobExecutionException{StdScheduler stdScheduler = (StdScheduler)context.getScheduler();CronTrigger cronTrigger = (CronTrigger)context.getTrigger();System.out.println("JOB執行.."+cronTrigger.getCronExpression());CronTrigger trigger = null;try{trigger = (CronTrigger) stdScheduler.getTrigger("cronJobTrigger", Scheduler.DEFAULT_GROUP);CronTrigger newTriggerIns = new CronTrigger();newTriggerIns.setJobName(cronTrigger.getJobName());newTriggerIns.setName("abc");//模擬從數據庫里面查出的時間表達式newTriggerIns.setCronExpression(new CronExpression("0/5 * * * * ?"));stdScheduler.rescheduleJob("cronJobTrigger", null, newTriggerIns);}catch (SchedulerException e){e.printStackTrace();}catch (ParseException e){e.printStackTrace();}} }?啟動類:
package com.chenjun.quartz_1.springenv;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class SimpleSpringQuartz {public static void main(String[] args){ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");} }結果:
搞定了
轉載于:https://my.oschina.net/u/2338224/blog/968172
總結
以上是生活随笔為你收集整理的quartz+spring框架动态调整频率实践的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centOS6.7 /etc/profi
- 下一篇: NHibernate概括