Spring Task定时任务

Spring Task是Spring3.0以后提供的定时任务工具。使用Spring定时任务,除Spring相关的包外不需要添加额外的jar包,支持注解和xml配置两种形式。

准备工作:在Spring配置文件头中添加命名空间xmlns:task及描述。

 <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-3.2.xsd
         http://www.springframework.org/schema/task 
         http://www.springframework.org/schema/task/spring-task.xsd">

一.注解形式

1.自动扫描

1.1编写注解的定时任务类

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Auther: DELL-3020
 * @Date: 2018/8/22 10:12
 * @Description:
 */
@Component
public class TaskDemo {

    @Scheduled(fixedRate = 60000)
    public void test() throws InterruptedException {
        System.out.println("test1:" + Thread.currentThread());
        Thread.sleep(100000);
    }

    @Scheduled(fixedRate = 60000)
    public void test2() {
        System.out.println("test2:" + Thread.currentThread());
    }
}

 

1.2在spring配置文件中启动注解

    <!--自动扫描定时任务类所在的包-->
    <context:component-scan base-package="cn.hc.erp.base.timer"/>
    <task:annotation-driven/>
<task:annotation-driven/>可以用
<task:scheduler id="myScheduler"/>
<task:annotation-driven scheduler="myScheduler" mode="proxy"/>替代。在<task:scheduler>中可以配置线程池

2.xml配置扫描

2.1编写定时任务类

import org.springframework.scheduling.annotation.Scheduled;

/**
 * @Auther: DELL-3020
 * @Date: 2018/8/22 11:33
 * @Description:
 */
public class TaskDemo2 {

    @Scheduled(fixedRate = 60000)
    public void test1() {
        System.out.println("task1:" + Thread.currentThread());
    }



    @Scheduled(fixedRate = 60000)
    public void test2() {
        System.out.println("task2:" + Thread.currentThread());
    }
}

2.2在spring配置文件中配置开启定时任务

    <bean id="task" class="cn.hc.erp.base.timer.TaskDemo2"/>
    <!--<task:annotation-driven/>-->
    <task:scheduler id="myScheduler"/>
    <task:annotation-driven scheduler="myScheduler" mode="proxy"/>

使用<task:annotation-driven/>可以将后面两行注释掉。

二.xml配置

1.在xml中配置定时任务

1.1编写要启动的定时任务类

public class TaskDemo3 {
    
    public void test1() {
        System.out.println("taskDemo1:" + Thread.currentThread());
    }

    public void test2() {
        System.out.println("taskDemo2:" + Thread.currentThread());
    }
}

1.2在spring配置文件中配置定时任务

    <bean id="taskDemo3" class="cn.hc.erp.base.timer.TaskDemo3"/>
    <task:scheduled-tasks>
        <task:scheduled ref="taskDemo3" method="test1" fixed-rate="1000"/>
        <task:scheduled ref="taskDemo3" method="test2" fixed-rate="1000"/>
    </task:scheduled-tasks>

三.其他说明

1.spring定时任务默认是串行的,阻塞式的,默认只开启一个线程。

taskDemo1:Thread[pool-1-thread-1,5,RMI Runtime]
taskDemo2:Thread[pool-1-thread-1,5,RMI Runtime]
taskDemo1:Thread[pool-1-thread-1,5,RMI Runtime]
taskDemo2:Thread[pool-1-thread-1,5,RMI Runtime]
taskDemo1:Thread[pool-1-thread-1,5,RMI Runtime]

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值