用Springframework實現定時器功能
采用Web自動加載TimerManager來管理Timer鏈,在Class更新服務器熱加載后會發生異常。這要求對TimerManager進行一些特殊的處理才能保證Timer鏈的正確性。
使用Spring framework中提供的TimerTask自動加載功能可以非常容易的實現定時器鏈的管理。同時,采用Spring framework的這一功能可以非常容易的對定時器進行添加、刪除。
1.在Web.xml中申明
2.在schedulingContext-timer.xml描述用戶的定時器
3.編寫workflow.common.MyTimer定時器
這樣就輕松完成了定時器的功能。如果需要修改、增加、刪除定時器,只需要對2、3步的內容進行調整就可以實現。
使用Spring framework中提供的TimerTask自動加載功能可以非常容易的實現定時器鏈的管理。同時,采用Spring framework的這一功能可以非常容易的對定時器進行添加、刪除。
1.在Web.xml中申明
| <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/schedulingContext-timer.xml</param-value> </context-param> <servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>3</load-on-startup> </servlet> |
2.在schedulingContext-timer.xml描述用戶的定時器
| <bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref local="JorwangScheduledTimerTask1"/> </list> </property> </bean> <bean id="JorTimeTask1" class="workflow.common.MyTimer"> </bean> <bean id="JorwangScheduledTimerTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask"><ref bean="JorTimeTask1"/></property> <property name="delay"><value>10000</value></property> <property name="period"><value>86400000</value></property> </bean> |
3.編寫workflow.common.MyTimer定時器
這樣就輕松完成了定時器的功能。如果需要修改、增加、刪除定時器,只需要對2、3步的內容進行調整就可以實現。