當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>
初始化SchedulerxWorker。
針對不同應用,在初始化SchedulerxWorker的時候會有所不同。
普通Java應用
在main函數中初始化SchedulerxWorker。
初始化SchedulerxWorker時,會用到您部署應用的公網地域(Region)和對應的Endpoint(acm.aliyun.com)。
登錄分布式任務調度平臺。
在控制臺頂部菜單欄,將地域切換為公網。
在左側導航欄單擊命名空間,在命名空間頁面查看命名空間ID(Namespace)。
在左側導航欄單擊應用管理,在應用管理頁面查看應用ID(groupId)和應用key(appKey)。您也可以在操作列,單擊接入配置獲取對應接入方式的配置信息。
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(); }
說明一個應用如果包含多個業務,或者想把定時任務進行歸類,可以建立多個分組,例如應用
animals
新建了兩個分組animals.dogs
和animals.cats
。此時不用申請兩批實例分別接入這兩個分組,在應用客戶端中把這兩個分組配置到groupId=
后面即可,例如groupId=animals.dogs,animals.cats
。在初始化SchedulerxWorker客戶端時,如果還有其他配置需求,可以添加配置。更多信息,請參見SchedulerxWorker配置參數說明添加配置。
Spring應用
在xml配置文件中注入SchedulerxWorker Bean。
登錄分布式任務調度平臺。
在控制臺頂部菜單欄,將地域切換為公網。
在左側導航欄單擊命名空間,在命名空間頁面查看命名空間ID(Namespace)。
在左側導航欄單擊應用管理,在應用管理頁面查看應用ID(groupId)和應用key(appKey)。您也可以在操作列,單擊接入配置獲取對應接入方式的配置信息。
<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>
Spring Boot應用
在application.properties文件中添加如下配置:
登錄分布式任務調度平臺。
在控制臺頂部菜單欄,將地域切換為公網。
在左側導航欄單擊命名空間,在命名空間頁面查看命名空間ID(Namespace)。
在左側導航欄單擊應用管理,在應用管理頁面查看應用ID(groupId)和應用key(appKey)。您也可以在操作列,單擊接入配置獲取對應接入方式的配置信息。
spring.schedulerx2.endpoint=${endpoint} spring.schedulerx2.namespace=${namespace} spring.schedulerx2.groupId=${groupId} # 1.2.1及以上版本設置appKey spring.schedulerx2.appKey=${appKey}
說明如果一個應用包含多個業務,或者需要將定時任務進行歸類,可以建立多個分組。例如,應用animals新建了兩個分組animals.dogs和animals.cats,此時無需申請兩批實例分別接入這兩個分組,在應用客戶端中將這兩個分組配置到groupId=后面即可,例如groupId=animals.dogs,animals.cats。
在應用中創建類
JobProcessor
,實現任務調度。下方示例代碼介紹如何實現一個簡單的定時打印“hello schedulerx2.0”的JobProcessor類。
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); } }
運行本地應用。
結果驗證
客戶端接入完成,將該應用發布到阿里云。
登錄分布式任務調度平臺,在頂部菜單欄選擇地域,然后在左側導航欄,單擊應用管理,在應用管理頁面查看實例總數。
如果實例總數為0,說明應用接入失敗。請檢查、修改本地應用。
如果實例總數不為0,顯示接入的實例個數,說明應用接入成功。在操作列單擊查看實例,即可在連接實例對話框中查看實例列表。