Expand my Community achievements bar.

Migrate Scheduler from AEM 6.5 to AEM Cloud

Avatar

Employee

Dear All,

 

I have one scheduler called asset test in my AEM6.5SP22 environment.

 

This scheduler runs at 8:30 AM, on every Monday. The scheduler scans through "/content/dam/asset zone" for all newly created assets, checks for missing metadata and sends notification email to the author who created the asset as shown below

 

********************** ASSET SCHEDULER IN AEM 6.5 ********************

@Designate(ocd = AssetTest.Config.class)
@Component(service = Runnable.class)
public class AssetTest implements Runnable {

  @ObjectClassDefinition(name = "Test Asset Creation Notifier", description = "Cron-job with SQL statement to send emails to users who uploaded assets. Expensive Query not to be run during business hours.")
  public static @interface Config {

    @AttributeDefinition(name = "Cron-job expression", description = "Cron-job task scheduled to run every monday at 8:30 AM")
    String scheduler_expression() default "0 30 8 ? * MON *"; 

    @AttributeDefinition(name = "Concurrent task", description = "Whether or not to schedule this task concurrently")
    boolean scheduler_concurrent() default false; 
  }

 

Now I want to migrate this scheduler to AEM Cloud. So do I need to update the code "or" I can migrate as it is ?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

4 Replies

Avatar

Community Advisor

@SunitaCh3 This should work just fine with AEM as cloud service.

Avatar

Level 6

Hi @SunitaCh3, there is no need for code changes since your configuration annotations are valid. Please test it in your local ASMasCS instance, and if you encounter any issues, feel free to ask here.

 

Avatar

Community Advisor

Hi @SunitaCh3 ,

 

Unfortunately, I faced some problems with schedulers that worked fine for AEM on-prem after the migration to AEMaaCS. Sometimes it was executed, sometimes - wasn't. I found similar topic: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/scheduled-jobs-not-execute... .

 

Long story short: you have to migrate your schedulers with code updates. See details below.

 

There is an official documentation: https://experienceleague.adobe.com/en/docs/experience-manager-cloud-service/content/implementing/dev... 

 

To minimize the trouble, long running jobs should be avoided if possible, and they should be resumable at a minimum. For executing such jobs, use Sling Jobs, which have an at-least-once guarantee and hence if they get interrupted will get re-executed as soon as possible. But they should probably not start from the beginning again. For scheduling such jobs, it is best to use the Sling Jobs scheduler as this again ensures the at-least-once execution.

Do not use the Sling Commons Scheduler for scheduling as execution cannot be guaranteed. It is just more likely that it is scheduled.

 

That's why, I would suggest guarantee of processing and scheduling of jobs.

@Component
public class MyJobScheduler {
    @Reference
    private JobManager jobManager;

    @Activate
    @Modified
    protected void activate() {
        startScheduledJob();
    }

    private void unScheduleExistingJobs() {
        Optional.ofNullable(jobManager.getScheduledJobs(TOPIC, 0, null))
                .orElse(List.of())
                .forEach(ScheduledJobInfo::unschedule);
    }

    private void startScheduledJob() {
        unScheduleExistingJobs();
        Collection<ScheduledJobInfo> scheduledJobs = jobManager.getScheduledJobs("my/topic", 0, null);
        if (!scheduledJobs.isEmpty()) {
            return;
        }
        JobBuilder.ScheduleBuilder scheduleBuilder = jobManager.createJob("my/topic").schedule();
        scheduleBuilder.cron(cronExpression);
        if (scheduleBuilder.add() == null) {
            return;
        }
    }
}

 

Best regards,

Kostiantyn Diachenko.

Avatar

Community Advisor

Hi @SunitaCh3 ,

Why Code Update is Necessary

In AEM 6.5 (on-prem), using @Component(service = Runnable.class) with OSGi Scheduler worked fine.

In AEMaaCS, Sling Commons Scheduler is not reliable for time-critical jobs like yours (email notifications at specific times).

Adobe officially recommends using Sling Jobs (JobManager API) for guaranteed, at-least-once execution.

Without updates, the job may not run consistently in AEM Cloud, causing missed executions.

(Sling Job + Cron Scheduling)

Step 1: Create the Scheduled Job

@Component(immediate = true)
public class AssetScheduler {

    private static final String JOB_TOPIC = "com/myproject/assetscan";

    @Reference
    private JobManager jobManager;

    @Activate
    @Modified
    protected void activate() {
        scheduleJob();
    }

    private void scheduleJob() {
        // Remove existing jobs (prevents duplicate schedules)
        jobManager.getScheduledJobs(JOB_TOPIC, 0, null)
                  .forEach(ScheduledJobInfo::unschedule);

        // Schedule the job
        JobBuilder.ScheduleBuilder builder = jobManager.createJob(JOB_TOPIC).schedule();
        builder.cron("0 30 8 ? * MON *"); // Every Monday at 8:30 AM
        builder.add();
    }
}

Step 2: Create the Job Consumer (Logic for Email Notification)

@Component(service = JobConsumer.class, immediate = true)
@Designate(ocd = AssetScanJob.Config.class)
public class AssetScanJob implements JobConsumer {

    interface Config {
        @AttributeDefinition(name = "Asset Folder Path", defaultValue = "/content/dam/asset zone")
        String assetPath();
    }

    @Reference
    private ResourceResolverFactory resolverFactory;

    @Override
    public JobResult process(Job job) {
        try (ResourceResolver resolver = resolverFactory.getServiceResourceResolver(null)) {
            // Logic to scan /content/dam/asset zone
            // Identify missing metadata
            // Send email to creator of each asset
        } catch (Exception e) {
            return JobResult.FAILED;
        }
        return JobResult.OK;
    }
}

Uses JobManager with cron, which is officially supported in AEM Cloud.

Provides at-least-once execution guarantee—your job will run even if AEM Cloud instance restarts.

Handles long-running jobs better than the old Runnable scheduler.

Scalable and resilient for cloud-based execution.

Migrate all on-prem Runnable schedulers to Sling Jobs using JobManager API in AEM Cloud for guaranteed execution. Adobe discourages use of Sling Commons Scheduler in AEMaaCS. Using JobManager.schedule().cron() ensures your job runs reliably at the scheduled time even with instance restarts or deployment changes.

Regards,
Amit