AEM
Created on:
Wednesday, October 10, 2018

AEM Replication Event Listeners

Replication Event Listener on Author Instance

import com.day.cq.replication.ReplicationAction;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Set;

@Component
@Service
@Property(name="event.topics",value= ReplicationAction.EVENT_TOPIC)
public class ReplicationListenerOnAuthor implements Runnable, EventHandler {

   private Logger log = LoggerFactory.getLogger(this.getClass());
   private BundleContext bundleContext;

   @Reference
   private SlingSettingsService slingSettingsService;

   @Override
   public void handleEvent(Event event) {
       if (isAuthor()) {
           // add your service to be executed on author instance here
           log.debug("Processing event {} with bundle context {}" , event, bundleContext);
            // i.e. DoSomeThingOnAuthor.execute(bundleContext, event);
       }
   }

   @Override
   public void run() {
       log.info("{} Running...", this.getClass().getName());
   }

   @Activate
   protected void activate(ComponentContext componentContext) {
       bundleContext = componentContext.getBundleContext();
   }

   @Deactivate
   protected void deactivate() {
       bundleContext = null;
   }

   private boolean isAuthor() {
       if (slingSettingsService != null) {
           Set<String> runModes = slingSettingsService.getRunModes();
           for (String runMode : runModes) {
               if (runMode.toLowerCase().equals("author")) {
                   return true;
               }
           }
       }
       return false;
   }
}

Replication Event Listener on Publish Instance

import com.day.cq.replication.ReplicationEvent;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Set;

@Component
@Service
@Property(name="event.topics",value= ReplicationEvent.EVENT_TOPIC)
public class ReplicationListenerOnPublish implements Runnable, EventHandler {

   private Logger log = LoggerFactory.getLogger(this.getClass());
   private BundleContext bundleContext;
   @Reference
   private SlingSettingsService slingSettingsService;

   @Override
   public void handleEvent(Event event) {
       if (isPublish()) {
           // add your service to be executed on publish instance here
            log.debug("Processing event {} with bundle context {}" , event, bundleContext);
           // i.e. DoSomeThingOnPublish.execute(bundleContext, event);
       }
   }

   @Override
   public void run() {
       log.info("{} Running...", this.getClass().getName());
   }

   @Activate
   protected void activate(ComponentContext componentContext) {
       bundleContext = componentContext.getBundleContext();
   }

   @Deactivate
   protected void deactivate() {
       bundleContext = null;
       FlushOnPublish.shutDown();
   }

   private boolean isPublish() {
       if (slingSettingsService != null) {
           Set<String> runModes = slingSettingsService.getRunModes();
           for (String runMode : runModes) {
               if (runMode.toLowerCase().equals("publish")) {
                   return true;
               }
           }
       }
       return false;
   }
}

Stop blending in with the rest of the crowd &
start leaving your mark on the web.
Your success is our success

Let's work together