使用轻量级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>
时间: 2024-08-02 15:13:08

使用轻量级Spring @Scheduled注解执行定时任务的相关文章

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

首先要配置我们的spring.xml xmlns 多加下面的内容 1 xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schemaLocation多加下面的内容 1 http://www.springframework.org/schema/task 2 http://www.springframework.org/schema/task/spring-task-3.1.xsd 最后是我们的task任务扫描注解

spring @Scheduled注解执行定时任务

以前框架使用quartz框架执行定时调度问题. 这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的方式进行调度. 感觉很方便.起码配置的东西少了很多. 所以留下来以备忘了. 首先要配置我们的spring.xml xmlns 多加下面的内容. [html] view plain copy xmlns:task="http://www.springframework.org/schema/task&

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

以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的方式进行调度. 感觉很方便.起码配置的东西少了很多. 所以留下来以备忘了. 首先要配置我们的spring.xml xmlns 多加下面的内容. [html] view plaincopyprint? xmlns:task="http://www.springframework.org/sche

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

http://blog.csdn.net/sd4000784/article/details/7745947 以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的方式进行调度. 感觉很方便.起码配置的东西少了很多. 所以留下来以备忘了. 首先要配置我们的spring.xml xmlns 多加下面的内容. [html] view plain co

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

曾经框架使用quartz框架运行定时调度问题. 老大说这配置太麻烦.每一个调度都须要多加在spring的配置中. 能不能降低配置的量从而提高开发效率. 近期看了看spring的 scheduled的使用注解的方式进行调度. 感觉非常方便.起码配置的东西少了非常多. 所以留下来以备忘了. 首先要配置我们的spring.xml xmlns 多加以下的内容. xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schem

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

在applicationContext.xml中添加: xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <task:annotation-

Spring使用注解执行定时任务

一   SpringContext.xml中添加以下配置 1.  beans添加xmlns:task xmlns:task="http://www.springframework.org/schema/task" 2. xsi:schemaLocation中添加 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd 3. 添加be

Spring 的@Scheduled注解实现定时任务执行和调度

首先要配置我们的spring.xml   ---  即spring的主配置文件(有的项目中叫做applicationContext.xml或context.xml) xmlns 多加下面的内容. [html] view plaincopy xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schemaLocation多加下面的内容. [html] view plaincopy http://www.springf

使用spring提供的@Scheduled注解创建定时任务

使用方法 操作非常简单,只要按如下几个步骤配置即可 1. 导入jar包或添加依赖,其实定时任务只需要spring-context即可,当然起服务还需要spring-web: 2. 编写定时任务类和方法,在方法上加@Scheduled注解,注意定时方法不能有返回值,如果采用包扫描方式注册bean,还需要在类上加组件注解: 3. 在spring容器中注册定时任务类: 4. 在spring配置文件中开启定时功能. 示例Demo maven依赖 <dependency> <groupId>