This topic describes how to connect a Spring Boot application to SchedulerX.
Prerequisites
(Optional) A namespace is created. For more information, see Create resources.
An application is created. For more information, see Create an application.
Connect the agent to SchedulerX for the Spring Boot application
Procedure
Add the SchedulerxWorker dependency to the
pom.xml
file of the application.<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>
${schedulerx2.version}
: the latest client version. For more information about agent versions, see Release notes.Configure the following parameters in the application.properties file to initialize SchedulerxWorker.
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}
${endpoint}
: When you initialize SchedulerxWorker, you must specify the endpoint of the region in which the application is deployed. For more information, see Endpoints.${namespace}
: the ID of the namespace to which the application belongs. You can obtain the namespace ID on the Namespace page of the SchedulerX console.${groupId}
and${appKey}
: the group ID and the key of the application. You can obtain the group ID and key on the Application Management page of the SchedulerX console You can also click Access configuration in the Operation column of the application to view the configuration information of the access mode.
NoteIf a client contains multiple services, you can create multiple applications and isolate scheduled jobs for permission management. You can append applications in the
groupId
andappKey
fields. Separate multiple applications with commas (,). Examples:spring.schedulerx2.domainName=${domainName} spring.schedulerx2.namespace=${namespace} spring.schedulerx2.groupId=${groupId1},${groupId2} spring.schedulerx2.appKey=${appKey1},${appKey2}
Parameters
key
Description
Initial version
spring.schedulerx2.enabled
Specifies whether to enable the starter of SchedulerX 2.0. Valid values:
true
false
By default, the starter is enabled. You do not need to configure this parameter.
0.1.7
spring.schedulerx2.endpoint
The endpoint of a specific region. For more information, see Endpoints.
0.1.7
spring.schedulerx2.namespace
The UID of the namespace. You can obtain the namespace UID on the Namespace page of the SchedulerX console.
0.1.7
spring.schedulerx2.groupId
The ID of the application. You can obtain the application ID on the Application Management page of the SchedulerX console.
0.1.7
spring.schedulerx2.appKey
The key of the application. You can obtain the application key on the Application Management page of the SchedulerX console.
1.2.1
spring.schedulerx2.host
The actual IP address when multiple IP addresses exist. Multiple IP addresses may exist if the agent uses a virtual private network (VPN) or multiple network interface cards (NICs).
0.1.7
spring.schedulerx2.port
The custom port on which the agent listens. If you do not configure this parameter, an available port is randomly selected.
0.1.7
spring.schedulerx2.blockAppStart
Specifies whether to block the startup of the application if SchedulerX fails to complete the initialization. Valid values:
true
false
Default value:
true
.1.1.0
spring.schedulerx2.shareContainerPool
Specifies whether to allow all jobs on an agent to share the container pool. Default value:
false
.1.2.1.2
spring.schedulerx2.sharePoolSize
Specifies the size of the pool if you allow all jobs to share the container pool. Default value: 64.
1.2.1.2
spring.schedulerx2.label
Used to add labels to agents so that you can manage jobs by label. This feature is suitable for canary release and stress testing scenarios.
1.2.2.2
spring.schedulerx2.enableCgroupMetrics
Specifies whether to use cgroup to collect the metrics of an agent. You must manually enable this feature in a Kubernetes environment. Valid values:
true
false
Default value:
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 print heartbeat log
${user.home}/logs/schedulerx/heartbeat.log
. Valid values:true
false
Default value: true.
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 frequency at which second-delay jobs are triggered, configure this parameter. In this example, the value is set to 3000.
1.2.5.2
spring.schedulerx2.enableSecondDelayCycleIntervalMs
Used to set the unit of the delay to milliseconds for second-delay jobs. If you set this parameter to true, the unit of the delay in the SchedulerX console is changed from seconds to milliseconds. This way, the frequency at which second-delay jobs are triggered is increased. Valid values:
true
false
Default value:
false
.1.2.5.2
spring.schedulerx2.h2DatabaseUser
The username of the H2 built-in database. Example:
root
. Default value:sa
.1.9.7
spring.schedulerx2.h2DatabasePassword
The password of the H2 built-in database. Example: 123456. By default, this parameter is left empty.
1.9.7
spring.schedulerx2.graceShutdownMode
The graceful shutdown mode. If you do not configure this parameter, the graceful shutdown mode is disabled. Valid values:
WAIT_ALL: gracefully shuts down the agent after all jobs are processed.
WAIT_RUNNING: gracefully shuts down the agent after running jobs are processed.
1.10.8
spring.schedulerx2.graceShutdownTimeout
The timeout period for the graceful shutdown mode. Unit: seconds. If you do not configure this parameter or set this parameter to 0, the graceful shutdown mode has no timeout period.
1.10.8
Create a
JobProcessor
class in the application to implement job scheduling. The following code shows how to implement the JobProcessor class for scheduled printing ofhello 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
Publish the application to Alibaba Cloud after the application is connected to SchedulerX.
Log on to the SchedulerX console. In the top navigation bar, select a region. In the left-side navigation pane, click Application Management. On the Application Management page, view the Total number of instances column of the application.
If the Total number of instances column displays 0, the application fails to be connected. 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. Click View instances in the Operation column to view the instances in the Connect to an instance panel.
What to do next
After the application is connected to SchedulerX, you can create jobs in the SchedulerX console. For more information, see Create a job.