當SchedulerX在公網地區時,只要可以訪問公網的機器或容器(例如,非阿里雲的機器或本地開發環境),都可以接入SchedulerX。本文介紹如何在本地接入SchedulerX進行測試和開發。
前提條件
操作步驟
在應用pom.xml檔案中添加SchedulerxWorker依賴。不同應用初始化SchedulerxWorker時有所不同:
普通Java或Spring應用
<dependency> <groupId>com.aliyun.schedulerx</groupId> <artifactId>schedulerx2-worker</artifactId> <version>${schedulerx2.version}</version> <!--如果用的是logback,需要把log4j和log4j2排除掉 --> <exclusions> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> </exclusion> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency>Spring Boot應用
<dependency> <groupId>com.aliyun.schedulerx</groupId> <artifactId>schedulerx2-spring-boot-starter</artifactId> <version>${schedulerx2.version}</version> <!--如果使用logback,需要將log4j和log4j2排除 --> <exclusions> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> </exclusion> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency>擷取接入應用的配置資訊,如部署應用的公網地區(Region)和對應的Endpoint(acm.aliyun.com)等。
登入MSE SchedulerX控制台,在頂部功能表列,將地區切換為公網。
在左側導覽列,單擊應用管理,在應用管理頁面,單擊操作列的接入配置擷取對應接入方式的配置資訊。


在①處選擇不同的接入方式擷取接入配置。
初始化SchedulerxWorker。不同應用在初始化SchedulerxWorker有所不同:
普通Java應用
在main函數中初始化SchedulerxWorker。
public void initSchedulerxWorker() throws Exception { SchedulerxWorker schedulerxWorker = new SchedulerxWorker(); schedulerxWorker.setEndpoint("xxxx"); schedulerxWorker.setNamespace("xxxx"); schedulerxWorker.setGroupId("xxxx"); //1.2.1及以上版本需要設定應用key schedulerxWorker.setAppKey("xxxx"); schedulerxWorker.init(); }在接入配置面板,單擊一鍵複製將配置拷貝至對應設定檔中,或將
schedulerxWorker.setEndpoint、schedulerxWorker.setNamespace、schedulerxWorker.setGroupId和schedulerxWorker.setAppKey的值設定為步驟2中擷取的參數值。說明一個應用如果包含多個業務,或者需將定時任務歸類,可以建立多個分組。例如,應用
animals建立兩個分組animals.dogs和animals.cats。此時,無需申請兩批執行個體分別接入這兩個分組,只需在應用用戶端中將這兩個分組配置到groupId=後面即可,例如groupId=animals.dogs,animals.cats。在初始化SchedulerxWorker用戶端時,如需添加其他配置需求,請參見SchedulerxWorker配置參數說明。
Spring應用
在XML設定檔中注入SchedulerxWorker Bean。
<bean id="schedulerxWorker" class="com.alibaba.schedulerx.worker.SchedulerxWorker"> <property name="endpoint"> <value>${endpoint}</value> </property> <property name="namespace"> <value>${namespace}</value> </property> <property name="groupId"> <value>${groupId}</value> </property> <!--1.2.1及以上版本設定appKey --> <property name="appKey"> <value>${appKey}</value> </property> </bean>在接入配置面板,單擊一鍵複製將配置拷貝至對應設定檔中,或將
${endpoint}、${namespace}、${groupId}和${appKey}替換為步驟2中擷取的參數值。Spring Boot應用
在application.properties檔案中添加如下配置:
spring.schedulerx2.endpoint=${endpoint} spring.schedulerx2.namespace=${namespace} spring.schedulerx2.groupId=${groupId} # 1.2.1及以上版本設定appKey spring.schedulerx2.appKey=${appKey}在接入配置面板,單擊一鍵複製將配置拷貝至對應設定檔中,或將
${endpoint}、${namespace}、${groupId}和${appKey}替換為步驟2中擷取的參數值。說明如果一個應用程式套件含多個業務,或者需要將定時任務進行歸類,可以建立多個分組。例如,應用animals建立了兩個分組animals.dogs和animals.cats,此時無需申請兩批執行個體分別接入這兩個分組,在應用用戶端中將這兩個分組配置到
groupId=後面即可,例如groupId=animals.dogs,animals.cats。在應用中建立類
JavaProcessor,實現任務調度。下方範例程式碼介紹如何?一個簡單的定時列印
hello schedulerx2.0的JavaProcessor類。package com.aliyun.schedulerx.test.job; import com.alibaba.schedulerx.worker.domain.JobContext; import com.alibaba.schedulerx.worker.processor.JavaProcessor; import com.alibaba.schedulerx.worker.processor.ProcessResult; @Component public class MyHelloJob extends JavaProcessor { @Override public ProcessResult process(JobContext context) throws Exception { System.out.println("hello schedulerx2.0"); return new ProcessResult(true); } }運行本地應用。
結果驗證
登入MSE SchedulerX控制台,在左側導覽列的應用管理,查看目標應用的執行個體總數。

如果執行個體總數為0,說明應用接入失敗。請檢查並修改本地應用。
如果執行個體總數不為0,顯示接入執行個體個數,說明應用接入成功。