1. 程式人生 > >Elastic Job 定時任務實現

Elastic Job 定時任務實現

官方文件:http://dangdangdotcom.github.io/elastic-job/elastic-job-lite/00-overview/intro/

該說的文件上都說了;在過程中遇到一些錯誤記下了

環境:zookeeper版本 zookeeper-3.4.6 或3.4.6以上 不然會莫名其妙的報錯

我是maven 搭建的專案 需要包(當然spring的包是不可少的當然你用java來操作的話就不需要spring的包了):

	    <dependency>
               <artifactId>elastic-job-common-core</artifactId>
               <groupId>com.dangdang</groupId>
               <version>2.1.5</version>
         </dependency>
           
        <dependency>
            <artifactId>elastic-job-lite-core</artifactId>
            <groupId>com.dangdang</groupId>
            <version>2.1.5</version>
        </dependency>
        
        <dependency>
            <artifactId>elastic-job-lite-spring</artifactId>
            <groupId>com.dangdang</groupId>
            <version>2.1.5</version>
        </dependency> 

spring-job配置(引數配置就不講了官方文件都有配置;關鍵點是配置是配置的資料來源作業維度操作歷史記錄看你自己了需要配你就配不需要就不配 event-trace-rdb-data-source="dataSource"):
<?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:context="http://www.springframework.org/schema/context" 
    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.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context.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="localhost:2181" namespace="erp-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />
    <!-- 0 0 0 * * ? -->
    <job:simple id="customerFollowElasticJob" class="com.isz.erp.job.CustomerFollowElasticJob" registry-center-ref="regCenter" cron="0 0 0 * * ?" 
    	sharding-total-count="2"    description="這裡是描述" event-trace-rdb-data-source="dataSource"/>
    <!-- 配置作業-->
</beans>

java程式碼實現:
package com.isz.erp.job;

import org.springframework.beans.factory.annotation.Autowired;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;


/**
 * @Description: 
 * @date: 2017年9月8日 下午5:53:56
 */
public class CustomerFollowElasticJob  implements SimpleJob{

	
	
	@Override
	public void execute(ShardingContext context) {
	 
		
		testShareData(context.getShardingItem(), context.getShardingTotalCount(), 100, context.getTaskId());
	}
	
	
	/**
	* @Description: 測試分片資料 引數一當前分片 引數二分片總數 引數三 總條數
	* @return void 
	* @date 2017年9月7日 下午5:24:41
	 */
	public void testShareData(int sharItem,int sharCount,int dataCount,String taskId ){
		
		if(sharItem==(sharCount-1)){//如果總分片資料==當前分片 說明是最後資料
			System.out.println("分片...."+taskId+"......."+sharItem+"執行資料:"+((dataCount/sharCount)*sharItem)+"到"+((dataCount/sharCount)*(sharItem==0?1:sharItem+1)+(dataCount%sharCount)));	
		}else{
			System.out.println("分片...."+taskId+"........"+sharItem+"執行資料:"+((dataCount/sharCount)*sharItem)+"到"+(dataCount/sharCount)*(sharItem==0?1:sharItem+1));
		}
	}
	
	public static void main(String[] args) {
		
		//97條資料
		int dataCount=97;
		
		//5片
		int share=5;
		
		System.out.println("每個分片執行:"+97/5+"條數資料");
		
		System.out.println("最後一個分片執行:"+((97/5)+(dataCount%share))+"條數資料");
		
	}

}
啟動服務:
package test.dubbo;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @描述: 啟動Dubbo服務用的MainClass.
 * @作者:
 * @建立時間:
 * @版本: 1.0
 */
public class DubboProvider {

	private static final Log log = LogFactory.getLog(DubboProvider.class);

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		try {

			
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
					"classpath:spring/spring-context.xml");
			context.start();
		} catch (Exception e) {
			log.error("== DubboProvider context start error:", e);
		}
		synchronized (DubboProvider.class) {
			while (true) {
				try {
					DubboProvider.class.wait();
				} catch (InterruptedException e) {
					log.error("== synchronized error:", e);
				}
			}
		}
	}

}


啟動服務後你應該就可以看到控制太列印的資訊了..........................到此程式就玩了

可能要用到的工具:

官網上下載:elastic-job-lite-console-2.1.5 可以看到以下資訊(相關引數官網上都有介紹相信你也看的懂是什麼)

目錄裡bin下面有個start.bat如果是linux啟動start.sh就好了 然後在瀏覽器上輸入:http://localhost:8899/ 你就會看到如下資訊


還可以下載:ZooInspector 看一下節點資訊:



就這樣完了。。。。。。。。。。。。。。。