分享Spring Scheduled定时器的用法
/*
*文件名:ITimeFlickerHandler.java
*版权:王安琪
*描述:时钟消息处理接口
*修改时间:2014-01-13
*修改内容:新增
*/ public interface ITimeFlickerHandler { void handle();
}
/*
*文件名:TimeFlicker.java
*版权:王安琪
*描述:时钟触发源
*修改时间:2014-01-13
*修改内容:新增
*/ import java.util.ArrayList;
import java.util.List; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled; /**
* 时钟触发源
*
* @author wanganqi
*
*/
public class TimeFlicker { private static final Logger logger = LoggerFactory
.getLogger(LocatingSourceFilter.class); private List<ITimeFlickerHandler> handlers = new ArrayList<ITimeFlickerHandler>(); /**
* 通过配置文件获取要调用的时钟消息处理类
*
* @return
*/
public List<ITimeFlickerHandler> getHandlers() {
return handlers;
} public void setHandlers(List<ITimeFlickerHandler> handlers) {
this.handlers = handlers;
} // 每一个小时30分00秒执行处理
@Scheduled(cron = "0 30 * * * ?")
public void run() {
for (ITimeFlickerHandler handler : handlers) {
handler.handle();
}
}
}
/*
*文件名:Monitor.java
*版权:王安琪
*描述:监控处理
*修改时间:2013-12-10
*修改内容:新增
*/ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class Monitor implements ITimeFlickerHandler { private static final Logger logger = LoggerFactory
.getLogger(LocatingSourceFilter.class); /**
* 定时时间到,更新所有电子围栏缓存
*
* @see ITimeFlickerHandler#handle()
*/
@Override
public void handle() {
EF[] res = null;
try {
res = service.GetEF();
} catch (Exception e) {
logger.error("This is error message");
}
EFB.setBuffer(res);
}
}
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd "> <!-- 处理器相关 -->
<task:annotation-driven />
<bean id="Monitor"
class="com.imp.Monitor" />
<bean id="TimeFlicker" class="com.imp.TimeFlicker">
<property name="handlers">
<list>
<ref bean="Monitor" />
</list>
</property>
</bean>
</beans>
五、
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; public class AppContext { private static AbstractApplicationContext dispatchContext; /**
* 以单例模式获取上下文
*
* @return 上下文
*/
public static AbstractApplicationContext getDispatchContext() {
if (dispatchContext == null)
dispatchContext = new FileSystemXmlApplicationContext(
"classpath:bean.xml");
return dispatchContext;
}
}
如有引用,请注明来自http://www.cnblogs.com/wgp13x/ ,另外推荐一篇我刚写的博客:
如何写一篇论文,<一种低耗能的数据融合隐私保护算法ESPART> (计算机学报2011-5,王安琪)
分享Spring Scheduled定时器的用法的更多相关文章
- Spring Boot使用@Scheduled定时器任务
摘要: Spring Boot之使用@Scheduled定时器任务 假设我们已经搭建好了一个基于Spring Boot项目,首先我们要在Application中设置启用定时任务功能@EnableS ...
- Spring Boot 使用@Scheduled定时器任务
摘要: Spring Boot之使用@Scheduled定时器任务 假设我们已经搭建好了一个基于Spring Boot项目,首先我们要在Application中设置启用定时任务功能@EnableSch ...
- Spring @Scheduled应用解析
最近,遇到的一个需求,需要执行定时任务,每个一定时间需要执行某个方法 因为项目是SpringMVC的项目,所以使用的是Spring @Scheduled(由于quartz应用起来太麻烦,所以没有采用) ...
- 使用轻量级Spring @Scheduled注解执行定时任务
WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...
- Spring任务调度定时器
1.在spring-context.xml配置 <!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ...
- javascript中window与document对象、setInterval与setTimeout定时器的用法与区别
一.写在前面 本人前端菜鸟一枚,学习前端不久,学习过程中有很多概念.定义在使用时容易混淆,在此给向我一样刚踏入前端之门的童鞋们归纳一下.今天给大家分享一下js中window与document对象.se ...
- spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务 >>>>>>>>>>>> ...
- Spring MVC RedirectAttributes的用法解决办法
Spring MVC RedirectAttributes的用法很久没发过技术贴了,今天对于一个问题纠结了2小时,遂放弃研究用另一种方法解决,奈何心中一直存在纠结,发帖求解 我先解释下什么是Redir ...
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
随机推荐
- HTML5离线资源缓存简介
cache manifest 示例 要使用离线资源缓存,开发者首先要提供一个 cache manifest 文件 它列出了所有需要在离线状态下使用的资源,浏览器会把这些资源缓存到本地 下面就是一个 c ...
- python 正则表达式应用——缩写词扩充
看具体示例 import re def expand_abbr(sen, abbr): lenabbr = len(abbr) ma = '' for i in range(0, lenabbr): ...
- TCP/IP协议簇分层详解---转
http://blog.csdn.net/hankscpp/article/details/8611229 一. TCP/IP 和 ISO/OSI ISO/OSI模型,即开放式通信系统互联参考模型(O ...
- Codeforces 809D. Hitchhiking in the Baltic States
Description 给出 \(n\) 个数 \(a_i\),每一个数有一个取值 \([l_i,r_i]\) ,你来确定每一个数,使得 \(LIS\) 最大 题面 Solution 按照平时做法,设 ...
- [APIO2018] Circle selection 选圆圈
Description 给出 \(n\) 个圆 \((x_i,y_i,r_i)\) 每次重复以下步骤: 找出半径最大的圆,并删除与这个圆相交的圆 求出每一个圆是被哪个圆删除的 Solution \(k ...
- .NET的EF框架中:在应用程序配置文件中找不到名为“”的连接字符串问题
今天在使用EF Code First框架时,当把模型都定义好了,想通过程序包管理控制台利用enable-migrations –force来生成数据库表的时候报错了,如下: 找不到连接字符串,但是我仔 ...
- Go.基础篇-1
package main import "fmt" import "math" import "errors" func main(){ f ...
- python os.popen 乱码问题
os.popen('ipconfig') 命令返回的结果在调试时乱码了: output1 = os.popen('ipconfig') o1=output1.read() 我猜这里输出的内容要和控制台 ...
- Tomcat添加用户
在conf目录下tomcat-users.xml文件里添加如下代码: <role rolename="manager-gui"/> <user username= ...
- 高德地图 API JavaScript API
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm12.aspx ...