hi @helloosuman,
if possible I’d leverage the author instance as single orchestrator for sending content to the external application because it would really simplify the architecture logic and possible corner cases.
@Component(
service = EventHandler.class,
immediate = true,
property = {
EventConstants.EVENT_TOPIC + "=org/apache/sling/distribution/agent/package/distributed"
}
)
public class TopicReplicationHandler implements EventHandler {
@Override
public void handleEvent(Event event) {
// Extract path and verify it's a Topic
String[] paths = (String[]) event.getProperty("paths");
// Verify it's an activation/publish event
// Send to external application from Author (only once)
sendToExternalApp(paths);
}
private void sendToExternalApp(String[] paths) {
// Your external API call logic here
// This executes only once from Author
}
}
If Author is not viable then you can leverage Discovery Service to find the topology leader within a EventHandler:
@Component(
service = EventHandler.class,
immediate = true,
property = {
EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC
}
)
public class LeaderElectedTopicHandler implements EventHandler {
private static final Logger LOG = LoggerFactory.getLogger(LeaderElectedTopicHandler.class);
@Reference private DiscoveryService discoveryService;
@Override
public void handleEvent(Event event) {
// Only process if this instance is the leader
if (!isLeaderInstance()) {
LOG.debug("Skipping event - not the leader");
return;
}
// Process replication event and send to external app
sendToExternalApp(event);
}
/**
* Check if this instance is the cluster leader
*/
private boolean isLeaderInstance() {
try {
TopologyView topology = discoveryService.getTopology();
if (topology == null) {
LOG.warn("Topology is null - cannot determine leader status");
return false;
}
InstanceDescription localInstance = topology.getLocalInstance();
if (localInstance == null) {
LOG.warn("Local instance is null - cannot determine leader status");
return false;
}
boolean isLeader = localInstance.isLeader();
LOG.debug("Leader status: {}, Sling ID: {}",
isLeader, localInstance.getSlingId());
return isLeader;
} catch (Exception e) {
LOG.error("Error determining leader status", e);
return false;
}
}
}
This should works but, as corner case to evaluate, the topology can change during deployments and there may be brief periods with no leader or multiple leaders during transitions.