spring定时任务的用法

Spring Task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种.

XML配置文件方式

编写作业类

就是即普通的Java类,如下,
* 定时任务1


import java.util.Date;
public class TaskJob {

    public void myMethod() {
        System.out.println(new Date().toLocaleString() + " 我是后台任务1...");
    }
} 
  • 定时任务2
定时任务2
import java.util.Date;
public class TaskJob2 {

    public void myMethod() {
        System.out.println(new Date().toLocaleString() + " 我是后台任务2...");
    }
}
在spring配置文件头中添加命名空间及描述
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="  
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/beans        
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context                
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>
spring配置文件中设置具体的任务
<!-- 定时任务要执行的方法 -->
<bean id="taskJob" class="com.task.TaskJob" />
<bean id="taskJob2" class="com.task.TaskJob2" />

<!--用于定时任务的执行 -->
<task:scheduled-tasks>
    <!-- 指定要运行的类的方法,每2秒执行一次 -->
    <task:scheduled ref="taskJob" method="myMethod" cron=" 0/2 * * * * ?" />
    <!-- 指定要运行的类的方法,每2秒执行一次 -->
    <task:scheduled ref="taskJob2" method="myMethod" cron=" 0/3 * * * * ?" />
</task:scheduled-tasks>

注解的方式

注解@Scheduled的定义
@Target({java.lang.annotation.ElementType.METHOD,java.lang.annotation.ElementType.ANNOTATION_TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface Scheduled  
{  
  public abstract String cron();  

  public abstract long fixedDelay();  

  public abstract long fixedRate();  
} 

可以看出该注解有三个方法或者叫参数,分别表示的意思是:
* cron:指定cron表达式
* fixedDelay:官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
* fixedRate:官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

编写pojo
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component("taskJob")
public class TaskJob {

    @Scheduled(cron = " 0/2 * * * * ?") // 每两秒执行一次
    public void myMethod() {
        System.out.println(new Date().toLocaleString() + " 我是基于注解的后台任务...");
    }
}
添加task相关的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="  
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/beans        
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context                
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />
    <!-- spring扫描注解的配置 -->
    <context:component-scan base-package="com.antation" />

    <!-- 开启这个配置,spring才能识别@Scheduled注解 -->
    <task:annotation-driven scheduler="qbScheduler" mode="proxy" />
    <task:scheduler id="qbScheduler" pool-size="10" />

</beans>

说明:理论上只需要加上这句配置就可以了,这些参数都不是必须的。

测试代码
  • Java程序
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnationTaskTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext-task.xml");

        try {
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
  • 运行结果
2017-6-18 20:19:40 我是基于注解的后台任务...
2017-6-18 20:19:42 我是基于注解的后台任务...
2017-6-18 20:19:44 我是基于注解的后台任务...

一些常见的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触发
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值