Will AEM scheduler keep on doing its job after redeployment(no change in scheduler code) and after server restart? | Community
Skip to main content
shikhasoni1
Level 3
February 27, 2022
Solved

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

  • February 27, 2022
  • 3 replies
  • 1200 views
No text available
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Anmol_Bhardwaj

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.

3 replies

DEBAL_DAS
New Member
February 27, 2022

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;

/**
*
* @380 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
*
* @2007960 {@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
*
* @2007960 {@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
*
* @2007960 {@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
*
*
* @2007960 {@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;

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

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

@3214626
TaskNotificationService taskNotificationService;

@3214626
Scheduler scheduler;

private String contentDampath;
private String schedulerName;

@580286
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);
}

@9182423
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 ******");
}

}

@3038739
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);

}

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

}

}

Debal Das, Senior AEM Consultant
Anish-Sinha
Adobe Employee
Adobe Employee
February 28, 2022

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?

Anmol_Bhardwaj
Community Advisor
Anmol_BhardwajCommunity AdvisorAccepted solution
Community Advisor
February 28, 2022

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.