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注解执行定时任务的更多相关文章

  1. spring @Scheduled注解执行定时任务

    以前框架使用quartz框架执行定时调度问题. 这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的 ...

  2. quartz 框架定时任务,使用spring @Scheduled注解执行定时任务

    配置quartz 在spring中需要三个jar包: quartz-1.6.5.jar.commons-collections-3.2.jar.commons-logging-1.1.jar 首先要配 ...

  3. 使用spring @Scheduled注解执行定时任务

    以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用 ...

  4. 【转】使用spring @Scheduled注解执行定时任务

    http://blog.csdn.net/sd4000784/article/details/7745947 以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在s ...

  5. 使用spring @Scheduled注解执行定时任务、

    http://blog.csdn.net/sd4000784/article/details/7745947,留下来备用.

  6. 使用spring @Scheduled注解运行定时任务、

    曾经框架使用quartz框架运行定时调度问题. 老大说这配置太麻烦.每一个调度都须要多加在spring的配置中. 能不能降低配置的量从而提高开发效率. 近期看了看spring的 scheduled的使 ...

  7. 使用spring的@Scheduled注解执行定时任务,启动项目不输出警告

    在applicationContext.xml中添加: xmlns:task="http://www.springframework.org/schema/task" xsi:sc ...

  8. 定时任务 spring @Scheduled注解

    使用spring @Scheduled注解执行定时任务: 运行!!! 关于Cron表达式(转载) 表达式网站生成: http://cron.qqe2.com/  直接点击 cronExpression ...

  9. Spring Boot 使用 @Scheduled 注解创建定时任务

    在项目开发中我们经常需要一些定时任务来处理一些特殊的任务,比如定时检查订单的状态.定时同步数据等等. 在 Spring Boot 中使用 @Scheduled 注解创建定时任务非常简单,只需要两步操作 ...

随机推荐

  1. MKMapView的内存释放问题

    MKMapView的内存释放问题 by 伍雪颖 - (void)dealloc { self.mapView.showsUserLocation = NO; self.mapView.userTrac ...

  2. TOJ 4325 RMQ with Shifts / 线段树单点更新

    RMQ with Shifts 时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte 描述 In the traditional RMQ (Range M ...

  3. Java中Queue类实现

    原先在java编程中,Queue的实现都是用LinkedList Queue queue = new LinkedList(); 但正如jdk中所说的那样: 注意,此实现不是同步的.如果多个线程同时访 ...

  4. 分享php中四种webservice实现的简单架构方法及实例(转)

    本人所了解的webservice有以下几种:PHP本身的SOAP,开源的NUSOAP,商业版的PHPRPC,以及使用二进制传输数据流的 HessianPHP,那么一下就简单的介绍下这几种webserv ...

  5. ABAP FIELD-SYMBOLS 有大作用- 将没有可改参数的增强出口变得也能改主程序的值了

    看下图代码: report  z_xul_test2 中 定义了 全局变量 G_DATA1 , 分别调用了 z_xul_tes1 中的 form  和 function zbapi_test , 这两 ...

  6. F5 负载均衡

    http://xjsunjie.blog.51cto.com/blog/999372/697285 http://www.eimhe.com/thread-142659-1-1.html

  7. shared_ptr的简单实现

    前面讲到auto_ptr有个很大的缺陷就是所有权的转移,就是一个对象的内存块只能被一个智能指针对象所拥有.但我们有些时候希望共用那个内存块.于是C++ 11标准中有了shared_ptr这样的智能指针 ...

  8. 关于MIUI悬浮窗权限问题的解决方案

    先扯会....好久没写Blog了....这段时间有点小忙...瞎忙.....忙的自己都感觉都不应该.....严重影响了生活质量......生活的幸福指数!!!.....到现在还特么的单身!!!求介绍啊 ...

  9. 用对象型泛型和ArraysList写一个输入学员信息并展示

    题目:录入学员信息并保存,当录入学员的编号为0时结束,展示出学员信息 //student类 public class Student { public int id; public String na ...

  10. hibernate 入门案例

    1,创建工程,导入jar ojdbc14.jar 数据库驱动包,我用的是oracle数据库,根据实际的数据库选择驱动包 创建java类,并利用bibernate插件完成orm映射,创建hbm.xml ...