Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to know if asset was replicated to preview or publish instance

Avatar

Level 4

I have a requirement to know after an asset is replicated whether it was replicated to the preview instance or the publish instance. Anyone has any idea how can I get this inside the Event handler?

The code below was implemented for another purpose. How can I implement the above-mentioned requirement in this?

 

@Override
    public void handleEvent(Event event) {
        processEventIfNeeded(event);
    }
    
    private void processEventIfNeeded(Event event) {
        
        ReplicationAction replicationAction = ReplicationAction.fromEvent(event);
        String eventType = replicationAction.getType().getName();
        String eventPath = replicationAction.getPath();

        if ((eventType.equals(REPLICATIONACTION_ACTIVATE) || eventType.equals(REPLICATIONACTION_DEACTIVATE))
                && eventPath.startsWith("/content/dam")) {
            processAssets(eventPath, eventType);
        }
    }

 

 

Topics

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

1 Accepted Solution

Avatar

Correct answer by
Level 4
            logger.info("START: Block one for replication agent check");
            ReplicationStatus afterStatus = resource.adaptTo(ReplicationStatus.class);
            ReplicationStatus previewStatus = afterStatus.getStatusForAgent("preview");
            ReplicationStatus publisherStatus = afterStatus.getStatusForAgent("publish");
            if(previewStatus!=null){
                logger.info("Asset {} is replicated to preview instance {}", assetPath, previewStatus.isActivated());
            }
            if(publisherStatus!=null) {
                logger.info("Asset {} is replicated to publish instance {}", assetPath, publisherStatus.isActivated());
            }
            logger.info("END: Block one for replication agent check");

            logger.info("START: Block two for replication agent check");
            List<String> agentIds = (List<String>) event.getProperty("agentIds");
            logger.info("Asset {} is replicated to preview instance {}", assetPath, agentIds.contains("preview"));
            logger.info("Asset {} is replicated to publish instance {}", assetPath, agentIds.contains("publish"));
            logger.info("END: Block two for replication agent check");

 

In the above mentioned code I tried two approached to identify the replication agent. The first block does not work as expected. It basically tells the current replication status of the resource for a certain instance ( preview/publisher). I needed to know during the replication trigger which instance it was triggered for. The second block does the exact thing (verified from the logs in the dev environment). So the code that worked for me is 

List<String> agentIds = (List<String>) event.getProperty("agentIds");

boolean isPreviewInstance = agentIds.contains("preview");

View solution in original post

8 Replies

Avatar

Community Advisor

Hi @Ankan_Ghosh 
Please check https://experienceleague.adobe.com/en/docs/experience-manager-cloud-service/content/operations/repli... 

 

ReplicationStatus afterStatus = enResource.adaptTo(ReplicationStatus.class); // afterStatus.isActivated == false
ReplicationStatus previewStatus = afterStatus.getStatusForAgent(PREVIEW_AGENT); // previewStatus.isActivated == true


Arun Patidar

Avatar

Level 4

The code I wrote based on the article you provided works well on my local setup, but will it function properly in the cloud environment?

List<String> agentIds = (List<String>) event.getProperty("agentIds");
logger.info("It is published to the PUBLISH isntance:  {}", agentIds.contains("publish"));

 

Avatar

Community Advisor

@Ankan_Ghosh For your above usecase , you can assign the replication of assets task from your Sling Event Handler to a Sling Job


The job returns a status of the job on completion. That way you can check if the replication operation was carried out successfully.

 

Also as @arunpatidar mentioned, you can directly check for an asset using ReplicationStatus api.

Avatar

Level 4

Hey @pulkitvashisth I have already used some jobs; my concern is to perform two different jobs based on which instance it was replicated. If the CF or any other asset is replicated to the preview instance, I need to perform a different job than if it is replicated to the publish instance. I am sorry if there has been any confusion in my question. 

 

And yes, @arunpatidar 's solution gives me the replication status of the resource. But even with this solution how will I know if the current replication Event was triggered for publish instance or preview? 

 

ReplicationStatus publishStatus = afterStatus.getStatusForAgent("publish"); // true on local
ReplicationStatus previewStatus = afterStatus.getStatusForAgent("preview"); // not true

 

This line returns true if the resource is published. Now, if the user replicates the resource to both the instance, it should return true for both. Now some modification happens and they decide to replicate it to preview, before pushing to publish. How will I know inside the event handler that this time the replication happened to preview and not publish? I need to index the data to a different source in the 3rd party search provider if the current replication was done to the preview instance than in case of publish instance. I am trying to understand if this is even possible. 

 

Avatar

Community Advisor

Hi @Ankan_Ghosh 
There are jcr properties, which will give you the hint, what publish where and when and you can check based on that of replication happend on preview first or publish.



Arun Patidar

Avatar

Administrator

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

Correct answer by
Level 4
            logger.info("START: Block one for replication agent check");
            ReplicationStatus afterStatus = resource.adaptTo(ReplicationStatus.class);
            ReplicationStatus previewStatus = afterStatus.getStatusForAgent("preview");
            ReplicationStatus publisherStatus = afterStatus.getStatusForAgent("publish");
            if(previewStatus!=null){
                logger.info("Asset {} is replicated to preview instance {}", assetPath, previewStatus.isActivated());
            }
            if(publisherStatus!=null) {
                logger.info("Asset {} is replicated to publish instance {}", assetPath, publisherStatus.isActivated());
            }
            logger.info("END: Block one for replication agent check");

            logger.info("START: Block two for replication agent check");
            List<String> agentIds = (List<String>) event.getProperty("agentIds");
            logger.info("Asset {} is replicated to preview instance {}", assetPath, agentIds.contains("preview"));
            logger.info("Asset {} is replicated to publish instance {}", assetPath, agentIds.contains("publish"));
            logger.info("END: Block two for replication agent check");

 

In the above mentioned code I tried two approached to identify the replication agent. The first block does not work as expected. It basically tells the current replication status of the resource for a certain instance ( preview/publisher). I needed to know during the replication trigger which instance it was triggered for. The second block does the exact thing (verified from the logs in the dev environment). So the code that worked for me is 

List<String> agentIds = (List<String>) event.getProperty("agentIds");

boolean isPreviewInstance = agentIds.contains("preview");