spring task的详细使用

来源:互联网 发布:为知笔记 微博收藏 编辑:程序博客网 时间:2024/06/13 22:20

定时任务是解决很多问题的常用手段,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;@Componentpublic 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表达式

第二种配置方法,主要基于注解

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;@Componentpublic 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@EnableSchedulingpublic class TaskTest {    @Scheduled(cron="* * * * * ?")    public void execute(){        System.out.println("let's go on " + new SimpleDateFormat().format(new Date()).toString());    }}

以@EnableScheduling注解。

使用

只要加载spring容器就能启动定时任务了。

这里写图片描述
这里写图片描述

原创粉丝点击