使用轻量级Spring @Scheduled注解执行定时任务
WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了定时任务。在这里做个备注。
spring配置文件 xmlns中加入一段:
xmlns:task="http://www.springframework.org/schema/task"
然后xsi:schemaLocation多加下面的内容:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
配置文件中启动TASK注解:
<task:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="com.xx.xx"/>
编写JAVA代码:
@Component
public class ScheduledTaskManager {
/**
* cron表达式:* * * * * *(共6位,使用空格隔开,具体如下)
* cron表达式:*(秒0-59) *(分钟0-59) *(小时0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT)
*/ /**
* 定时卡点计算。每天凌晨 02:00 执行一次
*/
@Scheduled(cron = "0 0 2 * * *")
public void autoCardCalculate() {
System.out.println("定时卡点计算... " + new Date());
} /**
* 心跳更新。启动时执行一次,之后每隔1分钟执行一次
*/
@Scheduled(fixedRate = 1000*60*1)
public void heartbeat() {
System.out.println("心跳更新... " + new Date());
} }
@Scheduled有两种方式:cron表达式和fixedRate
两种都可以定时每隔一段时间执行,个人觉得两种方式区别是fixedTate方式是应用启动时候会先执行一次,cron表达式能配置更加复杂的情况。
个人使用中出现了2个问题:
1.项目启动时候会报出No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined异常信息:
解决:这个错误问题其实是debug级别输出,就是不影响定时任务的使用,在网上看到别人有这样解释
Spring的定时任务调度器会尝试获取一个注册过的 task scheduler来做任务调度,它会尝试通过BeanFactory.getBean的方法来获取一个注册过的scheduler bean,获取的步骤如下:
a.尝试从配置中找到一个TaskScheduler Bean
b.寻找ScheduledExecutorService Bean
c.使用默认的scheduler
前两步,如果找不到的话,就会以debug的方式抛出异常,
分别是: logger.debug("Could not find default TaskScheduler bean", ex);
logger.debug("Could not find default ScheduledExecutorService bean", ex);
所以,日志中打印出来的两个异常,根本不是什么错误信息,也不会影响定时器的使用,只不过是spring的自己打印的一些信息罢了
如果想去掉这个输出,可以在log4j配置文件中增加一行即可:
log4j.logger.org.springframework.scheduling = INFO
2.发现定时任务每次执行都会执行两次
这个问题后来发现是由于Spring的配置文件被加载了两次造成的,listener和DispatcherServlet都会初始化spring配置文件,所有注释掉listener即可
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/application-context.xml</param-value>
</context-param> <!-- Spring监听器 -->
<!--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
--> <!-- servlet设置,将所有请求接到org.springframework.web.servlet.DispatcherServlet进行处理 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/application-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
使用轻量级Spring @Scheduled注解执行定时任务的更多相关文章
- spring @Scheduled注解执行定时任务
以前框架使用quartz框架执行定时调度问题. 这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的 ...
- quartz 框架定时任务,使用spring @Scheduled注解执行定时任务
配置quartz 在spring中需要三个jar包: quartz-1.6.5.jar.commons-collections-3.2.jar.commons-logging-1.1.jar 首先要配 ...
- 使用spring @Scheduled注解执行定时任务
以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用 ...
- 【转】使用spring @Scheduled注解执行定时任务
http://blog.csdn.net/sd4000784/article/details/7745947 以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在s ...
- 使用spring @Scheduled注解执行定时任务、
http://blog.csdn.net/sd4000784/article/details/7745947,留下来备用.
- 使用spring @Scheduled注解运行定时任务、
曾经框架使用quartz框架运行定时调度问题. 老大说这配置太麻烦.每一个调度都须要多加在spring的配置中. 能不能降低配置的量从而提高开发效率. 近期看了看spring的 scheduled的使 ...
- 使用spring的@Scheduled注解执行定时任务,启动项目不输出警告
在applicationContext.xml中添加: xmlns:task="http://www.springframework.org/schema/task" xsi:sc ...
- 定时任务 spring @Scheduled注解
使用spring @Scheduled注解执行定时任务: 运行!!! 关于Cron表达式(转载) 表达式网站生成: http://cron.qqe2.com/ 直接点击 cronExpression ...
- Spring Boot 使用 @Scheduled 注解创建定时任务
在项目开发中我们经常需要一些定时任务来处理一些特殊的任务,比如定时检查订单的状态.定时同步数据等等. 在 Spring Boot 中使用 @Scheduled 注解创建定时任务非常简单,只需要两步操作 ...
随机推荐
- DOS命令生成文件列表
DOS命令窗口,生成文件列表命令格式:dir xmlFiles /b >list.txt dir 文件列表相关命令 xmlFiles 待生成文件所在文件夹,在dos命令窗口根目录下,省略前缀.别 ...
- iOS开发——网络编程Swift篇&(四)异步Get方式
异步Get方式 // MARK: - 异步Get方式 func asynchronousGet() { //创建NSURL对象 var url:NSURL! = NSURL(string: " ...
- Python学习 之 OS模块
1.目录操作 import os os.mkdir('abc') #创建abc文件 tree a #查看目录结构 2.目录遍历 方式一:递归 import os def dirList(path ...
- "jobTracker is not yet running"(hadoop 配置)
今天自己尝试做配置了一下hadoop,环境是ubuntu13.10+jdk1.7.0_51+hadoop version1.2.1. 主要过程主要参考http://blog.csdn.net/hitw ...
- C#综合揭秘——Entity Framework 并发处理详解
引言 在软件开发过程中,并发控制是确保及时纠正由并发操作导致的错误的一种机制.从 ADO.NET 到 LINQ to SQL 再到如今的 ADO.NET Entity Framework,.NET 都 ...
- php对象当参数传递 && php深复制和浅复制
把对象当参数传递给方法,在方法里改过对象后,影响到外面的对象 因为对象是引用传递过去的 class Book { public $name; public function __construct( ...
- Helpers\TableBuilder
Helpers\TableBuilder Table builder helper is a class that would help you to create tables in MySQL ( ...
- App安全之网络传输安全
移动端App安全如果按CS结构来划分的话,主要涉及客户端本身数据安全,Client到Server网络传输的安全,客户端本身安全又包括代码安全和数据存储安全.所以当我们谈论App安全问题的时候一般来说在 ...
- css笔记09:选择器优先级
1. (1) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- struts2.1笔记02:servlet简介
1. Servlet 是在服务器上运行的小程序.这个词是在 Java applet的环境中创造的,Java applet 是一种当作单独文件跟网页一起发送的小程序,它通常用于在客户端运行,结果 ...