<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Monish Gavali on Medium]]></title>
        <description><![CDATA[Stories by Monish Gavali on Medium]]></description>
        <link>https://medium.com/@msgavali111?source=rss-2953472491f1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Monish Gavali on Medium</title>
            <link>https://medium.com/@msgavali111?source=rss-2953472491f1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 03:36:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@msgavali111/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AEM Scheduler]]></title>
            <link>https://medium.com/@msgavali111/aem-scheduler-3600c5c1dff8?source=rss-2953472491f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/3600c5c1dff8</guid>
            <category><![CDATA[aem]]></category>
            <category><![CDATA[aem-developer]]></category>
            <category><![CDATA[scheduler]]></category>
            <category><![CDATA[aem-scheduler]]></category>
            <category><![CDATA[adobe-experience-manager]]></category>
            <dc:creator><![CDATA[Monish Gavali]]></dc:creator>
            <pubDate>Fri, 16 May 2025 12:18:00 GMT</pubDate>
            <atom:updated>2025-05-16T12:18:00.976Z</atom:updated>
            <content:encoded><![CDATA[<p>A <strong>Scheduler in AEM</strong> is like setting an alarm clock ⏰ for your code.<br>You use it to <strong>automatically run specific tasks at set times</strong> — like sending a report every morning or cleaning up old data every night.</p><p>For example:</p><p>Imagine you want AEM to delete <strong>old log files every day at midnight</strong> — instead of doing it manually, you write a scheduler to do it automatically.</p><p>Scheduler can be implemented in two ways:</p><ol><li><strong>Whiteboard Pattern</strong><br>Creation of Scheduler using Runnable interface and overriding protected void activate() method.</li></ol><p>Example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*36sDlz0VWCefaTRn4sI16g.png" /></figure><p><strong>Note:</strong> The whiteboard pattern should be avoided for schedulers, as it uses deprecated annotations (org.apache.felix.scr.annotations.Property). Consider using the <strong>Scheduler API with OSGi annotations</strong> instead.</p><p><strong>2) Scheduler API</strong></p><p>Scheduler can be created using the <strong>Scheduler API (</strong><strong>org.apache.sling.commons.scheduler.Scheduler)</strong> directly using the @Reference annotation.</p><p>It is often combined with the <strong>OSGI Configurations</strong> (@Designate) for dynamic behavior.</p><p>Example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*7QFaPjERkj9LcpTnAUDG7w.png" /></figure><p><strong>Now, let’s go ahead and create a scheduler using the Scheduler API:</strong></p><p>1. Create OSGI configuration to provide properties values for scheduler.</p><ul><li>GeeksSchedulerConfiguration.java class.</li></ul><pre>package com.site.myproj.core.OSGIConfig;<br><br>import org.osgi.service.metatype.annotations.AttributeDefinition;<br>import org.osgi.service.metatype.annotations.AttributeType;<br>import org.osgi.service.metatype.annotations.ObjectClassDefinition;<br><br>@ObjectClassDefinition(<br>        name = &quot;AEM Geeks - Scheduler Configuration&quot;,<br>        description = &quot;Sling Scheduler Configuration&quot;<br>)<br>public @interface GeeksSchedulerConfiguration {<br><br>    @AttributeDefinition(<br>            name = &quot;Scheduler name&quot;,<br>            description = &quot;Enter the scheduler name&quot;,<br>            type = AttributeType.STRING<br>    )<br>    String schedulerName() default &quot;Scheduler Name&quot;;<br><br>    @AttributeDefinition(<br>            name = &quot;Cron Job&quot;,<br>            description = &quot;Enter the cron job to run the scheduler&quot;,<br>            type = AttributeType.STRING<br>    )<br>    String cronExpression() default &quot;8/20 * * * * ?&quot;; //Runs every 10 seconds</pre><p>This will create a configuration in <strong>Apache Felix Web Console</strong> and one config file as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5amfnFXaJcrqW_LjVTUhfA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*ybu0QsKuqWXQfZ7ZGXn69g.png" /></figure><p>2. Create a GeeksScheduler simple class implementing the Runnable class and override the run() method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*HSWCSD0tkwdudltOC_cBTA.png" /><figcaption>Scheduler in AEM</figcaption></figure><pre>package com.site.myproj.core.schedulers;<br><br>import com.site.myproj.core.OSGIConfig.GeeksSchedulerConfiguration;<br>import org.apache.sling.commons.scheduler.ScheduleOptions;<br>import org.apache.sling.commons.scheduler.Scheduler;<br>import org.osgi.service.component.annotations.Activate;<br>import org.osgi.service.component.annotations.Component;<br>import org.osgi.service.component.annotations.Deactivate;<br>import org.osgi.service.component.annotations.Reference;<br>import org.osgi.service.metatype.annotations.Designate;<br>import org.slf4j.Logger;<br>import org.slf4j.LoggerFactory;<br>@Component(<br>        service = Runnable.class,<br>        immediate = true<br>)<br>@Designate( ocd = GeeksSchedulerConfiguration.class )<br>public class GeeksScheduler implements Runnable{<br>    private static final Logger LOGGER = LoggerFactory.getLogger(GeeksScheduler.class);<br>    private int schedulerID;<br>    @Reference<br>    Scheduler scheduler;<br>    @Activate<br>    protected void activate( GeeksSchedulerConfiguration config ){<br>        schedulerID = config.schedulerName().hashCode();<br>        addScheduler(config);<br>    }<br>    // On deactivate component it will unschedule scheduler<br>    @Deactivate<br>    protected void deactivate( GeeksSchedulerConfiguration config ){<br>        scheduler.unschedule(String.valueOf(schedulerID));<br>    }<br>    // Configurations to Schedule a scheduler depending on name and expression<br>    public void addScheduler( GeeksSchedulerConfiguration config ){<br>        ScheduleOptions scheduleOptions = scheduler.EXPR(config.cronExpression());<br>        scheduleOptions.name(String.valueOf(schedulerID));<br>        scheduleOptions.canRunConcurrently(false);<br>        scheduler.schedule(this, scheduleOptions);<br>    }<br>    <br>    // Custom method to unschedule the scheduler<br>    public void removeScheduler(){<br>        scheduler.unschedule(String.valueOf(schedulerID));<br>    }<br>    // run() method will get call every 10 minutes<br>    @Override<br>    public void run() {<br>        LOGGER.info(&quot;------RUN METHOD EXECUTING------&quot;);<br>    }<br>}</pre><p>3. Deploy the code and check the component is in active state by opening below url</p><p><a href="http://localhost:4502/system/console/components">http://localhost:4502/system/console/components</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Eu-lvQEZwDTqUtS1dDDxjw.png" /></figure><p>4. You can either create a separate log file or check the <strong>error.log</strong> file to check the logs.</p><p><strong>RUN METHOD EXECUTING</strong> log is printing after every 10 seconds</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l8VjJXaRV_1O5T9Li5iPAQ.png" /></figure><p>I hope you enjoyed reading this article and found it insightful. Don’t forget to share it with your friends to pass on the knowledge. 😁✌️</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3600c5c1dff8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>