Event handler fails to activate properly- it needs to be restarted frequently to make it work
Hi,
Event handlers are failing to activate properly in higher environments. They work immediately after deployment, but after a day or two, they stop functioning again. I need to restart them manually to get them working.
I investigated the issue and found that starting the event handler before its referenced service is fully initialized could be a potential cause. To address this, I added @3214626(policyOption = ReferencePolicyOption.GREEDY), but it didn’t resolve the issue.
Any help or suggestions would be greatly appreciated.
import org.osgi.service.component.ComponentContext;
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.Reference;
import org.osgi.service.component.annotations.ReferencePolicyOption;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@8220494(
immediate = true,
service = EventHandler.class,
property = {
"event.topics" + "=" + "org/apache/sling/api/resource/Resource/*"
}
)
public class AbcEventHandler implements EventHandler {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private ExecutorService executorService;
@3214626 (policyOption = ReferencePolicyOption.GREEDY)
private AbcService abcService;
@9944223
public void handleEvent(Event event) {
if (abcService == null) {
logger.warn("Notifier service not available — skip event.");
return;
}
if (abcService.isValidEvent(event, false)) {
executorService.execute(new Runnable() {
@9944223
public void run() {
abcService.doSomething(event, false);
}
});
} else if (abcService.isValidPageChange(event, false)) {
executorService.execute(new Runnable() {
@9944223
public void run() {
abcService.doSomeTask(event, false);
}
});
}
logger.info("Event received: {} for path: {}", event.getTopic(), event.getProperty("path").toString());
}
@580286
protected void activate(ComponentContext componentContext) {
forceClose();
executorService = Executors.newFixedThreadPool(1);
logger.info("Created aaa Executor Service with 1 dedicated thread.");
}
@3038739
protected void deactivate() {
forceClose();
}
private void forceClose() {
logger.debug("aaa Executor Service :" + executorService);
if (executorService != null) {
logger.debug("executorService Shutdown? :" + executorService.isShutdown());
if (!executorService.isShutdown()) {
executorService.shutdownNow();
logger.info("Shutdown aaa Executor Service.");
}
}
}
}