Friday, November 6, 2020

Creating Custom OSGI Configuration for Scheduler

 

Creating Custom OSGI Configuration for Scheduler


SlingSchedulerConfiguration (Interface)

 

/* Here we define attributes for OSGI configuration. */


package com.community.config.core.schedulers;

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

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

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

 

@ObjectClassDefinition(name = "SlingSchedulerConfiguration", description = "Sling scheduler configuration")

 

public @interface SlingSchedulerConfiguration {

 

     @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 * * * * ?";

 

     @AttributeDefinition(name = "Custom Parameter", description = "Custom parameter to be displayed by the scheduler", type = AttributeType.STRING)

     public String customParameter() default "AEM Scheduler Demo";

}

 

 

 

CustomScheduler (Class)

 

 

/* Creating Service by Using @Component annotation */

 

@Component: It is the required annotation to declare java class as component, if this annotation is not declared for a Java class, the class is not declared as a component.

package com.community.config.core.schedulers;

 

import org.apache.sling.commons.scheduler.ScheduleOptions;

import org.apache.sling.commons.scheduler.Scheduler;

import org.osgi.service.component.annotations.Activate;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.ConfigurationPolicy;

import org.osgi.service.component.annotations.Deactivate;

import org.osgi.service.component.annotations.Modified;

import org.osgi.service.component.annotations.Reference;

import org.osgi.service.metatype.annotations.Designate;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

@Component(service = CustomScheduler.class, immediate = true, configurationPolicy = ConfigurationPolicy.REQUIRE)

@Designate(ocd = SlingSchedulerConfiguration.class)

public class CustomScheduler implements Runnable {

 

     @Reference

     private Scheduler scheduler;

     private String customParameter;

     private int schedulerId;

     private static final Logger log = LoggerFactory.getLogger(CustomScheduler.class);

 

     @Activate

     protected void activate(SlingSchedulerConfiguration config) {

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

           customParameter = config.customParameter();

     }

 

     @Modified

     protected void modified(SlingSchedulerConfiguration config) {

           removeScheduler();

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

           addScheduler(config);

     }

 

     @Deactivate

     protected void deactivate(SlingSchedulerConfiguration config) {

           removeScheduler();

     }

 

     private void removeScheduler() {

           scheduler.unschedule(String.valueOf(schedulerId));

     }

 

     private void addScheduler(SlingSchedulerConfiguration config) {

           if (config.enabled()) {

                ScheduleOptions scheduleOptions = scheduler.EXPR(config.cronExpression());

                scheduleOptions.name(config.schedulerName());

                scheduleOptions.canRunConcurrently(false);

                scheduler.schedule(this, scheduleOptions);

                log.info("Scheduler added");

           } else {

                log.info("Scheduler is disabled");

           }

     }

 

     @Override

     public void run() {

           log.info("Custom Scheduler is now running using the passed custom parameter, custom Parameter {}",

                     customParameter);

     }

 

}

 

Open http://localhost:4502/system/console/configMgr and search for SlingSchedulerConfiguration,

Here you can configure by providing custom configuration values as follows

 


No comments:

Post a Comment