zoukankan      html  css  js  c++  java
  • 在Spring3中使用注解(@Scheduled)创建计划任务

    Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了:

    1. 创建一个Java类,添加一个无参无返回值的方法,在方法上用@Scheduled注解修饰一下;
    2. 在Spring配置文件中添加三个<task:**** />节点;

    最后说明一下,第一步创建的Java类要成为Spring可管理的Bean,可以直接写在XML里,也可以@Component一下

    计划任务类:

    /**
     * com.zywang.spring.task.SpringTaskDemo.java
     * @author ZYWANG 2011-3-9
     */
    package com.zywang.spring.task;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    /**
     * Spring3 @Scheduled 演示
     * @author ZYWANG 2011-3-9Spring配置文件:
     */
    @Component
    public class SpringTaskDemo {
    
        @Scheduled(fixedDelay = 5000)
        void doSomethingWithDelay(){
            System.out.println("I'm doing with delay now!");
        }
        
        @Scheduled(fixedRate = 5000)
        void doSomethingWithRate(){
            System.out.println("I'm doing with rate now!");
        }
        
        @Scheduled(cron = "0/5 * * * * *")
        void doSomethingWith(){
            System.out.println("I'm doing with cron now!");
        }
    }

    Spring配置文件:

    <?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.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
        <!-- Enables the Spring Task @Scheduled programming model -->
        <task:executor id="executor" pool-size="5" />
        <task:scheduler id="scheduler" pool-size="10" />
        <task:annotation-driven executor="executor" scheduler="scheduler" />
    </beans>

    需要注意的几点:

    1、spring的@Scheduled注解  需要写在实现上、

    2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、具体就去百度google吧)

    3、实现类上要有组件的注解@Component

    剩下的就是corn表达式了、具体使用以及参数请百度google、

    【秒】   【分】  【时】   【日】  【月】   【周】  【年】   

    下面只例出几个式子

    CRON表达式    含义 
    "0 0 12 * * ?"    每天中午十二点触发 
    "0 15 10 ? * *"    每天早上10:15触发 
    "0 15 10 * * ?"    每天早上10:15触发 
    "0 15 10 * * ? *"    每天早上10:15触发 
    "0 15 10 * * ? 2005"    2005年的每天早上10:15触发 
    "0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发 
    "0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 
    "0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
    "0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 
    "0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 
    "0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发 

    Scheduled类的可配置属性项:

    String cron
    A cron-like expression, extending the usual UN*X definition to include triggers on the second as well as minute, hour, day of month, month and day of week.
    long fixedDelay
    Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
    String fixedDelayString
    Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
    long fixedRate
    Execute the annotated method with a fixed period between invocations.
    String fixedRateString
    Execute the annotated method with a fixed period between invocations.
    long initialDelay
    Number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.
    String initialDelayString
    Number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.
    String zone
    A time zone for which the cron expression will be resolved.

    以上内容基于Spring 3.0.5 版本运行,参考文档为spring-framework-reference-3.0.5.pdf

  • 相关阅读:
    rest-framework框架——解析器、ur控制、分页、响应器、渲染器
    rest-framework框架——认证、权限、频率组件
    rest-framework框架——视图三部曲
    rest-framework框架——APIView和序列化组件
    Django——CBV与FBV
    CRM——权限
    CRM——销售与客户
    CRM——讲师与学生
    CRM——起步
    jsp标签
  • 原文地址:https://www.cnblogs.com/longshiyVip/p/5585689.html
Copyright © 2011-2022 走看看