Connect a Spring Boot application to SchedulerX

更新时间:
复制 MD 格式

This topic walks you through creating an application, configuring connection parameters, and validating the client in your Spring Boot project. Once connected, your application can use the SchedulerX platform for automated and precise job management.

Video demonstration

To watch a step-by-step demo of how to connect a Spring Boot application to SchedulerX, see Video demonstration.

Prerequisites

  • (Optional) Create a namespace. If your application has strict security requirements, you can create a namespace to isolate resources and services. For more information, see Create a namespace.

  • You have an existing Spring Boot project.

Overview

Connecting a Spring Boot application to SchedulerX involves the following steps:

  1. Create an application in SchedulerX to get connection parameters and manage jobs centrally.

  2. Connect the client to SchedulerX: Add the required parameters for the SchedulerxWorker to your project's configuration file and create a JobProcessor class.

  3. Verify the results: Confirm that your Spring Boot application is successfully connected and running correctly.

Step 1: Create an application

1.1 Basic settings

  1. Log on to the MSE SchedulerX console, and select a region in the top navigation bar.

  2. On the Application Management page in the left navigation bar. Select the target Namespace, and click Create Application. Enter the Application Name and Application ID, select the Application Type, configure advanced settings as needed, and click Next.

    image

    Important

    Make sure that resources are created in the selected region and namespace and the resource information is valid.

    Configuration item

    Description

    Default value

    Application Name

    Customize the application name.

    None

    Application ID

    The Application ID is the GroupID for application access. It must be unique within the same namespace. Otherwise, the creation fails. It can be the same as the Application Name.

    None

    Application Type

    • Regular App: Select this option if the application is not deployed in a Kubernetes cluster or does not require Kubernetes jobs.

    • k8s App: Select this option if you want to deploy the application in a Kubernetes cluster and require Kubernetes jobs.

    Regular App

    Edition

    Select an edition based on your requirements. For a comparison of editions, see Comparison of product editions.

    Professional Edition

    Simple Log Service

    If you enable this feature, add a Log4j or Logback configuration. Then, you can view the scheduling logs of jobs, including distributed jobs, in the console for troubleshooting.

    Off

    load5

    The value cannot be greater than the number of CPU cores available on the client machine.

    0

    Memory Usage

    If the average memory usage within the previous 5 minutes exceeds the threshold that is specified by this parameter, the worker is considered busy.

    90%

    Disk Usage

    If the disk usage exceeds the threshold that is specified by this parameter, the client machine is considered busy.

    95%

    Trigger Busy Machine

    Specify whether to continue to trigger job execution when the machine is busy.

    On

    Advanced Configuration

    Maximum Number Of Jobs

    Specify the maximum number of jobs that are supported in a group.

    1000

    Automatic Scale-out

    Specify whether to enable automatic scale-out. If you enable this feature, you need to set Global Job Count.

    Off

    Traffic Throttling

    Specify whether to enable traffic throttling. If you enable this feature, you need to set Task Instance Concurrency.

    Off

    Calendar

    You can select Financial Day or Workday from the drop-down list to schedule the application.

    • Financial day: a trading day for financial business.

    • Workday: a day of normal work or operations for a society, organization, or individual.

    0

1.2 Notification settings

  1. On the Notification Configuration wizard page, enter the relevant information, select a Notification Channel, and set Contacts.

    • The Notification Channel supports Text Message, Email, Webhook, and Phone.

      For more information about Webhook configuration, see Webhook configuration description.

    • Notification Contacts supports two methods: contact groups and contacts.

      • Contact Group

        All contacts in the contact group can receive notifications. For more information about how to create a contact group, see Create a notification contact or notification contact group.

      • Contact

        1. Added contacts can receive notifications. You can add multiple contacts at a time. After you Add Contact, click Go To Create Contact.

        image

        1. In the dialog box that appears, click Create Contact, and enter the contact information.

        image

        Configuration item

        Description

        Notification Channel

        The following notification channels are supported: Text Message, Email, Webhook, and Phone.

        Email

        Enter the email address of the contact.

        Webhook

        • Enter the webhook URL that you want to use. WeCom, Lark, and DingTalk are supported. You can also specify multiple webhook robots at a time. Separate multiple robot links with commas (,).

        • You need to add the keyword "SchedulerX" (case-sensitive) to the DingTalk robot. Otherwise, you cannot receive alert information.22

        • For more information about how to obtain a webhook URL, see DingTalk Developer Documentation, WeCom Developer Documentation, and Lark Developer Documentation.

        Mobile Phone Number

        Enter the mobile phone number of the contact.

  2. Verification

    Refresh the page and select the specified region and namespace. Then, view the information about the created application.

