SchedulerX lets you run scheduled jobs in Spring Cloud applications deployed to Enterprise Distributed Application Service (EDAS). This guide walks you through creating a Simple Job Single-instance Edition job, testing it locally, and deploying it to EDAS.
SchedulerX is Alibaba Cloud's distributed job scheduling platform. It provides:
Sub-minute precision: Schedule jobs with second-level granularity using cron expressions.
High availability: Built-in failover and reliable execution.
Distributed execution models: Run parallel workloads with models such as MapReduce.
Prerequisites
Before you begin, make sure you have:
An Alibaba Cloud account with EDAS activated
Access to the EDAS console
Step 1: Create the Maven project
Create a Maven project named scx-example.
Add the following dependencies to your pom.xml file. This configuration uses Spring Boot 2.1.4.RELEASE with Spring Cloud Finchley.SR1.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!-- SchedulerX Spring Boot starter -->
<dependency>
<groupId>com.aliyun.schedulerx</groupId>
<artifactId>schedulerx2-spring-boot-starter</artifactId>
<version>${schedulerx2.version}</version>
<!-- Exclude log4j if you use Logback -->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Spring Boot 1.x has reached end of life. If you still use Spring Boot 1.5.x with Spring Cloud Edgware, set the Spring Cloud Alibaba version to 1.5.1.RELEASE.
Step 2: Create the application entry point
Create a startup class named ScxApplication:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ScxApplication {
public static void main(String[] args) {
SpringApplication.run(ScxApplication.class, args);
}
}Step 3: Create a test service
Create a Spring-managed service class named TestService. This class validates that Spring Inversion of Control (IoC) injection works correctly in the job class.
import org.springframework.stereotype.Service;
@Service
public class TestService {
public void test() {
System.out.println("---------IOC Success--------");
}
}Step 4: Implement the job processor
Create a job class named SimpleTask that implements ScxSimpleJobProcessor. This class defines the logic that runs each time the job triggers.
import com.alibaba.edas.schedulerx.ProcessResult;
import com.alibaba.schedulerx.worker.domain.JobContext;
import com.alibaba.schedulerx.worker.processor.JavaProcessor;
import org.springframework.beans.factory.annotation.Autowired;
public class SimpleTask implements ScxSimpleJobProcessor {
@Autowired
private TestService testService;
@Override
public ProcessResult process(JobContext context) {
System.out.println("-----------Hello world---------------");
testService.test();
return new ProcessResult(true);
}
}The process method returns ProcessResult(true) to indicate successful execution. Spring automatically injects TestService through the @Autowired annotation.
Step 5: Create a scheduled job in the EDAS console
Log on to the EDAS console.
In the test region, create a scheduled job group and note the group ID.
In the job group, create a job with the following parameters:
Parameter Value Description Application ID The group ID from the previous step Links the job to your job group Processor class name SimpleTaskFull class name that implements the job interface Task type Stand-alone operation Runs the job on a single instance cron expression 0 * * * * ?Triggers the job once every minute Description (Leave blank) Optional job description Task parameters (Leave blank) Optional runtime parameters
Step 6: Configure the application
In src/main/resources, create application.properties with the following settings:
server.port=18033
# SchedulerX connection settings
# Replace the placeholder values with your actual namespace and group credentials.
spring.schedulerx2.endpoint=acm.aliyun.com
spring.schedulerx2.namespace=<your-namespace-id>
spring.schedulerx2.groupId=<your-group-id>
spring.schedulerx2.appKey=<your-app-key>Replace the following placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
<your-namespace-id> | Namespace ID from the EDAS console | d1ce9d38-ab9b-444e-ab2a-4***** |
<your-group-id> | Group ID of your scheduled job group | Obtained in Step 5 |
<your-app-key> | Application key for authentication | 5pHlwaWpluJGd39****** |
The test region uses cn-test as its regionName. When you test locally, traffic goes over the Internet with no permission restrictions.Step 7: Run and verify locally
Run the main method in ScxApplication to start the service.
Check the console output in your IDE. When the job runs successfully, the following messages print once every minute:
-----------Hello world---------------
---------IOC Success--------Deploy to EDAS
After local verification, deploy the application to EDAS without modifying any code or configuration. SchedulerX supports seamless migration from a development environment to EDAS.
Choose a deployment target based on your infrastructure:
| Deployment target | Guide |
|---|---|
| Kubernetes cluster | Create and deploy applications in Kubernetes clusters |
| Elastic Compute Service (ECS) cluster | Create and deploy applications in ECS clusters |
After deployment, manage and monitor your scheduled jobs from the EDAS console.
Deploy to production regions
The steps above use the test region for development and verification. To deploy to a production region such as China (Hangzhou), add your AccessKey credentials to application.properties:
spring.cloud.alicloud.access-key=<your-access-key-id>
spring.cloud.alicloud.secret-key=<your-access-key-secret>Get your AccessKey ID and AccessKey secret from the Security Management page.
Store AccessKey credentials securely. Avoid committing them to version control. Use environment variables or a secrets manager in production environments.
What to do next
After you deploy the application to EDAS, explore additional SchedulerX capabilities:
Configure other job types beyond Simple Job Single-instance Edition.
Set up distributed execution models such as MapReduce to run parallel workloads across multiple instances.