Hi everyone,
We have an event handler to handle the replication event and are doing some processing based on that. And we have a scheduled job to republish a list of pages. Now we don't want to get that event handler triggered when the replication event occurs by this job.
Is there a way to identify the replication event is coming from that particular job or any other way to achieve this?
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi @Jangra98 ,
Both the provided options are correct.
Just, if you need the example code:
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:
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);
}
}
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:
replication-service
.
Session serviceSession = repository.loginService("replication-service", null);
replicator.replicate(serviceSession, ReplicationActionType.ACTIVATE, pagePath);
@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
Hi @Jangra98
Not sure how your implementation looks like and of you can benefit from this, but maybe you can leverage the EventConstants.EVENT_FILTER property of the EventHandler which can filter events based on path or resource type.
Other option would be to have a flag property on the resource that you replicate, flag controlled byt the scheduled job and read by the event handler. Based on its value, the handle will process it or not. But with this you can prevent only the processing itself, but not the actual trigger of the handler.
Hi @Jangra98 ,
Make sure you start the scheduled job with a custom service user say technical_user and then put a check in your event handler that if the event is triggered by the same service user you do not do the processing.
Hope it helps.
Regards,
Anupam Patra
The user you use to create the resolver can be a different user just to cater to this use case, then you will always see the replicated by as this custom service user, so using it as a flag to handle the event handler will do it.
Hi @Jangra98 ,
Both the provided options are correct.
Just, if you need the example code:
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:
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);
}
}
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:
replication-service
.
Session serviceSession = repository.loginService("replication-service", null);
replicator.replicate(serviceSession, ReplicationActionType.ACTIVATE, pagePath);
@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
@Jangra98 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!
Views
Replies
Total Likes
Sure, will update it soon. And thank you @Tethich @anupampat @ChitraMadan for your valuable response.
Views
Likes
Replies