Java 定時任務系列(2)-Spring 定時任務的幾種實現

本文來自網絡一些博客的整理(包括gong1208的博客 dary1715的博客)
一、簡介
這個系列介紹Spring框架實現定時任務的兩種方式以及一些高級的用法,包括:
一、使用Quartz,這是一個功能比較強大的的調度器,可讓你的程序在指定時間執行,也能夠按照某一個頻度執行,配置起來稍顯複雜,稍後會詳細介紹。
二、Spring3.0之後自帶的task,能夠將它當作一個輕量級的Quartz,並且使用起來比Quartz簡單許多,稍後會介紹。java

二、Quartz的使用
做業類繼承自特定基類的方式:org.springframework.scheduling.quartz.QuartzJobBeanspring

①定義做業類:tomcat

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class Job1 extends QuartzJobBean {
    private int timeout;
    private static int i = 0;
    //調度工廠實例化後,通過timeout時間開始執行調度
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
    /**
    * 要調度的具體任務
    */
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("定時任務執行中…");
    }
}

②spring配置文件中配置做業類JobDetailBean網絡

<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="com.gy.Job1" />
    <property name="jobDataAsMap">
        <map>
            <entry key="timeout" value="0" />
        </map>
    </property>
</bean>

說明:org.springframework.scheduling.quartz.JobDetailBean有兩個屬性,jobClass屬性即咱們在java代碼中定義的任務類,jobDataAs框架

③配置做業調度的觸發方式(觸發器)
Quartz的做業觸發器有兩種,分別是ide

org.springframework.scheduling.quartz.SimpleTriggerBean工具

org.springframework.scheduling.quartz.CronTriggerBeanthis

第一種SimpleTriggerBean,只支持按照必定頻度調用任務,如每隔30分鐘運行一次。.net

配置方式以下:code

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <property name="jobDetail" ref="job2" />
    <property name="startDelay" value="0" /><!-- 調度工廠實例化後,通過0秒開始執行調度 -->
    <property name="repeatInterval" value="2000" /><!-- 每2秒調度一次 -->
</bean>

第二種CronTriggerBean,支持到指定時間運行一次,如天天12:00運行一次等。

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="job2" />
    <!—天天12:00運行一次 -->
    <property name="cronExpression" value="0 0 12 * * ?" />
</bean>

④配置調度工廠

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="cronTrigger" />
        </list>
    </property>
</bean>

⑤啓動你的應用便可,即將工程部署至tomcat或其餘容器。
到此,spring中Quartz的基本配置就介紹完了,固然了,使用以前,要導入相應的spring的包與Quartz的包,這些就不消多說了。

其實能夠看出Quartz的配置看上去仍是挺複雜的,沒有辦法,由於Quartz實際上是個重量級的工具,若是咱們只是想簡單的執行幾個簡單的定時任務,有沒有更簡單的工具 請看我第下文Spring task的介紹。
特別的,關於CronExpression 的詳細用戶,請參考dary1715的博客

三、Spring Task
上節介紹了在Spring 中使用Quartz,本文介紹Spring3.0之後自主開發的定時任務工具,spring task,能夠將它比做一個輕量級
的Quartz,並且使用起來很簡單,除spring相 關的包外不須要額外的包,並且支持註解和配置文件兩種
形式,下面將分別介紹這兩種方式。

配置文件方式:

①配置做業類
即普通的pojo,以下:

import org.springframework.stereotype.Service;
@Service
public class TaskJob {

    public void job1() {
        System.out.println(“task exe...”);
    }
}

②在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表達式,具體寫法這裏不介紹了,詳情見上篇文章附錄。

這個配置不消多說了,spring掃描註解用的。

到這裏配置就完成了,是否是很簡單。

使用註解形式:

① 編寫普通的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"/>

其餘關於Spring Task使用的例子請參照 參考連接1 參考連接2

相關文章
相關標籤/搜索