分享Spring Scheduled定时器的用法
1 /* 2 *文件名:ITimeFlickerHandler.java 3 *版权:王安琪 4 *描述:时钟消息处理接口 5 *修改时间:2014-01-13 6 *修改内容:新增 7 */ 8 9 public interface ITimeFlickerHandler { 10 11 void handle(); 12 }
1 /* 2 *文件名:TimeFlicker.java 3 *版权:王安琪 4 *描述:时钟触发源 5 *修改时间:2014-01-13 6 *修改内容:新增 7 */ 8 9 import java.util.ArrayList; 10 import java.util.List; 11 12 import org.slf4j.Logger; 13 import org.slf4j.LoggerFactory; 14 import org.springframework.scheduling.annotation.Scheduled; 15 16 /** 17 * 时钟触发源 18 * 19 * @author wanganqi 20 * 21 */ 22 public class TimeFlicker { 23 24 private static final Logger logger = LoggerFactory 25 .getLogger(LocatingSourceFilter.class); 26 27 private List<ITimeFlickerHandler> handlers = new ArrayList<ITimeFlickerHandler>(); 28 29 /** 30 * 通过配置文件获取要调用的时钟消息处理类 31 * 32 * @return 33 */ 34 public List<ITimeFlickerHandler> getHandlers() { 35 return handlers; 36 } 37 38 public void setHandlers(List<ITimeFlickerHandler> handlers) { 39 this.handlers = handlers; 40 } 41 42 // 每一个小时30分00秒执行处理 43 @Scheduled(cron = "0 30 * * * ?") 44 public void run() { 45 for (ITimeFlickerHandler handler : handlers) { 46 handler.handle(); 47 } 48 } 49 }
1 /* 2 *文件名:Monitor.java 3 *版权:王安琪 4 *描述:监控处理 5 *修改时间:2013-12-10 6 *修改内容:新增 7 */ 8 9 import org.slf4j.Logger; 10 import org.slf4j.LoggerFactory; 11 12 public class Monitor implements ITimeFlickerHandler { 13 14 private static final Logger logger = LoggerFactory 15 .getLogger(LocatingSourceFilter.class); 16 17 /** 18 * 定时时间到,更新所有电子围栏缓存 19 * 20 * @see ITimeFlickerHandler#handle() 21 */ 22 @Override 23 public void handle() { 24 EF[] res = null; 25 try { 26 res = service.GetEF(); 27 } catch (Exception e) { 28 logger.error("This is error message"); 29 } 30 EFB.setBuffer(res); 31 } 32 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:task="http://www.springframework.org/schema/task" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 6 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd "> 7 8 <!-- 处理器相关 --> 9 <task:annotation-driven /> 10 <bean id="Monitor" 11 class="com.imp.Monitor" /> 12 <bean id="TimeFlicker" class="com.imp.TimeFlicker"> 13 <property name="handlers"> 14 <list> 15 <ref bean="Monitor" /> 16 </list> 17 </property> 18 </bean> 19 </beans>
五、
1 import org.springframework.context.support.AbstractApplicationContext; 2 import org.springframework.context.support.FileSystemXmlApplicationContext; 3 4 public class AppContext { 5 6 private static AbstractApplicationContext dispatchContext; 7 8 /** 9 * 以单例模式获取上下文 10 * 11 * @return 上下文 12 */ 13 public static AbstractApplicationContext getDispatchContext() { 14 if (dispatchContext == null) 15 dispatchContext = new FileSystemXmlApplicationContext( 16 "classpath:bean.xml"); 17 return dispatchContext; 18 } 19 }
如有引用,请注明来自 http://www.cnblogs.com/wgp13x/ ,另外推荐一篇我刚写的博客:
如何写一篇论文,<一种低耗能的数据融合隐私保护算法ESPART> (计算机学报2011-5,王安琪)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。