spring task的详细使用

定时任务是解决很多问题的常用手段,spring的spring task 可以看做一个轻量级的quartz框架。只需要通过少量的配置,就能启动定时任务。

1.引入相关jar包

<dependency>
   <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>

这几个jar是必需的,其中spring-web,可要可不要,在web容器中加载spring,需要spring-web。

2.以下都是基于xml+注解的混合的配置

在spring.xml文件中引入xsi校验。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     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/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task.xsd">
第一种配置方法

编写具体的任务实现类,里面指定一个方法,execute(),名字随便起。

package com.springTask;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Component;
@Component
public class TaskTest {

    public void execute(){
        System.out.println("let's go on " + new SimpleDateFormat().format(new Date()).toString());
    }
}

在spring.xml文件中配置

<context:component-scan base-package="com.springTask"/>

    <task:scheduled-tasks>   
        <task:scheduled ref="taskTest" method="execute" cron="0/1 * * * * ?"/>   
    </task:scheduled-tasks> 

一般常用的都是cron表达式,去控制定时任务执行的时间。

设置运行的参数还有

  • fixed-delay:间隔时间为上一个任务的完成时间
  • fixed-rate:间隔时间为上一个任务的开始时间
  • initial-delay:第一任务的延迟时间
    以上单位都是毫秒

一共6个参数,{秒,分,时,天,月,周}
秒:可使用”, - * /”四个字符,使用数字0-59 表示,

分:可使用”, - * /”四个字符,使用数字0-59 表示,

时 :可使用”, - * /”四个字符,使用数字0-23表示,

天:可使用”, - * / ? L W C”八个字符正常情况下,使用数字1-30或1-31。看当前点具体的月份。

month(月) :可使用”, - * /”四个字符,使用数字0-11 或 ”JAN-DEC“。

Day-of-Week(每周):可使用”, - * / ? L C #”四个字符,使用数字1-7 或 ”MON-FR“

具体用法这里推荐一篇博客

cron表达式

也可以是使用在线cron表达式生成器

第二种配置方法,主要基于注解
package com.springTask;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.config.TaskExecutorFactoryBean;
import org.springframework.stereotype.Component;

@Component
public class TaskTest {

    @Scheduled(cron="* * * * * ?")
    public void execute(){
        System.out.println("let's go on " + new SimpleDateFormat().format(new Date()).toString());
    }


}

xml文件加载task的注解驱动,spring容器在初始化的时候回去寻找带@scheduled注解的bean。将其加入定时任务的线程池中。

第三种方式

spring.xml只扫描bean

<context:component-scan base-package="com.springTask"/>
package com.springTask;

import java.text.SimpleDateFormat;
import java.util.Date;

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

@Component
@EnableScheduling
public class TaskTest {

    @Scheduled(cron="* * * * * ?")
    public void execute(){
        System.out.println("let's go on " + new SimpleDateFormat().format(new Date()).toString());
    }


}

以@EnableScheduling注解。

使用

只要加载spring容器就能启动定时任务了。
在这里插入图片描述在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值