Expand my Community achievements bar.

SOLVED

Disable event handler during the scheduled job

Avatar

Level 3

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?


Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

6 Replies

Avatar

Level 9

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.

Avatar

Level 7

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

Avatar

Level 7

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.

Avatar

Correct answer by
Community Advisor

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

Avatar

Administrator

@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!



Kautuk Sahni

Avatar

Level 3

Sure, will update it soon. And thank you @Tethich @anupampat @ChitraMadan for your valuable response.