Connect a Java application to SchedulerX
This topic shows how to quickly connect a Java application to SchedulerX.
Prerequisites
-
Optional. A namespace is created. For more information, see the "(Optional) Create a namespace" section of the Create resources topic.
-
One or more resources are created. For more information, see Create resources.
Connect the Java application client
-
Add the SchedulerxWorker dependency to your application's
pom.xmlfile.Use the latest client version for
schedulerx2.version. For more information, 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 the SchedulerxWorker client.
-
To initialize SchedulerxWorker, you need the region where your application is deployed and the endpoint for that region. For more information, see Endpoints.
-
The
namespaceparameter is the namespace ID. You can find the ID on the Namespace page in the console. -
The
GroupIdis the application ID. You can find this ID on the Application Management page. You can also click Access configuration in the Actions column to get the configuration details for your 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 application key. schedulerxWorker.setAppKey("xxxx"); schedulerxWorker.init(); }Note-
If an application includes multiple services or you want to categorize scheduled jobs, you can create multiple groups. For example, for an application named
animals, you can create two groups:animals.dogsandanimals.cats. You do not need to run separate instances for each group. Instead, you can specify a comma-separated list in thegroupIdparameter in your client configuration, such asgroupId=animals.dogs,animals.cats. -
When you initialize the SchedulerxWorker client, you can set other parameters as needed. For more information, see SchedulerxWorker parameters.
-
-
Create a
JobProcessorclass in your application to implement task scheduling.This 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 result
-
After you connect the client, deploy the application to Alibaba Cloud.
-
Log on to the SchedulerX console.
-
In the top navigation bar, select a region.
-
In the left-side navigation pane, click Application Management.
-
On the Application Management page, view the Total number of instances.
-
If the total number of instances is 0, the connection failed. Check your application configuration.
-
If the total number of instances is greater than 0, the connection is successful. This value indicates the number of connected instances.
-
What to do next
After the application is connected to SchedulerX, you can create jobs in the SchedulerX console. For more information, see the "Create a job" section of the Job management topic.