在网上看了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; } @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { // TODO Auto-generated method stub this.ctx=arg0; } }
这样当数据库里面没有动态定时任务时,每隔10秒钟扫描一次。当动态定时任务触发后,自动更新下一次任务的时间。
重点是在触发以后的类里面,获取scheduler。