HomeBackend Development › Schedulers & Sling Jobs

Schedulers & Sling Jobs

Purpose: Run background and recurring work correctly: scheduler vs job semantics, clustering, and idempotency.

Who this page is for

AudienceWhy it matters to you
Backend engineersAnything async or recurring

Two mechanisms, different guarantees

Scheduler (cron)Sling Jobs
TriggerTime-basedEvent/demand-based (job queue)
GuaranteeBest effort — missed if instance downAt least once — persisted, retried
Use forCache warmup, periodic sync, cleanupWork that must not be lost (send confirmation, sync one plan record)

Scheduled task

@Component(service = Runnable.class)
@Designate(ocd = PlanSyncTask.Config.class)
public class PlanSyncTask implements Runnable {
    @ObjectClassDefinition interface Config {
        @AttributeDefinition String scheduler_expression() default "0 0 2 * * ?"; // 2am daily
        @AttributeDefinition boolean scheduler_concurrent() default false;
    }
    @Override public void run() { /* sync plan data */ }
}

Job + consumer

// enqueue
jobManager.addJob("phi/plans/sync", Map.of("planId", "gold-hospital"));

// consume
@Component(service = JobConsumer.class,
           property = JobConsumer.PROPERTY_TOPICS + "=phi/plans/sync")
public class PlanSyncConsumer implements JobConsumer {
    @Override public JobResult process(Job job) {
        // do work; return JobResult.OK / FAILED (retry) / CANCEL (no retry)
    }
}

Operational rules

Quick navigation