Spring - Task定时任务

一、说明

    Spring 3.0以后自带的Spring Task,是一个轻量级的定时任务工具,而且使用起来很简单,除spring相关的包外不需要额外的包,支持注解和配置文件两种形式


二、使用

    1、配置方式
        1)创建Job作业

    package com.xl.task;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class SimpleCfgJob {
    
        public void doJob() throws Exception {
    
            System.out.println("Job start...");
    
        }
    }
        2)配置Cfg文件
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">  
    
        <!-- 任务列表 -->
        <task:scheduled-tasks>   
             
            <!-- 时间间隔的属性配置,三必选一:-->
            <!--    fixed-delay: 上一个任务完成到下一个任务开始的间隔(毫秒);  fixed-rate: 上一个任务开始到下一个任务开始的间隔(毫秒);  cron: Cron表达式(秒 分 时 日 月 周几 [年]) -->
            <task:scheduled ref="simpleCfgJob" method="doJob" fixed-rate="5000"/>
               
        </task:scheduled-tasks>  
        
        <!-- 注解扫描 -->
        <context:component-scan base-package="com.xl.task" /> 
     
    </beans>    
    2、注解方式
        1)创建Job作业

    package com.xl.task;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class SimpleAnnJob {
    
        @Scheduled(fixedRate = 5000)  //注解任务属性
        public void doJob() throws Exception {
            
            System.out.println("Job start...");
    
        }
    }
        2)配置Cfg文件
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
    
        <!-- Spring扫描注解的配置 -->
        <context:component-scan base-package="com.xl.task" />
    
        <!-- 开启这个配置, Spring才能识别@Scheduled注解 -->
        <task:annotation-driven />  
     
    </beans>













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值