分享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 ...
随机推荐
- mysql修改表名
mysql修改表名 SQL语句为: alter table table_name rename to new_table_name 参考:http://blog.csdn.net/xrt95050/a ...
- JavaScript数组forEach循环
JavaScript数组forEach循环 今天写JavaScript代码把forEach循环数组忘记写法了,在此记录一下以防止未来忘记. let a = [1, 2, 3]; a.forEach(f ...
- JSP中实现网页访问统计的方法【转】
我采用的是jsp网页,但是不管采用什么语言,原理是一样的. 第一种,单页面统计.就是说,只要点击这个页面就会统计一次. <body> <%!//在这种标记中定义的变量为全局变量 in ...
- C++公有继承、保护继承和私有继承
C++中的继承方式有: public.private.protected三种(它们直接影响到派生类的成员.及其对象对基类成员访问的规则). (1)public(公有继承):继承时保持基类中各成员属性不 ...
- java代码行数统计工具类
package com.syl.demo.test; import java.io.*; /** * java代码行数统计工具类 * Created by 孙义朗 on 2017/11/17 0017 ...
- [转]【Oracle Database 12c新特性】32k varchar2 max_string_size
本文转自:https://blogs.oracle.com/askmaclean/entry/oracle_database_12c%E6%96%B0%E7%89%B9%E6%80%A7_32k_va ...
- .NET 中使用阿里云短信的 API 接口
小弟初来乍到,这也是我的第一篇文章,写的不好的地方还望指正.谢谢各位! 引言 短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力,支持快速发送短信验证码.短信 ...
- Springmvc file多附件上传 显示 删除操作
之前项目需求要做一个多附件上传 并显示上传文件 带删除操作 一筹莫展之际搜到某个兄弟发的博客感觉非常好用被我copy下来了此贴算是改良版 再次感谢(忘记叫什么了时间也有点久没有历史记录了)先上图 基于 ...
- django基础一:web、wsgi、mvc、mtv
一.web框架 web框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以快速开发特定的系统.他山之石,可以攻玉.python的所有web框架,都是对so ...
- centos自带python2.6升级到python2.7。并解决yum pip easy_install pip等模块兼容性问题
参考原文: https://www.cnblogs.com/kimyeee/p/7250560.html https://www.cnblogs.com/galaxy-gao/p/5796488 ...