Skip to content

Instantly share code, notes, and snippets.

@floriankraft
Last active March 9, 2021 18:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save floriankraft/3710f30f4eaf212410a3 to your computer and use it in GitHub Desktop.
Save floriankraft/3710f30f4eaf212410a3 to your computer and use it in GitHub Desktop.
OSGi EventHandler for AEM replication events, which makes use of a JobProcessor to avoid blacklisting of the EventAdmin.
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.event.jobs.JobProcessor;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import static org.apache.sling.event.jobs.JobUtil.processJob;
@Component(immediate = true)
@Service
@Property(name = "event.topics", value = { ReplicationAction.EVENT_TOPIC })
public class MyCoolEventHandler implements EventHandler, JobProcessor {
private static final Logger LOG = LoggerFactory.getLogger(MyCoolEventHandler.class);
/**
* Default constructor.
*/
public MyCoolEventHandler() {
super(LOG.getName());
}
@Override
public void handleEvent(final Event event) {
if (isInstanceInMode(INSTANCE_MODE.AUTHOR)) {
processJob(event, this);
}
}
@Override
public boolean process(Event event) {
ReplicationAction action = ReplicationAction.fromEvent(event);
LOG.debug("Processing event for {}", action.getPath());
if (action.getPath().contains(A_RELEVANT_PATH)) {
// Do something cool here
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment