First I would like to ask few questions here -
1. Are you facing this issue since long time ?
2. Have you written this scheduler with OSGi R6 annotation?
3. Have you made any changes in scheduler recently and deploys the same without modifying the cronjob expression?
I had similar experience with scheduler and able to fix the issue.
Sharing my demo scheduler and Implementation class as reference , please review code comments and I believe it will help you to debug the issue -
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 ******");
}
}
Impl -
/**
*
*/
package com.aem.demo.core.services.impl;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.granite.taskmanagement.Task;
import com.adobe.granite.taskmanagement.TaskManager;
import com.adobe.granite.taskmanagement.TaskManagerException;
import com.adobe.granite.taskmanagement.TaskManagerFactory;
import com.adobe.granite.workflow.exec.InboxItem;
import com.aem.demo.core.services.TaskNotificationService;
import com.drew.lang.annotations.Nullable;
import com.google.common.base.Strings;
/**
* @380 debal
*
* This Implementation class is responsible to responsible to set/send
* notification in AEM inbox in case we set expiration datetime of an
* asset after current date time. Task Management in AEM is the set of
* APIs that manage Tasks, which can show up in AEM user's inbox. This
* can be done using Task and TaskManager API
*/
@8220494(service = TaskNotificationService.class, immediate = true)
public class TaskNotificationServiceImpl implements TaskNotificationService {
private final Logger logger = LoggerFactory.getLogger(TaskNotificationServiceImpl.class);
public static final String NOTIFICATION_TASK_TYPE = "Notification";
@3214626
ResourceResolverFactory resourceResolverFactory;
@9944223
public void setTaskNotification(String assetPath) {
ResourceResolver serviceResourceResolver = getResourceResolver();
try {
TaskManager taskManager = serviceResourceResolver.adaptTo(TaskManager.class);
TaskManagerFactory taskManagerFactory = taskManager.getTaskManagerFactory();
if (!Strings.isNullOrEmpty(assetPath)) {
Resource resource = serviceResourceResolver.getResource(assetPath);
if (Objects.nonNull(resource) && resource.isResourceType("dam:Asset")) {
Resource metadataresource = resource.getChild("jcr:content/metadata");
ModifiableValueMap modifiableValueMap = metadataresource.adaptTo(ModifiableValueMap.class);
@3146596
String creatorName = modifiableValueMap.get("dc:creator", String.class);
@3146596
Date expirydate = modifiableValueMap.get("prism:expirationDate", Date.class);
logger.info("*** Expiration Date ***{}", expirydate);
Date currentDate = new Date();
if (Objects.nonNull(expirydate) && !Strings.isNullOrEmpty(creatorName)) {
if (expirydate.after(currentDate)) {
Task newTask = taskManagerFactory.newTask(NOTIFICATION_TASK_TYPE);
newTask.setName("Content Expiry Notification");
newTask.setContentPath(assetPath);
// Optionally set priority (High, Medium, Low)
newTask.setPriority(InboxItem.Priority.HIGH);
newTask.setDescription("Content Expiry Notification");
newTask.setInstructions("Content Expiry Notification");
newTask.setCurrentAssignee(creatorName);
taskManager.createTask(newTask);
}
}
}
}
} catch (TaskManagerException te) {
logger.error("Could not create task {} ", te.getMessage());
} finally {
if (Objects.nonNull(serviceResourceResolver)) {
serviceResourceResolver.close();
}
}
}
private ResourceResolver getResourceResolver() {
Map<String, Object> map = new HashMap<String, Object>();
map.put(resourceResolverFactory.SUBSERVICE, "readWriteService");
ResourceResolver serviceResourceResolver = null;
try {
serviceResourceResolver = resourceResolverFactory.getServiceResourceResolver(map);
} catch (LoginException e) {
logger.error("Could not get service user [ {} ]", "demoSystemUser", e.getMessage());
}
return serviceResourceResolver;
}
}
Interface -
/**
*
*/
package com.aem.demo.core.services;
/**
* @380 debal
*
* This service is responsible to set/send notification in AEM inbox
*/
public interface TaskNotificationService {
public void setTaskNotification(String assetPath);
}
Please review the comments at code level.