Boost Spring Boot Scheduled Tasks with Elastic Job Sharding

This tutorial explains how to use Elastic Job's sharding configuration in Spring Boot to accelerate scheduled task execution, improve resource utilization, and achieve high‑availability by distributing work across multiple job instances.

Programmer DD
Programmer DD
Programmer DD
Boost Spring Boot Scheduled Tasks with Elastic Job Sharding

In the previous article we introduced how to use Elastic Job to replace @Scheduled, solving competition issues and achieving high‑availability for scheduled tasks.

However, long‑running tasks and low resource utilization can still occur when many instances are started but only one instance executes the job.

We now continue by configuring Elastic Job sharding to speed up task execution and increase resource usage.

Hands‑on Try

Download the chapter7-2 project from the repository linked at the end of the article and modify it as needed. If you are not familiar with Elastic Job, review the previous tutorial first.

Step 1: Create a Sharding Job

@Slf4j
@Service
public class MyShardingJob implements SimpleJob {
    @Override
    public void execute(ShardingContext context) {
        switch (context.getShardingItem()) {
            case 0:
                log.info("Shard 1: executing task");
                break;
            case 1:
                log.info("Shard 2: executing task");
                break;
            case 2:
                log.info("Shard 3: executing task");
                break;
        }
    }
}

The switch statement determines which shard to run based on the shardingItem value, which starts from 0 and is set by the total shard count configured later.

Step 2: Configure the Job

elasticjob.jobs.my-sharding-job.elastic-job-class=com.didispace.chapter73.MyShardingJob
elasticjob.jobs.my-sharding-job.cron=0/5 * * * * ?
elasticjob.jobs.my-sharding-job.sharding-total-count=3

Setting sharding-total-count=3 splits the job into three shards, each corresponding to one case in the switch.

Run and Test

Single‑instance run

After building the code, start a single instance. Every 5 seconds the logs show all three shards executing on the same instance:

2021-07-21 17:42:05.254 INFO  ... : Shard 1: executing task
2021-07-21 17:42:05.254 INFO  ... : Shard 2: executing task
2021-07-21 17:42:05.254 INFO  ... : Shard 3: executing task

Dual‑instance run

Start a second instance (using -Dserver.port to change the port). The logs now show shards distributed across both instances, improving resource utilization:

Instance 1 logs:
2021-07-21 17:44:50.190 INFO ... : Shard 2: executing task

Instance 2 logs:
2021-07-21 17:44:50.272 INFO ... : Shard 1: executing task
2021-07-21 17:44:50.273 INFO ... : Shard 3: executing task

Adding or removing instances dynamically changes shard allocation, demonstrating how Elastic Job balances load across multiple nodes.

Feel free to experiment further and compare your configuration with the code examples provided.

The full project can be found in the chapter7-3 directory of the repository:

GitHub: https://github.com/dyc87112/SpringBoot-Learning/tree/master/2.x

Gitee: https://gitee.com/didispace/SpringBoot-Learning/tree/master/2.x

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.

Backend DevelopmentshardingSpring BootScheduled TasksElastic-Job
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.