Spring-Task
本文介紹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">
第三步:spring配置文件中設置具體的任務
<task:scheduled-tasks>
<task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>
</task:scheduled-tasks>
<context:component-scan base-package=" com.gy.mytask " />
說明: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.scheduling.annotation.Scheduled;
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 />這句配置就可以了,這些參數都不是必須的。
Ok配置完畢,當然spring task還有很多參數,我就不一一解釋了,具體參考xsd文檔http://www.springframework.org/schema/task/spring-task-3.0.xsd。
附錄:
cronExpression的配置說明,具體使用以及參數請百度google
字段 允許值 允許的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小時 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可選) 留空, 1970-2099 , - * /
- 區間
* 通配符
? 你不想設置那個字段
下面只例出幾個式子
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觸發