Skip to content

[Core] Replace legacy OSGi Scheduler (Runnable) with modern Sling Job Consumer #1330

Description

@bkkothari2255

The current AEM Project Archetype includes a sample scheduled task (SimpleScheduledTask.java) that implements the java.lang.Runnable interface for the OSGi Scheduler. While functional for basic tasks, this approach is considered a "legacy" pattern in modern AEM development, especially for AEM as a Cloud Service and distributed environments.

The AEM community and Adobe best practices now strongly recommend using Sling Jobs (JobConsumer) for background processing.

The existing Runnable implementation has several limitations in a modern AEM architecture:

  1. Guaranteed Execution: OSGi Schedulers are in-memory. If an instance restarts during execution, the task is lost. Sling Jobs are JCR-persisted, ensuring they resume after a restart.
  2. Distributed Safety: In a clustered environment (multiple Authors/Publishers), a simple OSGi Scheduler can trigger on all nodes simultaneously, leading to race conditions or VersionConflicts. Sling Jobs are "topology-aware" and ensure a job runs only on one designated node (usually the Leader).
  3. Observability: Sling Jobs can be monitored, stopped, and restarted via the Sling Jobs console (/system/console/jobs), providing much better visibility than hidden background threads.

Proposed Changes

  1. Deprecate/Replace: Remove SimpleScheduledTask.java or update it to follow the Sling Job pattern.
  2. Introduce JobConsumer: Add a new sample SimpleJobConsumer.java implementing the JobConsumer interface.
  3. Scheduling Logic: Provide an example of how to trigger this job using the JobManager or a ScheduledJob.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions