Master Distributed Scheduling in Spring Boot with ElasticJob Lite
This guide explains how to replace @Scheduled with the Apache ElasticJob Lite framework in a Spring Boot application, covering project setup, dependency configuration, job implementation, ZooKeeper registration, sharding settings, and testing in a clustered environment to achieve reliable distributed task execution.
In the previous article we introduced how to use Spring Boot's @Scheduled annotation for scheduled tasks and discussed its limitations in a clustered environment where shared resources can cause contention, making distributed locks necessary. Therefore, @Scheduled is best suited for single‑instance tasks such as cleaning a local directory or uploading instance statistics.
This article presents a mature distributed scheduling framework—ElasticJob—and demonstrates its usage with Spring Boot.
ElasticJob
ElasticJob originated as a distributed task scheduling framework open‑sourced by Dangdang and is now part of the Apache Foundation.
The project provides two branches: ElasticJob‑Lite, a lightweight task management solution, and ElasticJob‑Cloud, which uses containers for task isolation. This tutorial uses ElasticJob‑Lite.
Hands‑On Example
Step 1: Create a basic Spring Boot project. If you need a quick start, refer to a Spring Boot introductory guide.
Step 2: Add the ElasticJob‑Lite starter to pom.xml:
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-lite-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
// ...
</dependencies>Step 3: Implement a simple job class:
@Slf4j
@Service
public class MySimpleJob implements SimpleJob {
@Override
public void execute(ShardingContext context) {
log.info("MySimpleJob start : didispace.com {}", System.currentTimeMillis());
}
}Step 4: Configure the job in application.properties (or application.yml):
elasticjob.reg-center.server-lists=localhost:2181
elasticjob.reg-center.namespace=didispace
elasticjob.jobs.my-simple-job.elastic-job-class=com.didispace.chapter72.MySimpleJob
elasticjob.jobs.my-simple-job.cron=0/5 * * * * ?
elasticjob.jobs.my-simple-job.sharding-total-count=1The configuration consists of two parts: the elasticjob.reg-center section defines the ZooKeeper registration center and namespace, while the elasticjob.jobs section defines the job name, implementation class, cron expression, and sharding count (set to 1 for this example).
Running and Testing
After completing the steps above, start a local ZooKeeper instance and run the Spring Boot application. The log will show initialization messages and periodic execution of MySimpleJob every five seconds.
Start a second instance on the same machine with a different port (e.g., -Dserver.port=8081). Both instances will register with ZooKeeper, but because the sharding total count is 1, only one instance will execute the job at any time, demonstrating high‑availability and avoiding duplicate execution.
If the second instance is stopped, the first instance resumes job execution automatically, confirming the fail‑over capability of ElasticJob without writing any custom distributed‑lock code.
This approach simplifies distributed scheduled tasks compared to using @Scheduled directly, especially in complex clustered scenarios.
Code Repository
The complete project can be found in the chapter7-2 directory of the following repositories:
GitHub: https://github.com/dyc87112/SpringBoot-Learning/tree/master/2.x
Gitee: https://gitee.com/didispace/SpringBoot-Learning/tree/master/2.x
Feel free to star the repository if you find the tutorial helpful.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
