Connect a Spring application to SchedulerX
Connect your Spring application to SchedulerX for distributed job scheduling.
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 a Spring application client to SchedulerX
-
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 for the region where your application is deployed. For more information, see Endpoints.
-
The
namespaceparameter is the ID of your namespace. You can find it on the Namespace page in the console. In the left-side navigation pane, click Namespace, and find the Namespace ID in the list. -
The
groupIdparameter is the application group ID, andappKeyis the application key. You can find them on the Application Management page in the console. Alternatively, click Access configuration in the Operation column.
<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> <!-- The appKey is required for client versions 1.2.1 and later. --> <property name="appKey"> <value>${appKey}</value> </property> </bean>Note-
If an application includes multiple business units or you want to categorize 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 use separate instances for each group. Instead, in the application client, you can list both groups after thegroupId=parameter, for example,groupId=animals.dogs,animals.cats. -
For other configuration options when you initialize the SchedulerxWorker client, see SchedulerxWorker configuration parameters.
-
-
Create the
JobProcessorclass in the application to schedule jobs.The following code shows how to implement the
JobProcessorclass for scheduled printing of "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); } }
Result check
Log on to the Microservices Engine (MSE) console. In the left-side navigation pane, choose Task Scheduling > SchedulerX Version. On the SchedulerX Version page, click Applications in the left-side pane. Then, find the application that you want to manage and click View instances in the Operation column.

-
If the Total number of instances column displays 0, the application fails to connect to SchedulerX. Check and modify the on-premises application.
-
If the Total number of instances column displays an integer other than 0, the application is connected to SchedulerX.
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.