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();
}
}
}
}
0 Post a Comment:
Post a Comment