All Products
Search
Document Center

SchedulerX:Connect a Spring Boot application to SchedulerX

Last Updated:Nov 14, 2024

This topic describes how to connect a Spring Boot application to SchedulerX, including how to create a SchedulerX application, configure the necessary connection parameters, configure an agent within a Spring Boot project, and verify the connection. This ensures that your Spring Boot application runs as expected in SchedulerX and implements automatic scheduling and precise management of jobs.

Prerequisites

  • Optional. A namespace is created. If your Spring Boot application requires high security, you can create a namespace to isolate the resources and services of the application from other applications. For more information, see the "Create a namespace" section of the Namespace management topic.

  • A Spring Boot project is created.

Overview

To connect a Spring Boot application to SchedulerX, perform the following steps:

  1. Create a SchedulerX application: Create an application in SchedulerX, obtain the parameters for connecting to SchedulerX, and implement centralized job management and configuration.

  2. Connect your Spring Boot application to SchedulerX by using an agent: In the .properties or .yml file of your Spring Boot project, configure the parameters required by SchedulerxWorker and create a JobProcessor class to connect the Spring Boot application to the specified SchedulerX application.

  3. Verify the result: Verify that the Spring Boot application is connected to SchedulerX and runs as expected.

Step 1: Create a SchedulerX application

1.1 Basic configuration

  1. Log on to the SchedulerX console and select a region.

  2. In the left-side navigation pane, click Applications. Select a namespace and click Create application. In the Create application panel, specify the Application Name and Application ID parameters, select a value for app type, configure settings in Advanced Configuration based on your business requirements, and then click Next Step.

    image

    Important

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

    Parameter

    Description

    Default value

    Application Name

    Specify a name for the application.

    None

    Application ID

    Specify an ID for the application. The ID of an application is also the ID of a job group after the application is connected to SchedulerX. Make sure that the application ID is unique in a namespace. Otherwise, the application fails to be created. You can use the value of the Application Name parameter as the application ID.

    None

    app type

    • general app: Select this option if you do not want to deploy the application in a Kubernetes cluster or do not require Kubernetes jobs.

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

    general app

    Release

    Select an edition based on your business requirements.

    Professional Edition

    Remote Log Server

    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%

    Whether to trigger a busy machine

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

    On

    Advanced Configuration

    Maximum number of tasks

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

    1000

    Automatic expansion

    Specify whether to enable automatic scale-out. If you enable this feature, you must specify the Number of global tasks parameter.

    Off

    Flow Control

    Specify whether to enable traffic throttling. If you turn on this switch, you must specify the Number of concurrent task instances parameter.

    Off

    Calendar

    If you set this parameter to Specify calendar, you can select Financial day or Workday from the drop-down list.

    • 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 configuration

  1. In the Notification configuration step, specify the Notification channel and Notify contact parameters.

    • Select SMS, Mail, webhook, or Telephone for Notification channel.

      For more information about how to configure a webhook, see the Configure a webhook section in the "Application management" topic.

    • Select Contact group or Contact for Notify contact.

      • Contact group

        All contacts in the contact group can receive notifications. For more information about how to create a contact group, see the Create a notification contact and a notification contact group section in the "Notification contacts and notification contact groups" topic.

      • Contact

        1. Added contacts can receive notifications. You can add multiple contacts at a time. If you have not created any contact, click Add contact and then click Go to create a contact from the drop-down list.

        image

        1. On the page that appears, click New contact and specify the contact information.

        image

        Parameter

        Description

        Notification channel

        Valid values: SMS, Mail, webhook, and Telephone.

        Mail

        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 (,).

        • If you use a DingTalk chatbot, you must add the keyword SchedulerX (case-sensitive). Otherwise, the alert information cannot be received.

        • For more information about how to obtain a webhook, see DingTalk development document, WeCom development documentation, and Lark development documentation.

        Mobile phone number

        Enter the mobile phone number of the contact.

  2. Verify the information.

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

