Connect a Spring application to SchedulerX

更新时间:
复制 MD 格式

You can quickly connect your Spring application to SchedulerX to enable 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 the Spring application client to SchedulerX

  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 the XML configuration file.

    • When you initialize SchedulerxWorker, you must specify the endpoint for the region where your application is deployed. For more information, see Endpoint list.

    • namespace specifies the namespace ID. You can obtain the ID from the Namespaces page in the console. In the left navigation bar, click Namespaces and find the Namespace ID of the target namespace on the page that appears.

    • groupId is the application ID, and appKey is the application key. You can obtain them 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.

    <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 the appKey parameter for versions 1.2.1 and later. -->
        <property name="appKey">
          <value>${appKey}</value>
        </property>
    </bean>
    Note
    • If an application contains multiple services or you want to categorize jobs, you can create multiple groups. For example, for an application named animals, you can create two groups named animals.dogs and animals.cats. You do not need to run separate instances to access the two groups. In the application client, you can specify multiple groups by using a comma-separated list after groupId=. Example: groupId=animals.dogs,animals.cats.

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

  3. Create the JobProcessor class in the application to schedule jobs.

    The following code shows how to implement the JobProcessor class 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.

image

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