Cloud Native 10 min read

Implement Distributed Scheduled Tasks with Spring Cloud Alibaba SchedulerX

This guide explains why traditional Java timers fail in microservice clusters, introduces Spring Cloud Alibaba's SchedulerX module with high‑availability and lock features, and provides step‑by‑step Maven setup, configuration, and code examples for both Alibaba SchedulerX and the open‑source ShedLock approach.

Alibaba Cloud Native
Alibaba Cloud Native
Alibaba Cloud Native
Implement Distributed Scheduled Tasks with Spring Cloud Alibaba SchedulerX

Background

Scheduled tasks run at fixed times or intervals and are used for asynchronous data processing, automated operations, system monitoring, and data synchronization. In monolithic Java applications developers often use java.util.Timer, ScheduledExecutorService, or Spring’s @Scheduled annotation.

Challenges in Microservices

When a service is deployed on multiple nodes, each instance runs its own timer, causing duplicate executions. Distributed scheduling must prevent repeats, support sharding, and control parallelism.

Spring Cloud Alibaba SchedulerX Module

Spring Cloud Alibaba provides a Scheduling module (pull request #3732) that offers a lightweight, open‑source, high‑availability solution for distributed scheduled tasks. The implementation mode is selected via spring.cloud.scheduling.distributed-mode:

schedulerx : integrates with Alibaba Cloud SchedulerX, a managed scheduling platform.

shedlock : uses the open‑source ShedLock library with JDBC, Zookeeper, Redis, etc., for distributed locking.

Quick Start (Maven)

Add the starter dependency:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-schedulerx</artifactId>
    <version>{version}</version>
</dependency>

Configure the mode in application.properties or YAML:

spring.cloud.scheduling.distributed-mode=schedulerx   # or shedlock

Using Alibaba SchedulerX

Create a SchedulerX instance in the Alibaba Cloud console, obtain the access credentials, and use the provided web UI for task creation, monitoring, sharding and parallelism.

Using ShedLock (local open‑source)

Add JDBC lock dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version>
</dependency>

Configure the datasource (example for MySQL):

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

ShedLock creates a shedlock table at startup to coordinate task execution.

Sample Application

Enable scheduling and the lock manager:

@SpringBootApplication
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "3m")
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

Define jobs with @Scheduled and @SchedulerLock annotations:

@Component
public class SpringJob {
    @Scheduled(cron = "0 */5 * * * ?")
    @SchedulerLock(name = "SpringJob.job1", lockAtMostFor = "2m", lockAtLeastFor = "1m")
    public void job1() {
        System.out.println("time=" + DateTime.now().toString("YYYY-MM-dd HH:mm:ss") + " do job1...");
    }

    @Scheduled(fixedRate = 5000)
    @SchedulerLock(name = "SpringJob.job2", lockAtMostFor = "4s", lockAtLeastFor = "4s")
    public void job2() {
        System.out.println("time=" + DateTime.now().toString("YYYY-MM-dd HH:mm:ss") + " do job2...");
    }

    @Scheduled(fixedDelay = 5000)
    @SchedulerLock(name = "SpringJob.job3", lockAtMostFor = "4s", lockAtLeastFor = "4s")
    public void job3() throws InterruptedException {
        System.out.println("time=" + DateTime.now().toString("YYYY-MM-dd HH:mm:ss") + " do job3...");
        Thread.sleep(10000);
    }
}

Future Plans

The module will integrate additional open‑source schedulers such as Quartz and XXL‑Job, allowing a single @Scheduled method to run on different back‑ends and providing features like task sharding and concurrency limits.

References

Scheduling task module PR: https://github.com/alibaba/spring-cloud-alibaba/pull/3732

Spring Framework scheduling docs: https://docs.spring.io/spring-framework/docs/5.3.37/reference/html/integration.html#scheduling

Example project: https://github.com/alibaba/spring-cloud-alibaba/blob/2023.x/spring-cloud-alibaba-examples/spring-cloud-scheduling-example/README.md

ShedLock repository: https://github.com/lukas-krecan/ShedLock

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javadistributed schedulingmicroservicesSpring CloudShedLockSchedulerX
Alibaba Cloud Native
Written by

Alibaba Cloud Native

We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.