All Products
Search
Document Center

SchedulerX:Connect a Spring Boot application to SchedulerX

Last Updated:Sep 29, 2024

This topic describes how to connect a Spring Boot application to SchedulerX.

Procedure

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

  1. Add dependencies to the pom.xml file: After you add the dependencies, the Spring Boot application can use the features provided by SchedulerX.

  2. Configure parameters: In the properties or yml file, configure the parameters required for SchedulerXWorker to connect to the specified SchedulerX application.

  3. Create a job class: Create one or more JobProcessor classes in the application to implement specific job logic so that SchedulerX can schedule and manage jobs.

Prerequisites

  • Optional. A namespace is created. For more information, see Create a namespace.

  • One or more resources are created. For more information, see Create resources.

  • A Spring Boot application is created.

Step 1: Add dependencies to the pom.xml file

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

Note

SchedulerX supports Spring Boot 3.X.

<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 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>                   
Note

Replace <version>${schedulerx2.version}</version> with the latest agent version. Example: <version>1.11.5</version>. For more information, see Agent release notes.

Step 2: Configure parameters

In the application.properties configuration file of the Spring Boot application, add 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

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

    image

    image

    Important

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

    Note

    If an application contains multiple services or you want to classify the Cron jobs of an 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.

  2. The following list describes the parameters in the sample code.

    • 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 Namespace page of the SchedulerX console.1

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

    • aliyunAccessKey and aliyunSecretKey: the AccessKey ID and the 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.

key

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.

None

0.1.7

spring.schedulerx2.namespace

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

None

0.1.7

spring.schedulerx2.groupId

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

None

0.1.7

spring.schedulerx2.appKey

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

None

1.2.1

spring.schedulerx2.host

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

None

0.1.7

spring.schedulerx2.port

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

None

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 container pool. The default value is false.

  • true

  • false

1.2.1.2

spring.schedulerx2.sharePoolSize

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

None

1.2.1.2

spring.schedulerx2.label

Adds 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.

None

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

Specifies 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 print heartbeat logs in the ${user.home}/logs/schedulerx/heartbeat.log path.

Default value: true.

  • true

  • false

1.2.4

spring.schedulerx2.mapMasterStatusCheckInterval

Specifies the interval at which SchedulerX checks whether all tasks are completed in the Map model. Unit: milliseconds. If you want to increase the frequency at which second-delay jobs are triggered, configure this parameter.

3000

1.2.5.2

spring.schedulerx2.enableSecondDelayCycleIntervalMs

Specifies whether to set the unit of the delay to milliseconds for second-delay jobs. Default value: false. If you set this parameter to true, the unit of the delay specified in the console is changed from seconds to milliseconds. This way, the frequency at which second-delay jobs are triggered is increased.

  • 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 distribution and execution of second-delay standalone jobs. Default value: false.

  • true

  • false

1.8.13

Step 3: Create a job class

Enable job scheduling for the Spring Boot application

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

The following sample code shows how to periodically print a simple JobProcessor class. In this example, the package is named 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. In the left-side navigation pane, click Application Management. On the Application Management page, click the number in the Existing/maximum number of tasks column to view the details.

    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 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 the terms of SchedulerX, see Terms.