本篇將介紹Spring3.0以后自主開發的定時任務工具,spring task,可以將它比作一個輕量級的Quartz,而且使用起來很簡單,除spring相關的包外不需要額外的包,而且支持注解和配置文件兩種形式,下面將分別介紹這兩種方式。
一、第一種:配置文件方式
第一步:編寫作業類
即普通的pojo,如下:
import org.springframework.stereotype.Service; @Service public class TaskJob { public void job1() { System.out.println(“任務進行中。。。”); } }
第二步:spring配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" ...... xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:scheduled-tasks> <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/> </task:scheduled-tasks> <context:component-scan base-package=" com.gy.mytask " /> </beans>
說明:ref參數指定的即任務類,method指定的即需要運行的方法,cron及cronExpression表達式。<context:component-scan base-package="com.gy.mytask" />這個配置不多說了,spring掃描注解用的。
二、第二種:使用注解形式
也許我們不想每寫一個任務類還要在xml文件中配置下,我們可以使用注解@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 org.springframework.stereotype.Component; @Component(“taskJob”) public class TaskJob { @Scheduled(cron = "0 0 3 * * ?") public void job1() { System.out.println(“任務進行中。。。”); } }
第二步:添加task相關的配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" default-lazy-init="false"> <context:annotation-config /> <!—spring掃描注解的配置 —> <context:component-scan base-package="com.gy.mytask" /> <!—開啟這個配置,spring才能識別@Scheduled注解 —> <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> <task:scheduler id="qbScheduler" pool-size="10"/>
說明:理論上只需要加上<task:annotation-driven />這句配置就可以了,這些參數都不是必須的。
配置完畢,spring task還有很多參數,具體參考xsd文檔 http://www.springframework.org/schema/task/spring-task-3.0.xsd
參考文章:https://www.cnblogs.com/hongwz/p/5642497.html