介紹
SchedulerX 是阿里中間件團隊開發的一款分佈式任務調度產品,在阿里內部有着廣泛的使用,經過集團內上千個業務應用歷經多年打磨而成。每天非常穩定的運行着集團內幾十萬個任務以及完成每天幾億次的任務調度。在未來 SchedulerX 將支持更多特性以滿足集團內外不斷增長的用戶需求。
用戶在應用中依賴 SchedulerX-Client,並在 SchedulerX 控制檯創建定時任務,進行相應的參數配置後,啓動該應用就可以接收到定時任務的週期調度。SchedulerX-Server 集羣爲調度觸發提供高可用性和高穩定性的保證,並且可以實現對用戶客戶端機器集羣進行分佈式調度
詳細請查看
spring boot 整合
添加依賴
<dependency>
<groupId>com.alibaba.edas</groupId>
<artifactId>schedulerX-client</artifactId>
<version>1.6.6</version>
</dependency>
boot 配置
package com.xescm.tfc.config;
import com.alibaba.dts.common.exception.InitException;
import com.alibaba.edas.schedulerX.SchedulerXClient;
import lombok.Data;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* The class Schedual x config.
* Created by [email protected]
*/
@ConfigurationProperties(prefix = SchedulerXConfig.JOB_PREFIX)
@EnableAutoConfiguration
@Data
public class SchedulerXConfig {
/**
* The constant JOB_PREFIX.
*/
public static final String JOB_PREFIX = "aliyun.schedulerX";
private String groupId;
private String regionName;
/**
* Scheduler x client scheduler x client.
*
* @return the scheduler x client
*/
@Bean
public SchedulerXClient schedulerXClient(){
SchedulerXClient schedulerXClient = new SchedulerXClient();
schedulerXClient.setGroupId(getGroupId());
schedulerXClient.setRegionName(getRegionName());
try {
schedulerXClient.init();
} catch (InitException e) {
e.printStackTrace();
}
return schedulerXClient;
}
}
創建任務HelloWorldJobProcessor
/**
* The class Hello world job processor.
* Created by [email protected]
*/
public class HelloWorldJobProcessor implements ScxSimpleJobProcessor {
/**
* Process process result.
*
* @param scxSimpleJobContext the scx simple job context
*
* @return the process result
*/
@Override
public ProcessResult process(ScxSimpleJobContext scxSimpleJobContext) {
System.out.println("Hello World!");
return new ProcessResult(true);
}
}
application.properties增加配置
aliyun.schedulerX.groupId=201-3-2-****
aliyun.schedulerX.regionName=cn-test
添加密鑰信息
創建文件
E:\xescm\aliyun\job\accessKey\accessKey.txt
修改文件信息
accessKey=LTAIMrsQ********
secretKey=22YzqR6Gjy********************
配置Idea的VM options
-Dspas.identity=E:\xescm\aliyun\job\accessKey\accessKey.txt
創建測試用例
import com.alibaba.dts.common.exception.InitException;
import com.alibaba.edas.schedulerX.SchedulerXClient;
/**
* The class Job test.
* Created by [email protected]
*/
public class JobTest {
public static void main(String[] args) throws InitException {
SchedulerXClient schedulerXClient = new SchedulerXClient();
schedulerXClient.setGroupId("201-3-2-****");
schedulerXClient.setRegionName("cn-test");
schedulerXClient.init();
}
}
執行結果
2017-09-20 14:54:16 WARN [com.alibaba.dts.common.util.RemotingUtil:144] - [RemotingUtil]: ipv4Result last ip, ipv4Result:[192.168.241.1, 192.168.32.191]
14:54:17.021 [main] WARN com.alibaba.dts.client.DtsClient - [SchedulerXClient]: tell initialized, thread:main, groupId:201-3-2-3650
2017-09-20 14:54:17 INFO [com.alibaba.dts.common.remoting.netty.NettyRemotingClient:439] - createChannel: begin to connect remote host[{112.124.219.1:80}] asynchronously
2017-09-20 14:54:17 INFO [com.alibaba.dts.common.remoting.netty.NettyRemotingClient$NettyConnetManageHandler:129] - NETTY CLIENT PIPELINE: CONNECT {UNKNOW} => {/112.124.219.1:80}
2017-09-20 14:54:17 INFO [com.alibaba.dts.common.remoting.netty.NettyRemotingClient:459] - createChannel: connect remote host[{112.124.219.1:80}] success, {DefaultChannelPromise@60f00693(success)}
2017-09-20 14:54:17 INFO [com.alibaba.dts.common.remoting.netty.NettyRemotingClient:439] - createChannel: begin to connect remote host[{112.124.219.2:80}] asynchronously
2017-09-20 14:54:17 INFO [com.alibaba.dts.common.remoting.netty.NettyRemotingClient$NettyConnetManageHandler:129] - NETTY CLIENT PIPELINE: CONNECT {UNKNOW} => {/112.124.219.2:80}
2017-09-20 14:54:17 INFO [com.alibaba.dts.common.remoting.netty.NettyRemotingClient:459] - createChannel: connect remote host[{112.124.219.2:80}] success, {DefaultChannelPromise@14f232c4(success)}
14:54:17.983 [main] WARN c.a.d.client.remoting.ClientRemoting - [ClientRemoting]: initHeartBeatTimer success, heartBeatIntervalTime:120000
14:54:18.031 [main] WARN com.alibaba.dts.client.DtsClient - [SchedulerXClient]: init over, clientConfig:ClientConfig [remotingThreads=32, heartBeatIntervalTime=120000, connectionTimeout=3000, groupId=201-3-2-3650, zkHosts=null, namespace=zk-dts-root, zkSessionTimeout=10000, zkConnectionTimeout=10000, isSpring=false, queueSize=10000, consumerThreads=5, consumerThreadsMap=null, version=1.6.13-SNAPSHOT, pageSize=1000, pageSizeMap=null, accessKey=LTAIMrsQLm2RypHN, secretKey=null, localAddress=192.168.241.1, clientId=a8cbca9b-28cb-42a9-b869-d85ef418d289:192.168.241.1, crashRetry=false, pullTaskListOverSleepTime=20000, failureJobProcessorMap=null, stopJobProcessor=null, finishLog=true, isEveryTimeNew=false, zkHostsAutoChange=true, maxBodySize=65536, environment=SchedulerX, signature=wIhotPp56AtUEhiFlqtcw1rTsrY=, jobMap=null, machine=null, domainName=schedulerx.console.aliyun.com, serviceGroup=null, regionName=cn-test, serviceGroupId=null, autoInit=true, isAgent=false, newInstance=true, configPath=null, connectTime=1505890457908, innerDebug=0]
Hello World!
Hello World! 完畢
在本地啓動出現 “授權失敗,無效的時間戳時修改時間服務器npt1.aliyun.com