How apache/shardingsphere-elasticjob works
Purpose and Build Shape
ElasticJob is a Java 8+ distributed scheduled-job framework. Applications define a job once and run coordinated, sharded executions across multiple nodes. The Maven reactor is rooted at pom.xml; main deployable/library entry points are bootstraps, Spring integrations, and optional ecosystem modules.
Public Contract and Application Entry Points
The stable job-facing API defines job configuration, executable jobs, sharding context, listeners, error handling, tracing, and YAML conversion:
• api/src/main/java/org/apache/shardingsphere/elasticjob/api/ElasticJob.java
• api/src/main/java/org/apache/shardingsphere/elasticjob/api/JobConfiguration.java
• api/src/main/java/org/apache/shardingsphere/elasticjob/spi/executor/item/param/ShardingContext.java
Jobs are registered through one-off or scheduled bootstrap paths:
• bootstrap/src/main/java/org/apache/shardingsphere/elasticjob/bootstrap/type/OneOffJobBootstrap.java
• bootstrap/src/main/java/org/apache/shardingsphere/elasticjob/bootstrap/type/ScheduleJobBootstrap.java
Spring Boot auto-configuration is an alternative application entry point:
• spring/boot-starter/src/main/java/org/apache/shardingsphere/elasticjob/spring/boot/job/ElasticJobAutoConfiguration.java
• spring/boot-starter/src/main/java/org/apache/shardingsphere/elasticjob/spring/boot/job/ScheduleJobBootstrapStartupRunner.java
Scheduling Kernel
The kernel initializes a job, maintains its local registry, schedules triggers, and executes assigned work. JobScheduler/SchedulerFacade bridge setup and runtime scheduling; ElasticJobExecutor runs the resulting execution:
• kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/schedule/JobScheduler.java
• kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/schedule/SchedulerFacade.java
• kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/executor/ElasticJobExecutor.java
Per-item execution is isolated behind a factory and managed executor service, enabling configurable concurrency and reloadable thread-pool behavior:
• kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/executor/item/JobItemExecutorFactory.java
• kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/executor/threadpool/ElasticJobExecutorService.java
Distributed Coordination and Sharding
The registry-center abstraction is the critical distributed boundary: it stores configuration/state, exposes watches and transactions, and supports leader election. Concrete providers integrate ZooKeeper/Curator or etcd:
• registry-center/api/src/main/java/org/apache/shardingsphere/elasticjob/reg/base/CoordinatorRegistryCenter.java
• registry-center/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperRegistryCenter.java
• registry-center/provider/etcd/src/main/java/org/apache/shardingsphere/elasticjob/reg/etcd/EtcdRegistryCenter.java
Kernel services use that state to register instances/servers, elect leaders, allocate sharding items, monitor execution, reconcile failures, and perform failover:
• kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/sharding/ShardingService.java
• kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/failover/FailoverService.java
• kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/election/LeaderService.java
Control flow is: bootstrap configuration → registry setup/instance registration → scheduling trigger → sharding/execution-context creation → concurrent item execution → registry status updates and recovery listeners.
Job Execution Ecosystem
The kernel selects implementations through SPI contracts, separating scheduler mechanics from job semantics. Built-in ecosystem modules provide simple Java jobs, dataflow jobs, shell scripts, and outbound HTTP jobs:
• ecosystem/executor/simple/src/main/java/org/apache/shardingsphere/elasticjob/simple/executor/SimpleJobExecutor.java
• ecosystem/executor/dataflow/src/main/java/org/apache/shardingsphere/elasticjob/dataflow/executor/DataflowJobExecutor.java
• ecosystem/executor/http/src/main/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutor.java
SPI registrations under each module’s META-INF/services make extensions classpath-discoverable. Failures cross the same boundary into configurable handlers, including logging/throwing behavior and DingTalk, email, or WeChat notification integrations.
Observability and Operations
Execution/status events are published through the kernel tracing event bus and consumed by pluggable tracing listeners. The RDB module persists events through JDBC and database-specific SQL mappings:
• kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/tracing/event/JobTracingEventBus.java
• ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/listener/RDBTracingListener.java
• ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/storage/repository/RDBJobEventRepository.java
Programmatic operational APIs read or mutate registry-backed job, server, and sharding state:
• lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lifecycle/api/JobAPIFactory.java
• lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lifecycle/api/JobOperateAPI.java
Integration and Runtime Boundaries
Spring Core supports scanning/proxy-aware job discovery; the XML namespace and Boot starter translate container configuration into bootstraps. A standalone Netty-based REST framework exists as a reusable module, not the scheduler’s primary runtime:
• spring/core/src/main/java/org/apache/shardingsphere/elasticjob/spring/core/scanner/ClassPathJobScanner.java
• spring/namespace/src/main/java/org/apache/shardingsphere/elasticjob/spring/namespace/ElasticJobNamespaceHandler.java
• restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/NettyRestfulService.java
External infrastructure is ZooKeeper (documented required; native metadata targets ZooKeeper 3.9.5), optionally etcd, plus an optional relational database for tracing. GraalVM native-image reachability metadata supports native deployments:
• reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere.elasticjob/elasticjob-reachability-metadata/reachability-metadata.json
Main entry points: CoordinatorRegistryCenter.java Registry center API · ElasticJobExecutor.java Job executor · ShardingService.java Sharding service · ScheduleJobBootstrap.java Job bootstraps
Components 21
Application integration
- Job API · Defines jobs, configuration, and sharding context.
api/src/main/java/org/apache/shardingsphere/elasticjob/api/ElasticJob.java - Job configuration
api/src/main/java/org/apache/shardingsphere/elasticjob/api/JobConfiguration.java - Sharding context
api/src/main/java/org/apache/shardingsphere/elasticjob/spi/executor/item/param/ShardingContext.java - Job bootstraps · One-off and scheduled registration paths.
bootstrap/src/main/java/org/apache/shardingsphere/elasticjob/bootstrap/type/ScheduleJobBootstrap.java - Spring Boot integration · Translates Boot configuration into job bootstraps.
spring/boot-starter/src/main/java/org/apache/shardingsphere/elasticjob/spring/boot/job/ElasticJobAutoConfiguration.java
Scheduling kernel
- Job scheduler · Initializes jobs and local scheduling.
kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/schedule/JobScheduler.java - Scheduler facade
kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/schedule/SchedulerFacade.java - Sharding service · Allocates items from registry state.
kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/sharding/ShardingService.java - Leader service
kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/election/LeaderService.java - Failover service
kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/failover/FailoverService.java - Job executor · Creates execution contexts and runs assigned work.
kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/executor/ElasticJobExecutor.java - Item executor factory
kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/executor/item/JobItemExecutorFactory.java - Managed executor service · Supports configurable, reloadable item concurrency.
kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/executor/threadpool/ElasticJobExecutorService.java - Tracing event bus
kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/tracing/event/JobTracingEventBus.java
Distributed coordination
- Registry center API · Registry state, watches, transactions, and leader election.
registry-center/api/src/main/java/org/apache/shardingsphere/elasticjob/reg/base/CoordinatorRegistryCenter.java - ZooKeeper provider
registry-center/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperRegistryCenter.java - etcd provider
registry-center/provider/etcd/src/main/java/org/apache/shardingsphere/elasticjob/reg/etcd/EtcdRegistryCenter.java
Execution and observability
- Job executor SPI · Simple, dataflow, HTTP, and other classpath-discovered job semantics.
ecosystem/executor/simple/src/main/java/org/apache/shardingsphere/elasticjob/simple/executor/SimpleJobExecutor.java - RDB tracing listener · Persists execution events through JDBC.
ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/listener/RDBTracingListener.java - Lifecycle operations API · Reads and mutates registry-backed job state.
lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lifecycle/api/JobOperateAPI.java - Native reachability metadata
reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere.elasticjob/elasticjob-reachability-metadata/reachability-metadata.json
Connections 23
- Job API → Job bootstraps: implemented and registered by
- Job configuration → Job bootstraps: configures
- Spring Boot integration → Job bootstraps: creates
- Job bootstraps → Job scheduler: initializes
- Job scheduler → Scheduler facade: configures
- Job scheduler → Registry center API: registers instance and state
- Scheduler facade → Sharding service: triggered execution requests shards
- Sharding service → Leader service: coordinates allocation leadership
- Sharding service → Registry center API: reads and writes shard state
- Leader service → Registry center API: elects through
- Failover service → Registry center API: reconciles failures
- Sharding service → Job executor: builds assigned execution
- Job executor → Item executor factory: creates item tasks
- Item executor factory → Managed executor service: submits concurrent work
- Job executor → Sharding context: supplies
- Job executor → Job executor SPI: dispatches through SPI
- Job executor → Registry center API: updates execution status
- Job executor → Tracing event bus: publishes events
- Tracing event bus → RDB tracing listener: delivers events
- Registry center API → ZooKeeper provider: implemented by
- Registry center API → etcd provider: implemented by
- Lifecycle operations API → Registry center API: operates on
- Native reachability metadata → Spring Boot integration: supports native deployment of