spring動態定時器

來源:互聯網
上載者:User

在網上看了http://www.blogjava.net/xiaodaoxiaodao/archive/2007/02/05/103437.html 之類的文章,像留言說的一樣報錯。本文通過改進,在程式裡面擷取Scheduler。

設定檔:

<bean id="schedulerFactory"class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="popupWindowTrigger"/></list></property></bean>      <bean id="popupWindowTrigger" class="xxx.util.InitializingCronTriggerBean">         <property name="jobDetail"><ref bean="popupWindowDetail" /></property><property name="popupWindowManager"><ref bean="popupWindowManager" /></property>     </bean>     <bean id="popupWindowDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">        <property name="targetObject"><ref bean="popupWindowTimerTask" /></property>        <property name="targetMethod"><value>execute</value></property>        <property name="concurrent"><value>false</value></property>    </bean>    <bean id="popupWindowTimerTask" class="xxx.action.PopupWindowTimerTask"><property name="popupWindowManager" ref="popupWindowManager"/>     </bean>

InitializingCronTriggerBean類繼承了CronTriggerBean類。由於我需要在觸發定時器時,處理一些內容,因此添加了額外的屬性TagList。

public class InitializingCronTriggerBean extends CronTriggerBean{protected final  Log log = LogFactory.getLog(InitializingCronTriggerBean.class);PopupWindowManager popupWindowManager;TagList tagList;public PopupWindowManager getPopupWindowManager() {return popupWindowManager;}public void setPopupWindowManager(PopupWindowManager popupWindowManager) throws ParseException {this.popupWindowManager = popupWindowManager;updateCronExpression();}public void updateCronExpression() throws ParseException{tagList= popupWindowManager.getCronExpressionFromDB (); String cronExpression=null;if(tagList!=null){Date updateTime=tagList.getUpdateTime();Calendar calendar=GregorianCalendar.getInstance(); calendar.setTime(updateTime);cronExpression=calendar.get(Calendar.SECOND)+" "+calendar.get(Calendar.MINUTE)+" "+calendar.get(Calendar.HOUR_OF_DAY)+" "+calendar.get(Calendar.DATE)+" "+(calendar.get(Calendar.MONTH)+1)+" ? "+calendar.get(Calendar.YEAR);}if(cronExpression==null)cronExpression="0/10 * * ? * *"; setCronExpression(cronExpression);log.info("時間為:"+cronExpression);}public TagList getTagList() {return tagList;}public void setTagList(TagList tagList) {this.tagList = tagList;}}

PopupWindowTimerTask 類是觸發後的處理類,實現了ApplicationContextAware介面用來捕獲ApplicationContext對象

public class PopupWindowTimerTask implements ApplicationContextAware{protected final  Log log = LogFactory.getLog(HTML5TimerTask.class);PopupWindowManager popupWindowManager;Scheduler scheduler;ApplicationContext ctx;public String execute() throws Exception{log.info("執行定時任務,執行時間="+DateUtils.getDateTime("yyyy-MM-dd HH:mm:ss")+" 任務功能:影音小彈窗!");try{scheduler = (Scheduler) ctx.getBean("schedulerFactory");InitializingCronTriggerBean trigger = (InitializingCronTriggerBean) scheduler.getTrigger("popupWindowTrigger", Scheduler.DEFAULT_GROUP);TagList tl=trigger.getTagList();//要處理的商務邏輯///////////////////////////trigger.updateCronExpression();scheduler.rescheduleJob("popupWindowTrigger", Scheduler.DEFAULT_GROUP, trigger);}catch(Exception e){log.error("擷取配置表中頻道資訊出錯",e);}log.info("定時任務完成,完成時間="+DateUtils.getDateTime("yyyy-MM-dd HH:mm:ss")+" 任務功能:影音小彈窗!");return "success";}public PopupWindowManager getPopupWindowManager() {return popupWindowManager;}public void setPopupWindowManager(PopupWindowManager popupWindowManager) {this.popupWindowManager = popupWindowManager;}public Scheduler getScheduler() {return scheduler;}public void setScheduler(Scheduler scheduler) {this.scheduler = scheduler;}@Overridepublic void setApplicationContext(ApplicationContext arg0)throws BeansException {// TODO Auto-generated method stubthis.ctx=arg0;}}

這樣當資料庫裡面沒有動態定時任務時,每隔10秒鐘掃描一次。當動態定時任務觸發後,自動更新下一次任務的時間。

重點是在觸發以後的類裡面,擷取scheduler。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.