Hi @jangra98 ,
Both the provided options are correct.
Just, if you need the example code:
Use a Custom Property or Flag
When your scheduled job triggers replication, you can set a custom property or flag in the ReplicationOptions or add a unique metadata attribute to the event. Then, in the event handler, you can filter out the events that have this flag.
Steps:
- Modify the replication action in your scheduled job to include a unique identifier (e.g.,
isScheduledJob=true).ReplicationOptions replicationOptions = new ReplicationOptions();
replicationOptions.setFilter(new ReplicationFilter() {
@Override
public boolean accepts(ReplicationAction action) {
action.getMetaData().put("isScheduledJob", true);
return true;
}
});
replicator.replicate(session, ReplicationActionType.ACTIVATE, pagePath, replicationOptions
In your event handler, check for this property in the metadata and ignore the event if the flag exists:
@Override
public void onEvent(Event event) {
try {
if (event.getProperty("replicationMetadata.isScheduledJob") != null &&
event.getProperty("replicationMetadata.isScheduledJob").equals(true)) {
return; // Skip processing for scheduled job events
}
// Process other events
} catch (RepositoryException e) {
log.error("Error processing event", e);
}
}
Use a Dedicated User for Scheduled Jobs
If possible, configure the scheduled job to use a dedicated system user for replication. In the event handler, check the user who triggered the replication and ignore events triggered by the system user.
Steps:
- Ensure the scheduled job uses a specific service user, e.g.,
replication-service.
Session serviceSession = repository.loginService("replication-service", null);
replicator.replicate(serviceSession, ReplicationActionType.ACTIVATE, pagePath);
- In the event handler, check the triggering user:
@Override
public void onEvent(Event event) {
try {
String userId = event.getProperty("cq:lastReplicatedBy");
if ("replication-service".equals(userId)) {
return; // Skip processing for events triggered by the scheduled job
}
// Process other events
} catch (RepositoryException e) {
log.error("Error processing event", e);
}
}
Thanks,
Chitra