定時任務

Elastic-Job的SimpleJob類型作業

Simple類型作業意爲簡單實現,未經任何封裝的類型。需要繼承AbstractSimpleElasticJob,該類只提供了一個方法用於覆蓋,此方法將被定時執行。用於執行普通的定時任務,與Quartz原生接口相似,只是增加了彈性擴縮容和分片等功能。

引入maven依賴

elastic-job已經發布到中央倉庫,可以在pom.xml文件中直接引入maven座標。

<!-- 噹噹分佈式任務調度 -->
<!-- 引入elastic-job核心模塊 -->
<dependency>
  <groupId>com.dangdang</groupId>
  <artifactId>elastic-job-core</artifactId>
  <version>1.1.1</version>
</dependency>
<!-- 使用springframework自定義命名空間時引入 -->
<dependency>
  <groupId>com.dangdang</groupId>
  <artifactId>elastic-job-spring</artifactId>
  <version>1.1.1</version>
</dependency>

SimpleJob

@Component("simpleElasticJob")
public class DemoSimpleElasticJob extends AbstractSimpleElasticJob {

    @Override
    public void process(JobExecutionMultipleShardingContext jobExecutionMultipleShardingContext) {
        // do something by sharding items
    }
}

Spring命名空間配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:reg="http://www.dangdang.com/schema/ddframe/reg"
       xmlns:job="http://www.dangdang.com/schema/ddframe/job"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.dangdang.com/schema/ddframe/reg
                        http://www.dangdang.com/schema/ddframe/reg/reg.xsd
                        http://www.dangdang.com/schema/ddframe/job
                        http://www.dangdang.com/schema/ddframe/job/job.xsd
                        ">

    <!--配置作業註冊中心-->  
    <!--<reg:zookeeper id="regCenter" server-lists="${JOB_URL}"-->
                   <!--namespace="${namespace}" base-sleep-time-milliseconds="1000"-->
                   <!--max-sleep-time-milliseconds="3000" max-retries="3" />--> 

    <!--配置作業註冊中心 -->
    <bean id="zkConfig" class="com.dangdang.ddframe.reg.zookeeper.ZookeeperConfiguration">
        <property name="serverLists" value="${worker.zk.servers.urls}"/>
        <property name="namespace" value="${namespace}"/>
        <property name="baseSleepTimeMilliseconds" value="1000"/>
        <property name="maxSleepTimeMilliseconds" value="3000"/>
        <property name="maxRetries" value="3"/>
    </bean>
    <bean id="regCenter" class="com.dangdang.ddframe.reg.zookeeper.ZookeeperRegistryCenter" init-method="init">
        <constructor-arg ref="zkConfig"/>
    </bean>

    <!-- 每隔三分鐘執行一次測試任務 -->
    <job:simple id="simpleElasticJob" class="com.worker.service.worker.DemoSimpleElasticJob" registry-center-ref="regCenter" cron="0 0/3 * * * ?"   sharding-total-count="1" ></job:simple>
</beans>    

reg:bean命名空間屬性詳細說明

屬性名 類型 是否必填 缺省值 描述
id String   註冊中心在Spring容器中的主鍵
server-lists String   連接Zookeeper服務器的列表
包括IP地址和端口號
多個地址用逗號分隔
如: host1:2181,host2:2181
namespace String   Zookeeper的命名空間
base-sleep-time-milliseconds int 1000 等待重試的間隔時間的初始值
單位:毫秒
max-sleep-time-milliseconds int 3000 等待重試的間隔時間的最大值
單位:毫秒
max-retries int 3 最大重試次數
session-timeout-milliseconds int 60000 會話超時時間
單位:毫秒
connection-timeout-milliseconds int 15000 連接超時時間
單位:毫秒
digest String 無驗證 連接Zookeeper的權限令牌
缺省爲不需要權限驗證
nested-port int -1 內嵌Zookeeper的端口號
-1表示不開啓內嵌Zookeeper
nested-data-dir String   內嵌Zookeeper的數據存儲路徑
爲空表示不開啓內嵌Zookeeper

job:simple命名空間屬性詳細說明

屬性名 類型 是否必填 缺省值 描述
id String   作業名稱
class String   作業實現類,需實現ElasticJob接口,腳本型作業不需要配置
registry-center-ref String   註冊中心Bean的引用,需引用reg:zookeeper的聲明
cron String   cron表達式,用於配置作業觸發時間
sharding-total-count int   作業分片總數
sharding-item-parameters String   分片序列號和參數用等號分隔,多個鍵值對用逗號分隔
分片序列號從0開始,不可大於或等於作業分片總數
如:
0=a,1=b,2=c
job-parameter String   作業自定義參數
可以配置多個相同的作業,但是用不同的參數作爲不同的調度實例
monitor-execution boolean true 監控作業運行時狀態
每次作業執行時間和間隔時間均非常短的情況,建議不監控作業運行時狀態以提升效率。因爲是瞬時狀態,所以無必要監控。請用戶自行增加數據堆積監控。並且不能保證數據重複選取,應在作業中實現冪等性。
每次作業執行時間和間隔時間均較長的情況,建議監控作業運行時狀態,可保證數據不會重複選取。
monitor-port int -1 作業監控端口
建議配置作業監控端口, 方便開發者dump作業信息。
使用方法: echo “dump” | nc 127.0.0.1 9888
max-time-diff-seconds int -1 最大允許的本機與註冊中心的時間誤差秒數
如果時間誤差超過配置秒數則作業啓動時將拋異常
配置爲-1表示不校驗時間誤差
failover boolean false 是否開啓失效轉移
monitorExecution開啓,失效轉移纔有效
misfire boolean true 是否開啓錯過任務重新執行
job-sharding-strategy-class String true 作業分片策略實現類全路徑
默認使用平均分配策略
詳情參見:作業分片策略
description String   作業描述信息
disabled boolean false 作業是否禁止啓動
可用於部署作業時,先禁止啓動,部署結束後統一啓動
overwrite boolean false 本地配置是否可覆蓋註冊中心配置
如果可覆蓋,每次啓動作業都以本地配置爲準

Spring集成Quartz定時任務

增加Quartz的Maven依賴

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
</dependency>

Spring配置

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx  
            http://www.springframework.org/schema/tx/spring-tx.xsd"
       default-autowire="byName">
       <!-- CronTask -->
        <bean id="cronTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail" ref="generateSettlementForNatureWeekTaskJob" />
            <property name="cronExpression" value="${ecc.worker.generate.settlement.nature.week}"></property>
        </bean>
        <bean id="cronTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!--實際調用類-->  
            <property name="targetObject" ref="cronTask" />
            <!--實際調用方法-->  
            <property name="targetMethod" value="execute" />
        </bean>
        <bean id="cronTask" class="com.b2b.worker.service.worker.CronTask" />
</beans>       

在線cron表達式:http://cron.qqe2.com/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章