Connect a Java application to SchedulerX

更新时间:
复制 MD 格式

This topic shows how to quickly connect a Java application to SchedulerX.

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 Java application client

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

    Use the latest client version for schedulerx2.version. For more information, 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 the SchedulerxWorker client.

    • To initialize SchedulerxWorker, you need the region where your application is deployed and the endpoint for that region. For more information, see Endpoints.

    • The namespace parameter is the namespace ID. You can find the ID on the Namespace page in the console.

    • The GroupId is the application ID. You can find this ID on the Application Management page. You can also click Access configuration in the Actions column to get the configuration details for your 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 application key.
        schedulerxWorker.setAppKey("xxxx");
        schedulerxWorker.init();
    }
    Note
    • If an application includes 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. You do not need to run separate instances for each group. Instead, you can specify a comma-separated list in the groupId parameter in your client configuration, such as groupId=animals.dogs,animals.cats.

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

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

    This 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 result

  1. After you connect the client, deploy the application to Alibaba Cloud.

  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. On the Application Management page, view the Total number of instances.

    • If the total number of instances is 0, the connection failed. Check your application configuration.

    • If the total number of instances is greater than 0, the connection is successful. This value indicates the number of connected instances.

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.