Connect your Spring application to SchedulerX to enable distributed task scheduling.
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 the SchedulerX client
-
Add the SchedulerxWorker dependency to the
pom.xmlfile of the application.Replace
schedulerx2.versionin the following sample code with the latest version of the agent. For more information, see Agent release notes.<dependency> <groupId>com.aliyun.schedulerx</groupId> <artifactId>schedulerx2-worker</artifactId> <version>${schedulerx2.version}</version> <!--If you use logback, 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 bean in your XML configuration file.
-
To initialize SchedulerxWorker, you need the endpoint that corresponds to the region where your application is deployed. For more information, see Endpoints.
-
For the namespace parameter, use your namespace ID. You can find this ID on the Namespaces page in the console.
-
Use your application ID for groupId and your application key for appKey. You can obtain them from the application management page in the console. You can also click Access Configuration in the Actions column to find these values.
<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> <!-- Set appKey for versions 1.2.1 and later. --> <property name="appKey"> <value>${appKey}</value> </property> </bean>Note-
If your application contains 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. In this case, you do not need to use two separate sets of instances. Instead, you can specify multiple groups in thegroupId=parameter in your client configuration, such asgroupId=animals.dogs,animals.cats. -
For other SchedulerxWorker client configuration options, see SchedulerxWorker configuration parameters.
-
-
Create a
JobProcessorclass in your application to implement job scheduling.This example shows a simple
JobProcessorclass that prints "hello schedulerx2.0" when the job runs.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
Log on to the MSE SchedulerX console. In the left navigation bar, click Application Management and view the Total Instances of the target application.

If Total Instances is 0, the application failed to connect. Check and modify the local application.
If Total Instances is not 0 and shows the number of connected instances, the application is successfully connected.
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.