Expand my Community achievements bar.

SOLVED

Will AEM scheduler keep on doing its job after redeployment(no change in scheduler code) and after server restart?

Avatar

Level 4
 
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Yes.

Scheduler is nothing but just another OSGi service. You can look at it just like a servlet in this case, when we push code changes and then deploy, even if the server restarts, the servlets start running as soon as the bundle is active. 

Same with schedulers.

View solution in original post

3 Replies

Avatar

Employee Advisor

When we build any AEM scheduler we need to consider below points -

 

 

1. A scheduler will continue to run automatically even after the server reboot/redeployment. 

2. Remove the scheduler registered with old configuration.

3. Add the scheduler registered with new configuration.

 

Yes, it will be working. Hope this will help.

 

Please refer the below sample scheduler and here I am focusing my scheduler should execute it's job after redeployment and instance restart -

 

OSGi configuration associated with scheduler -

package com.aem.demo.core.configurations;

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

/**
*
* @author debal This is the configuration class that takes properties for a
* scheduler to run
*/

@ObjectClassDefinition(name = "TaskNotificationSchedulerConfiguration", description = "Task Notification scheduler configuration")
public @interface TaskNotificationSchedulerConfiguration {

/**
* This method will return the name of the Scheduler
*
* @Return {@link String}
*/
@AttributeDefinition(name = "Scheduler name", description = "Name of the scheduler", type = AttributeType.STRING)
public String schdulerName() default "Task Notification scheduler configuration";

/**
* This method will set flag to enable the scheduler
*
* @Return {@link Boolean}
*/

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

/**
* This method returns the Cron expression which will decide how the scheduler
* will run
*
* @Return {@link String}
*/

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

/**
* This method returns the Asset Path
*
*
* @Return {@link String}
*/

@AttributeDefinition(name = "Asset Path", description = "Asset Path", type = AttributeType.STRING)
public String assetPath() default "/content/dam";


}

 

Scheduler -

/**
*
*/
package com.aem.demo.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.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;

import com.aem.demo.core.configurations.TaskNotificationSchedulerConfiguration;
import com.aem.demo.core.services.TaskNotificationService;
import com.google.common.base.Strings;

/**
* @author debal
*
* A cron-job like tasks that get executed regularly.
*/
@component(service = Runnable.class, immediate = true)
@Designate(ocd = TaskNotificationSchedulerConfiguration.class)
public class TaskNotificationScheduler implements Runnable {

private final Logger logger = LoggerFactory.getLogger(TaskNotificationScheduler.class);

@reference
TaskNotificationService taskNotificationService;

@reference
Scheduler scheduler;

private String contentDampath;
private String schedulerName;

@activate
private void activate(TaskNotificationSchedulerConfiguration configguration) {

this.contentDampath = configguration.assetPath();
this.schedulerName = configguration.schdulerName();
logger.info("**** Task Notification Scheduler ****");
//This scheduler will continue to run automatically even after the server reboot, otherwise the scheduled tasks will stop running after the server reboot.
addScheduler(configguration);
}

@MODIFIED
protected void modified(TaskNotificationSchedulerConfiguration configguration) {
// Remove the scheduler registered with old configuration
removeScheduler(configguration);

contentDampath = configguration.assetPath();
// Add the scheduler registered with new configuration
addScheduler(configguration);

}

private void addScheduler(TaskNotificationSchedulerConfiguration configguration) {

boolean enabled = configguration.enabled();
if (enabled) {
ScheduleOptions scheduleOptions = scheduler.EXPR(configguration.cronExpression());

if (!Strings.isNullOrEmpty(schedulerName)) {
scheduleOptions.name(schedulerName);
scheduleOptions.canRunConcurrently(false);
scheduler.schedule(this, scheduleOptions);
logger.info("****** Task Notification Scheduler has been added successfully ******");

}

} else {
logger.info("****** Task Notification Scheduler is in disable state ******");
}

}

@deactivate
protected void deactivated(TaskNotificationSchedulerConfiguration configguration) {
logger.info("**** Removing Task Notification Scheduler Successfully on deactivation ****");
removeScheduler(configguration);
}

private void removeScheduler(TaskNotificationSchedulerConfiguration configguration) {

logger.info("**** Removing Task Notification Scheduler Successfully **** {}", schedulerName);
scheduler.unschedule(schedulerName);

}

@Override
public void run() {
taskNotificationService.setTaskNotification(contentDampath);
logger.info("******Inside Task Notification Scheduler ******");

}

}

Avatar

Employee Advisor

hi @shikhasoni1 , can you elaborate your question? Do you mean if the server restarts/redeployment happens while the scheduler job in progress, will it keep doing the job?

Avatar

Correct answer by
Community Advisor

Yes.

Scheduler is nothing but just another OSGi service. You can look at it just like a servlet in this case, when we push code changes and then deploy, even if the server restarts, the servlets start running as soon as the bundle is active. 

Same with schedulers.