Skip to main content

Day 13: Schedulers in AEM


AEM Developer Series

In simple terms, a scheduler is a service to schedule jobs. Thus if we want some work to happen at a particular time or at a regular interval, then Schedulers are the way to go.

In AEM, we can create scheduler in two ways -

  1. Whiteboard Pattern - In this, we create a Runnable thread to perform our task. This is similar to the Java Thread concept.
  2. Scheduler API - In this, we use Apache Commons' Scheduler API to perform our task. It uses open-source Quartz library.
Both of these methods can be seen here. However, from AEM 6.2 onwards Felix SCR annotations are deprecated and are replaced with OSGi R6 annotations. Therefore, in this post, we will be discussing the clean way of creating schedulers that OSGi R6 annotations provide.

Scheduler in AEM

To create a scheduler in AEM, we will follow the below steps - 
  • Create an OSGi configuration to read the scheduler specific values from the user i.e. cron expression, the name of the scheduler, custom parameter etc.
  • Create a sling scheduler which displays the custom parameter at an interval specified by the cron expression.
Without making further delay, let's dive into our code

Create an OSGi configuration

Follow this post to create a new OSGi configuration class and paste the following code in it.

Here we are getting configuration properties for the Sling Scheduler.

Create Scheduler

Now we are going to create a sling scheduler. Create a class named CustomScheduler and paste the following code in it.
  • Let us understand the code step-by-step. First, we are registering the class as a service and implementing the Runnable interface. At the same time, using @Desginate annotation, we are linking the OSGi configuration created in the previous section with this class.
  • Now, we are injecting the org.apache.sling.commons.scheduler.Scheduler dependency.
  • In the activate() method, we are reading the required values. Then we are getting the schedulerId from the scheduler name.
  • The modified() method recalculates the schedulerId in case the OSGi configuration is modified.
  • In the addScheduler() method, we are registering the scheduler using the Scheduler API.
  • The run() method will be defining our task. Here we are just printing the customParameter in the logs.

Configuring OSGi

  • Go to ./system/console/configMgr and search for SlingSchedulerConfiguration and configure it like below screenshot.
OSGi Configuration

  • Save the configuration.

Result

Now go to your project-specific log (my log file is - project-demoproject.log) and look for the traces, you will find like below

org.redquark.demo.core.schedulers.CustomScheduler Scheduler added

org.redquark.demo.core.schedulers.CustomScheduler Custom Scheduler is now running using the passed custom parameter, customParameter AEM Scheduler Demo

org.redquark.demo.core.schedulers.CustomScheduler Custom Scheduler is now running using the passed custom parameter, customParameter AEM Scheduler Demo

org.redquark.demo.core.schedulers.CustomScheduler Removing scheduler: -1760311957

org.redquark.demo.core.schedulers.CustomScheduler Scheduler added

org.redquark.demo.core.schedulers.CustomScheduler Custom Scheduler is now running using the passed custom parameter, customParameter AEM Scheduler Demo

org.redquark.demo.core.schedulers.CustomScheduler Custom Scheduler is now running using the passed custom parameter, customParameter AEM Scheduler Demo

Conclusion

Congratulations!! 🙋 we have successfully created a Sling Scheduler and I hope you enjoyed this post.

You can find the complete code of this project on my GitHub in this commit. Feel free to fork or open issues, if any.

I would love to hear your thoughts on this and would like to have suggestions from you to make it better. 

Feel free to befriend me on Facebook, Twitter or Linked In or say Hi by email.

Happy Coding 😃

Comments

  1. Well explained . Great work. Hope this code will be executed AEM 6.3. Let me try this.
    Thanks
    Shahid

    ReplyDelete
    Replies
    1. Thanks Shahid for your valuable feedback. I am sure this will work on your AEM 6.3 as the only difference is the use of OSGi annotations instead of Felix SCR annotations. OSGi annotations are supported in AEM 6.3.
      Do let me know in case you face issues executing the code.
      Cheers!

      Delete
  2. Thanks for this great article. Huge help on a project that I'm currently working on.

    ReplyDelete

Post a Comment

Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.
Adobe is way ahead in this war and has gained a lot of market capture. They are leaders in the Digital Marketing platforms since 2014-15. One of the flagship product in Adobe's Digital Marketing suite is Adobe Experience Manager (AEM).
Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.
When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the res…

Day 01: Introduction to AEM

Day 04: Developing first OSGi bundle