CODE FOR A FICTION

Syntax of Imagination.

Monday, April 15, 2024

How do we capture the Activation/Deactivation Events on AEM Publisher Instance.




In Adobe Experience Manager (AEM), content is replicated from the author instance to the publish instances whenever authors publish anything. This process can be thought of as copying a page to the publish instance in layman’s terms.

When we want to capture these events on the publish instance while writing our event handlers, we might get confused and use ReplicationAction.EVENT_TOPIC. However, this is not correct. The key difference lies in the event topics for the author and publisher instances. For the author's instance, the event topic is com.day.cq.replication.ReplicationAction, while for the publisher instance, the event topic is com.day.cq.replication.ReplicationEvent.

The names themselves give us a fair idea of their roles. In the author instance, the replication process is triggered or actually started. We configure replication agents on the author instance itself. On the other hand, the publish instance is where we receive the pages or assets. Therefore, the author instance is associated with the replication action, and the publish instance is associated with the replication event. I hope this clarifies the difference! 😊

Here is a sample code : 
import com.day.cq.replication.ReplicationEvent;
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.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

@Component(
    service = EventHandler.class,
    immediate = true,
    property = {
        "event.topics=" + ReplicationEvent.EVENT_TOPIC
    }
)
public class CustomReplicationEventHandler implements EventHandler {

    @Reference
    private ResourceResolverFactory resolverFactory;

    @Override
    public void handleEvent(final Event event) {
        ReplicationEvent replicationEvent = ReplicationEvent.fromEvent(event);

        if (replicationEvent.getType().equals(ReplicationEvent.Action.ACTIVATE) || 
            replicationEvent.getType().equals(ReplicationEvent.Action.DEACTIVATE)) {
            try (ResourceResolver resolver = resolverFactory.getServiceResourceResolver(null)) {
                // Your custom logic here
                // For example, you might log the path of the replicated resource:
                System.out.println("Resource replicated: " + replicationEvent.getPath());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}


In this example, I have created an OSGi component that listens for replication events on publisher. When a replication event occurs, the handleEvent method is called. Inside this method, I am checking if the replication event type is either ACTIVATE or DEACTIVATE. If it is, we’re logging the path of the replicated resource.
Share:

0 Post a Comment:

Post a Comment

Comments

Belive in yourself

Code for a fiction