All Products
Search
Document Center

Microservices Engine:Connect a Spring application to SchedulerX

Last Updated:Sep 13, 2024

This topic describes how to connect a Spring application to SchedulerX. This way, you can use SchedulerX to schedule jobs for the application.

Before you begin

Connect the agent to SchedulerX for the Spring application

  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 SchedulerxWorker by injecting a bean into the XML configuration file.

    • To initialize SchedulerxWorker, you must specify the region in which the application is deployed and the matching endpoint. For more information, see Endpoints.

    • namespace specifies the ID of the namespace to which the application belongs. You can view the namespace ID on the Namespace page of the SchedulerX console.

    • groupId specifies the group ID of the application and appKey specifies the key of the application. You can view the group ID and application key on the Application Management page of the SchedulerX console. You can also click Access configuration in the Operation column of the application to view the configuration information of the access mode.

      image..png

    <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>
        <!--If your agent version is 1.2.1 or later, you must specify the application key. -->
        <property name="appKey">
          <value>${appKey}</value>
        </property>
      
    </bean>
    Note
    • If an application provides multiple services or you want to classify the scheduling jobs of the application, you can create multiple groups. For example, you created groups animals.dogs and animals.cats for the application animals. In this case, you do not need to separately add workers to the two groups. You can append the two groups to groupId= in the configurations of the agent. Example: groupId=animals.dogs,animals.cats.

    • When you initialize SchedulerxWorker, you can add other configurations. For more information, see SchedulerxWorker 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);
        }
    }              

Verify the result

  1. Publish the application to Alibaba Cloud after the application is connected to SchedulerX.

  2. Log on to the SchedulerX console.

  3. In the top navigation bar, select a region.

  4. In the left-side navigation pane, click Application Management.

  5. View Total number of instances on the Application Management page.

    • If the Total number of instances column displays 0, the application is not connected to SchedulerX. Check and modify the on-premises application.

    • If the Total number of instances column displays a value other than 0, the application is connected to SchedulerX. Click View instances in the Operation column to view the instances in the Connect to an instance panel.

What to do next

After the application is connected to SchedulerX, you can create jobs in the SchedulerX console. For more information, see Create a job.