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:
- 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.
- 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).
- 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
- Deprecate/Replace: Remove
SimpleScheduledTask.java or update it to follow the Sling Job pattern.
- Introduce
JobConsumer: Add a new sample SimpleJobConsumer.java implementing the JobConsumer interface.
- Scheduling Logic: Provide an example of how to trigger this job using the
JobManager or a ScheduledJob.
The current AEM Project Archetype includes a sample scheduled task (
SimpleScheduledTask.java) that implements thejava.lang.Runnableinterface 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
Runnableimplementation has several limitations in a modern AEM architecture:/system/console/jobs), providing much better visibility than hidden background threads.Proposed Changes
SimpleScheduledTask.javaor update it to follow the Sling Job pattern.JobConsumer: Add a new sampleSimpleJobConsumer.javaimplementing theJobConsumerinterface.JobManageror aScheduledJob.