Step 2: Connect a Spring Boot application to SchedulerX by using an agent

  1. Add the required dependency to the pom.xml file.

    Add the required SchedulerxWorker dependency to the pom.xml file of your Spring Boot application to allow the Spring Boot application to use the features provided by SchedulerX.

    Note

    SchedulerX supports Spring Boot 2.x and 3.x applications.

    <dependency>
      <groupId>com.aliyun.schedulerx</groupId>
      <artifactId>schedulerx2-spring-boot-starter</artifactId>
      <version>${schedulerx2.version}</version>
      <!-- If you use Logback, you must exclude Log4j and Log4j 2. -->
      <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 agent version. Example: <version>1.11.5</version>. For more information, see Agent release notes.

  2. Configure parameters.

    In the application.properties configuration file of the Spring Boot application, add the parameters required by SchedulerxWorker, such as connection information and authentication information. Such parameters are required for the Spring Boot application to connect to the SchedulerX application.

    spring.schedulerx2.endpoint=${endpoint}
    spring.schedulerx2.namespace=${namespace}
    spring.schedulerx2.groupId=${groupId}
    # If your agent version is 1.2.1 or later, you must specify the application key.
    spring.schedulerx2.appKey=${appKey}
    
    # If your agent version is earlier than 1.2.1, you must specify an AccessKey ID and an AccessKey secret. To enable other special configurations, such as defining job synchronization and automatic synchronization of Spring jobs in the configuration file, you must configure the following parameters:
    #spring.schedulerx2.aliyunAccessKey=${aliyunAccessKey}
    #spring.schedulerx2.aliyunSecretKey=${aliyunSecretKey}                  

    Parameter sources:

    Log on to the SchedulerX console. In the left-side navigation pane, click Applications. On the Applications page, find the application that you want to manage and click AccessConfig in the Operation column. In the AccessConfig panel, select Spring Boot from the drop-down list.

    image

    Important

    Check whether SchedulerX and the agent are in the same region. If not, the agent fails to connect to SchedulerX.

    If your Spring Boot application runs on a non-Alibaba Cloud server or in an on-premises environment, make sure that you have enabled Internet access to the Spring Boot application and created a SchedulerX application in the Internet region. For more information, see Connect to SchedulerX over the Internet from an on-premises environment.

    Note

    If your Spring Boot application contains multiple service modules or you want to isolate CRON jobs for permission management, you can create multiple applications in the SchedulerX console to implement fine-grained management of CRON jobs. For example, applications animals.dog and animals.cat are created for the application animals in the SchedulerX console. In this case, you do not need to separately apply for workers for the two applications. Instead, you can simply specify spring.schedulerx2.groupId=${groupId} in the application.properties configuration file. Replace ${groupId} with the application names. Separate multiple application names with commas (,). Example: spring.schedulerx2.groupId=animals.dog,animals.cat.

    Parameter description:

    • endpoint: the endpoint of SchedulerxWorker. The value of this parameter must match the endpoint of the region in which your application is deployed. For more information, see Endpoints.

    • namespace: the ID of the namespace to which the application belongs. You can view the namespace ID on the Namespaces page of the SchedulerX console.1

    • groupId and appKey: the ID and key of the application. You can view the values on the Applications page of the SchedulerX console.1

    • aliyunAccessKey and aliyunSecretKey: the AccessKey ID and AccessKey secret of your Alibaba Cloud account. You can view the AccessKey pair on the Security Management page of the User Management console.

    • The following table describes the parameters that you can configure based on your business requirements.

      Parameter

      Description

      Value

      Initial version

      spring.schedulerx2.enabled

      Specifies whether to enable the starter of SchedulerX 2.0. Default value: true. You do not need to configure this parameter.

      true

      false

      0.1.7

      spring.schedulerx2.endpoint

      The endpoint of the specified region. For more information, see Endpoints.

      N/A

      0.1.7

      spring.schedulerx2.namespace

      The UID of the namespace. You can view the namespace UID on the Namespaces page of the SchedulerX console.

      N/A

      0.1.7

      spring.schedulerx2.groupId

      The ID of the application. You can view the application ID on the Applications page of the SchedulerX console.

      N/A

      0.1.7

      spring.schedulerx2.appKey

      The key of the application. You can view the application key on the Applications page of the SchedulerX console.

      N/A

      1.2.1

      spring.schedulerx2.host

      The actual IP addresses if multiple IP addresses exist. Multiple IP addresses may exist if the agent uses a VPN or multiple network interface cards (NICs).

      N/A

      0.1.7

      spring.schedulerx2.port

      The port on which the agent listens. If you do not specify a port, the system randomly selects an available port.

      N/A

      0.1.7

      spring.schedulerx2.blockAppStart

      Specifies whether to block the startup of the application if SchedulerX fails to complete the initialization. Default value: true.

      true

      false

      1.1.0

      spring.schedulerx2.shareContainerPool

      Specifies whether to allow all jobs on agents to share the thread pool. Default value: false.

      true

      false

      1.2.1.2

      spring.schedulerx2.sharePoolSize

      The size of the thread pool if you allow all jobs to share the pool. Default value: 64.

      N/A

      1.2.1.2

      spring.schedulerx2.label

      The labels. You can add labels to agents so that you can use labels to specify workers for job management. This feature is suitable for canary release and stress testing scenarios.

      N/A

      1.2.2.2

      spring.schedulerx2.enableCgroupMetrics

      Specifies whether to use the cgroup to collect the metrics of an agent. Default value: false. You must manually enable this feature in a Kubernetes environment.

      true

      false

      1.2.2.2

      spring.schedulerx2.cgroupPathPrefix

      The path of the cgroup in the container.

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

      1.2.2.2

      spring.schedulerx2.enableHeartbeatLog

      Specifies whether to display heartbeat logs in the ${user.home}/logs/schedulerx/heartbeat.log file.

      Default value: true.

      true

      false

      1.2.4

      spring.schedulerx2.mapMasterStatusCheckInterval

      The interval at which SchedulerX checks whether all tasks are complete in the Map model. Unit: milliseconds. If you want to increase the scheduling frequency of second-granularity jobs, configure this parameter.

      3000

      1.2.5.2

      spring.schedulerx2.enableSecondDelayCycleIntervalMs

      Specifies whether to enable millisecond precision for second-granularity jobs. Default value: false. If you set this parameter to true, the scheduling interval specified in the console for second-granularity jobs is interpreted in milliseconds instead of seconds. This increases the scheduling frequency of your jobs.

      true

      false

      1.2.5.2

      spring.schedulerx2.broadcastMasterExecEnable

      Specifies whether the primary node is required to run a broadcast job. Default value: true.

      true

      false

      1.8.13

      spring.schedulerx2.broadcastDispatchRetryTimes

      The maximum number of retries after broadcasting fails. By default, no retry is performed. The retry interval is fixed to 2 seconds.

      3

      1.8.13

      spring.schedulerx2.enableSecondDelayStandaloneDispatch

      Specifies whether to enable dispatching of second-granularity standalone jobs. Default value: false.

      true

      false

      1.8.13

  3. Create a JobProcessor class

    Create a JobProcessor class in the Spring Boot application to enable job scheduling.

    The following sample code shows how to schedule a simple JobProcessor class to periodically display a message. In this example, the message hello schedulerx2.0 is displayed.

    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);
        }
    }              

Step 3: Verify the result

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 Spring Boot 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.

FAQ

References

For more information about other methods for connecting an application to SchedulerX by using an agent, see Connect an agent to SchedulerX.