AEM 6510 - Sample Sling Scheduler

Goal


Create a sample Sling Scheduler

Package Install | Github

                          http://localhost:4502/system/console/configMgr/apps.experienceaem.assets.SampleScheduler




Solution


1) Create Scheduler configuration apps.experienceaem.assets.SampleSchedulerConfiguration

package apps.experienceaem.assets;

import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(
        name = "Experience AEM: SlingSchedulerConfiguration",
        description = "Sling scheduler configuration"
)
public @interface SampleSchedulerConfiguration {

    @AttributeDefinition(
            name = "Scheduler name",
            description = "Name of the scheduler",
            type = AttributeType.STRING)
    public String schedulerName() default "Custom Sling Scheduler Configuration";

    @AttributeDefinition(
            name = "Enabled",
            description = "True, if scheduler service is enabled",
            type = AttributeType.BOOLEAN)
    public boolean enabled() default false;

    @AttributeDefinition(
            name = "Cron Expression",
            description = "Cron expression used by the scheduler",
            type = AttributeType.STRING)
    public String cronExpression() default "0/10 * * * * ?"; // runs every 10 seconds

    @AttributeDefinition(
            name = "Custom Path Parameter",
            description = "Custom path parameter to be used by the scheduler",
            type = AttributeType.STRING)
    public String customPathParameter() default "/content/dam";
}


2) Create Scheduler class apps.experienceaem.assets.SampleScheduler implementing Runnable.class

package apps.experienceaem.assets;

import org.apache.sling.commons.scheduler.ScheduleOptions;
import org.apache.sling.commons.scheduler.Scheduler;
import org.osgi.service.component.annotations.*;
import org.osgi.service.metatype.annotations.Designate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate = true, service = Runnable.class)
@Designate(ocd = SampleSchedulerConfiguration.class)
public class SampleScheduler implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(SampleScheduler.class);

    private String customPathParameter;
    private int schedulerId;

    @Reference
    private Scheduler scheduler;

    @Activate
    protected void activate(SampleSchedulerConfiguration config) {
        schedulerId = config.schedulerName().hashCode();
        customPathParameter = config.customPathParameter();

        addScheduler(config);
    }

    @Deactivate
    protected void deactivate(SampleSchedulerConfiguration config) {
        removeScheduler();
    }

    @Modified
    protected void modified(SampleSchedulerConfiguration config) {
        removeScheduler();

        schedulerId = config.schedulerName().hashCode();

        addScheduler(config);
    }


    private void removeScheduler() {
        scheduler.unschedule(String.valueOf(schedulerId));
    }

    /**
     * This method adds the scheduler
     *
     * @param config
     */
    private void addScheduler(SampleSchedulerConfiguration config) {
        if(config.enabled()) {
            ScheduleOptions scheduleOptions = scheduler.EXPR(config.cronExpression());
            scheduleOptions.name(config.schedulerName());
            scheduleOptions.canRunConcurrently(false);

            scheduler.schedule(this, scheduleOptions);
            LOGGER.info("Experience AEM Scheduler added");
        } else {
            LOGGER.info("Experience AEM Scheduler disabled");
        }
    }

    public void run() {
        LOGGER.info("Experience AEM, customPathParameter {}", customPathParameter);
    }
}


No comments:

Post a Comment