Step 2: Connect the client to SchedulerX

  1. Add the dependency to pom.xml

    Add the schedulerx2-spring-boot-starter dependency to your project's pom.xml file to use SchedulerX features.

    Note

    SchedulerX supports connections from Spring Boot 2.x and 3.x.

    <dependency>
      <groupId>com.aliyun.schedulerx</groupId>
      <artifactId>schedulerx2-spring-boot-starter</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>                   
    Important

    Replace <version>${schedulerx2.version}</version> with the latest client version. For example: <version>1.11.5</version>. For more information, see Client release notes.

  2. Configure parameters

    In your Spring Boot application's application.properties file, add the required parameters to connect the SchedulerxWorker to your SchedulerX application.

    spring.schedulerx2.endpoint=${endpoint}
    spring.schedulerx2.namespace=${namespace}
    spring.schedulerx2.groupId=${groupId}
    # Only version 1.2.1 and later supports configuring appKey.
    spring.schedulerx2.appKey=${appKey}
    
    # If you do not use an appKey, you can use an Alibaba Cloud AccessKey ID and AccessKey secret, or an STS token.
    #spring.schedulerx2.aliyunAccessKey=${aliyunAccessKey}
    #spring.schedulerx2.aliyunSecretKey=${aliyunSecretKey} 
    #spring.schedulerx2.stsToken=${aliyunStsToken}                   

    Parameter source:

    In the MSE SchedulerX console, go to the Application Management page in the left-side navigation pane. In the Actions column for your target application, click Access Config and select Spring Boot as the service type. In the dialog box that appears, you can view example values for parameters like endpoint, namespace, groupId, and appKey. Copy these values into the corresponding locations in your configuration file.

    Important

    Ensure that the SchedulerX region matches the client region to avoid connection failures.

    If you are using a non-Alibaba Cloud server or a local environment, make sure you have enabled public network access and created your application in a public-facing region of the scheduling platform. For more details, see Connect to a public environment locally.

    Note

    If your client application includes multiple services or requires permission management to isolate scheduled jobs, you can create multiple applications in the console for fine-grained job management. For example, if a client application named animals has two applications, animals.dog and animals.cat, created in the console, you do not need to request separate instances for each. Instead, you can configure them in the application.properties file by specifying multiple application IDs in the spring.schedulerx2.groupId=${groupId} parameter, separated by commas, such as: spring.schedulerx2.groupId=animals.dog,animals.cat.

    Parameter description:

    • endpoint: When initializing the SchedulerxWorker, use the region and corresponding endpoint where your application is deployed. For more information, see Endpoints.

    • namespace: The ID of the namespace. You can find it on the Namespaces page in the console.

    • groupId and appKey: The groupId is the application ID, and the appKey is the application key. You can find them on the Application Management page in the console.

    • aliyunAccessKey and aliyunSecretKey: aliyunAccessKey is your Alibaba Cloud account's AccessKey ID, and aliyunSecretKey is the AccessKey secret. You can obtain them from the Security Management page in the User Information Management console.

    • For more parameters, see the following table:

      Key

      Description

      Value

      Supported from version

      spring.schedulerx2.enabled

      Enables the SchedulerX 2.0 starter. Default: true.

      true

      false

      0.1.7

      spring.schedulerx2.endpoint

      Sets the endpoint for the region. For more information, see Endpoints.

      None

      0.1.7

      spring.schedulerx2.namespace

      Sets the UID of the namespace. You can find it on the Namespaces page in the console.

      None

      0.1.7

      spring.schedulerx2.groupId

      The groupId. You can find it on the Application Management page in the console.

      None

      0.1.7

      spring.schedulerx2.appKey

      The appKey. You can find it on the Application Management page in the console.

      None

      1.2.1

      spring.schedulerx2.host

      If you have multiple IP addresses (for example, from a VPN or multiple network cards), you can specify the IP address to use.

      None

      0.1.7

      spring.schedulerx2.port

      A custom port for the client to listen on. If not set, a random available port is chosen.

      None

      0.1.7

      spring.schedulerx2.blockAppStart

      If true, blocks application startup if SchedulerX fails to initialize. Default: true.

      true

      false

      1.1.0

      spring.schedulerx2.shareContainerPool

      If true, all job executions on the client share a single thread pool. Default: false.

      true

      false

      1.2.1.2

      spring.schedulerx2.sharePoolSize

      Sets the size of the shared thread pool when spring.schedulerx2.shareContainerPool is true. Default: 64.

      None

      1.2.1.2

      spring.schedulerx2.label

      You can set labels on different clients and specify labels for job execution in job management. This is useful for scenarios like canary releases and stress testing.

      None

      1.2.2.2

      spring.schedulerx2.enableCgroupMetrics

      Specifies whether to use cgroups to collect metrics for client instances. This must be enabled manually in container (K8s) environments. The default is false.

      true

      false

      1.2.2.2

      spring.schedulerx2.cgroupPathPrefix

      The cgroup path inside the container.

      The default path is /sys/fs/cgroup/cpu/. If this path exists, you do not need to set this parameter.

      1.2.2.2

      spring.schedulerx2.enableHeartbeatLog

      Specifies whether to print the heartbeat log to ${user.home}/logs/schedulerx/heartbeat.log.

      The default is true.

      true

      false

      1.2.4

      spring.schedulerx2.mapMasterStatusCheckInterval

      The interval, in milliseconds, at which a Map job checks for sub-job completion. A lower value can accelerate scheduling for second-level jobs.

      3000

      1.2.5.2

      spring.schedulerx2.enableSecondDelayCycleIntervalMs

      If true, the second_delay value from the console is treated as milliseconds instead of seconds, enabling faster scheduling. Default: false.

      true

      false

      1.2.5.2

      spring.schedulerx2.broadcastMasterExecEnable

      Specifies whether the master node participates in the execution of a broadcast job. The default is true.

      true

      false

      1.8.13

      spring.schedulerx2.broadcastDispatchRetryTimes

      The number of retry attempts for a failed broadcast dispatch. The retry interval is fixed at 2 seconds and is not configurable.

      3

      1.8.13

      spring.schedulerx2.enableSecondDelayStandaloneDispatch

      Specifies whether to enable second-level dispatch and execution for standalone jobs. The default is false.

      true

      false

      1.8.13

  3. Create a job processor class

    Create a JobProcessor class in your Spring Boot application to handle the job logic.

    The following example shows how to create a simple JobProcessor class that prints a message periodically (example package name: com.example.schedulerx.job).

    package com.example.schedulerx.job;
    
    import com.alibaba.schedulerx.worker.domain.JobContext;
    import com.alibaba.schedulerx.worker.processor.JavaProcessor;
    import com.alibaba.schedulerx.worker.processor.ProcessResult;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyHelloJob extends JavaProcessor {
    
        @Override
        public ProcessResult process(JobContext context) throws Exception {
            System.out.println("hello schedulerx2.0");
            return new ProcessResult(true);
        }
    }              

Step 3: Verify the results

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.

FAQ

Related documentation

To learn about other client connection methods, see Quickly connect a client to SchedulerX.