Connect a Spring application to SchedulerX

更新时间:
复制 MD 格式

Connect your Spring application to SchedulerX to enable distributed task scheduling.

Prerequisites

Connect the SchedulerX client

  1. Add the SchedulerxWorker dependency to the pom.xml file of the application.

    Replace schedulerx2.version in 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>              
  2. 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.dogs and animals.cats. In this case, you do not need to use two separate sets of instances. Instead, you can specify multiple groups in the groupId= parameter in your client configuration, such as groupId=animals.dogs,animals.cats.

    • For other SchedulerxWorker client configuration options, see SchedulerxWorker configuration parameters.

  3. Create a JobProcessor class in your application to implement job scheduling.

    This example shows a simple JobProcessor class 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.

image

Note
  • 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.