Automating/Performing tasks on replication of a page from AEM Author

Today in this article we have a look on how we can basically automate/perform any task/tasks on the publication/replication of a page from our author instance. We are going to make use of Event Handler here to achieve this goal.

So we just need to implement the interface EventHandler in our Java class by adding below import statement:

import org.osgi.service.event.EventHandler;


So your code after implementing the interface will look something like below:

package com.kpmg.core.utils;

import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.*;
import com.day.cq.replication.ReplicationAction;

/**
 * @author Nikhil Kumar
 *
 *         Event Handler that listens to the Sling events
 */
@Component(immediate = true, service = EventHandler.class, property = {
		Constants.SERVICE_DESCRIPTION + "= This event handler listens the events on page activation",
		EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC})
public class OnPublishEvent implements EventHandler {

	private static final Logger log = LoggerFactory.getLogger(OnPublishEvent.class);


	@Reference
	private ResourceResolverFactory resourceResolverFactory;

	@Override
	public void handleEvent(Event event) {

		final ReplicationAction action = ReplicationAction.fromEvent(event);

		if (action != null) {
			log.info("Event Added triggered from: {}", action.getPath());
			
			/*
			
			YOUR AUTOMATED TASKS GOES HERE, WHICH WILL BE TRIGGERED EVERYTIME A PAGE IS PUBLISHED.
			
			*/	
		}

	}

}

Now here in above code if you see, we are using EVENT TOPIC to be of type com/day/cq/replication. Which means we want this Java class to be called when any replication happens from author instance.

As we can see in above Java class, we need to override the handleEvent function to capture the event which has occurred and from that event we can get different information as per our need. For example, in above class we have used it to get the action which happened on the event. And from that action we are getting the path on which the action (i.e, replication/publish) happened. Which will give us the path of the page which was replicated/published.

Now you can use it many use cases where you have to make API calls on publish of a page. For instance, you want to index the data of a published page on some 3rd party search tool, then you can make API calls using Event Handler on page publication.

Thank you for reading.
Happy Coding!


Author:

I am Nikhil Kumar, AEM developer. Working on AEM since the start of my career. Created this blog to share my AEM knowledge and give back to the AEM Community. You can reach out to me on any query.

Leave a comment