This topic explains how to connect a Java application to SchedulerX.
Prerequisites
(Optional) A namespace is created. For more information, see Create a namespace.
A resource is created. For more information, see Create a resource.
Connect a Java application client to SchedulerX
-
Add the SchedulerxWorker dependency to your application's
pom.xmlfile.For the latest client version of
schedulerx2.version, see Release Notes.<dependency> <groupId>com.aliyun.schedulerx</groupId> <artifactId>schedulerx2-worker</artifactId> <version>${schedulerx2.version}</version> <!-- If you use Logback, you must exclude log4j and 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> -
Initialize SchedulerxWorker in the main method.
-
When initializing SchedulerxWorker, specify your application's deployment region and its corresponding endpoint. For more information, see Endpoints.
-
The namespace parameter is the namespace ID. You can obtain it from the Namespaces page in the console.
-
The GroupId parameter is the application ID. You can obtain it from the Application Management page in the console. You can also click Access Configuration in the Actions column to obtain the configuration information for the corresponding access method.
public void initSchedulerxWorker() throws Exception { SchedulerxWorker schedulerxWorker = new SchedulerxWorker(); schedulerxWorker.setEndpoint("xxxx"); schedulerxWorker.setNamespace("xxxx"); schedulerxWorker.setGroupId("xxxx"); // For versions 1.2.1 and later, you must set the appKey. schedulerxWorker.setAppKey("xxxx"); schedulerxWorker.init(); }Note-
If an application contains multiple services or you want to categorize scheduled tasks, you can create multiple application groups. For example, for an application named
animals, you can create two groups:animals.dogsandanimals.cats. You do not need separate instances to connect to these groups. In the application client, you can configure both group IDs in thegroupId=parameter, such asgroupId=animals.dogs,animals.cats. -
When you initialize the SchedulerxWorker client, you can add other configurations as needed. For more information, see SchedulerxWorker parameters.
-
-
In your application, create a
JobProcessorclass to implement task scheduling.The following example shows a simple
JobProcessorclass that prints "hello schedulerx2.0".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); } }
Verify the results
-
After configuring the client, deploy the application to Alibaba Cloud.
Log on to the MSE SchedulerX console.
-
In the top navigation bar, select a region.
-
In the left-side navigation pane, click Application Management.
-
On the Application Management page, check the value of Total number of instances.
-
If Total number of instances is 0, the application failed to connect. Check your client configuration.
-
A value greater than 0 for Total number of instances indicates a successful connection and represents the number of connected instances. In the Actions column, click View instances to see a detailed list in the Connect to an instance dialog box.
-
What to do next
After the application is connected to SchedulerX, you can create scheduling tasks in the Distributed Task Scheduling Platform. For more information, see Create a scheduling task.