Connect a Java application to SchedulerX

更新时间:
复制 MD 格式

This topic explains how to connect a Java application to SchedulerX.

Prerequisites

Connect a Java application client to SchedulerX

  1. Add the SchedulerxWorker dependency to your application's pom.xml file.

    For the latest client version of schedulerx2.version, see Release Notes.

    <dependency>
      <groupId>com.aliyun.schedulerx</groupId>
      <artifactId>schedulerx2-worker</artifactId>
      <version>${schedulerx2.version}</version>
      <!-- If you use Logback, you must 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 in the main method.

    • When initializing SchedulerxWorker, specify your application's deployment region and its corresponding endpoint. For more information, see Endpoints.

    • The namespace parameter is the namespace ID. You can obtain it from the Namespaces page in the console.

    • The GroupId parameter is the application ID. You can obtain it 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.

    public void initSchedulerxWorker() throws Exception {
        SchedulerxWorker schedulerxWorker = new SchedulerxWorker();
        schedulerxWorker.setEndpoint("xxxx");
        schedulerxWorker.setNamespace("xxxx");
        schedulerxWorker.setGroupId("xxxx");
        // For versions 1.2.1 and later, you must set the appKey.
        schedulerxWorker.setAppKey("xxxx");
        schedulerxWorker.init();
    }
    Note
    • If an application contains multiple services or you want to categorize scheduled tasks, you can create multiple application groups. For example, for an application named animals, you can create two groups: animals.dogs and animals.cats. You do not need separate instances to connect to these groups. In the application client, you can configure both group IDs in the groupId= parameter, such as groupId=animals.dogs,animals.cats.

    • When you initialize the SchedulerxWorker client, you can add other configurations as needed. For more information, see SchedulerxWorker parameters.

  3. In your application, create a JobProcessor class to implement task scheduling.

    The following example shows a simple JobProcessor class that prints "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 results

  1. After configuring the client, deploy the application to Alibaba Cloud.

  2. Log on to the MSE SchedulerX console.

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

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

  5. On the Application Management page, check the value of Total number of instances.

    • If Total number of instances is 0, the application failed to connect. Check your client configuration.

    • A value greater than 0 for Total number of instances indicates a successful connection and represents the number of connected instances. In the Actions column, click View instances to see a detailed list in the Connect to an instance dialog box.

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.