Skip to main content

Day 19: Replication API in action


AEM Developer Series

Replication is an important concept in AEM as it enables an enterprise to author the content in the Author AEM instance and after it is final replicate to the Publish instance which is visible to the end-users of the website.

AEM provides a rich Replication API which replicates the content from AEM Author to the AEM Publish instance. Though AEM provides out of the box Replication agents which are super easy to configure and provides much abstraction for the replication process, sometimes we are required to achieve this functionality programmatically such as when asset metadata is changed, or a page is versioned any many other cases.

In this post, we will see how can we replicate content programmatically. For this, we will be creating a Sling Servlet (how? see here) and passing the content path which we want to replicate as a query parameter. So without wasting much time, let's get our hands dirty with some code.

Replication by code

For using the Replication API, we will be using an API com.day.cq.replication.Replicator to replicate the content. 
  • Create a class named ReplicationServlet and paste the following code in it.
  • First, we are getting the path of the path to be replicated as a request parameter.
  • All we need to do is to define a field of the type com.day.cq.replication.Replicator, let OSGi injects the reference to Replicator service.
  • The code is pretty simple as we are calling the replicate(Session session, ReplicationActionType type, String path) to replicate the content to the publish instance.
  • But wait a minute? What about the assets that are referenced on the page? How do we replicate them? 🤔 We have an awesome class com.day.cq.dam.commons.util.AssetReferenceSearch that will get the reference of the Assets and replicate them as well. Pretty cool stuff, eh? 🙋.
  • We pass page paths and assets paths and they are replicated one by one.
  • After deploying the code, go to the browser and hit http://<host>:<port>/bin/demo/replication?contentPath=/content/we-retail/language-masters/en/user and see the content on the publish.
  • The logs will look like this - 
2018-10-19 22:34:10 - INFO - publish : Creating content for path /content/we-retail/language-masters/en/user
2018-10-19 22:34:11 - INFO - publish : Creating content for path /content/dam/we-retail/en/experiences/arctic-surfing-in-lofoten/northern-lights.jpg
2018-10-19 22:34:11 - INFO - publish : Sending POST request to http://localhost:4503/bin/receive?sling:authRequestLogin=1
2018-10-19 22:34:11 - INFO - publish : sent. Response: 200 OK
2018-10-19 22:34:11 - INFO - publish : ------------------------------------------------
2018-10-19 22:34:11 - INFO - publish : Sending message to localhost:4503
2018-10-19 22:34:11 - INFO - publish : >> POST /bin/receive HTTP/1.0
2018-10-19 22:34:11 - INFO - publish : >> Action: Activate
2018-10-19 22:34:11 - INFO - publish : >> Path: /content/we-retail/language-masters/en/user
2018-10-19 22:34:11 - INFO - publish : >> Handle: /content/we-retail/language-masters/en/user
2018-10-19 22:34:11 - INFO - publish : >> Referer: about:blank
2018-10-19 22:34:11 - INFO - publish : >> ...spooling 2606 bytes...
2018-10-19 22:34:11 - INFO - publish : --
2018-10-19 22:34:11 - INFO - publish : << HTTP/1.1 200 OK
2018-10-19 22:34:11 - INFO - publish : << Date: Fri, 19 Oct 2018 17:04:11 GMT
2018-10-19 22:34:11 - INFO - publish : << X-Content-Type-Options: nosniff
2018-10-19 22:34:11 - INFO - publish : << Content-Type: text/plain;charset=utf-8
2018-10-19 22:34:11 - INFO - publish : << Content-Length: 30
2018-10-19 22:34:11 - INFO - publish : << 
2018-10-19 22:34:11 - INFO - publish : << ReplicationAction ACTIVATE ok.
2018-10-19 22:34:11 - INFO - publish : Message sent.
2018-10-19 22:34:11 - INFO - publish : ------------------------------------------------
2018-10-19 22:34:11 - INFO - publish : Replication (ACTIVATE) of /content/we-retail/language-masters/en/user successful.
2018-10-19 22:34:11 - INFO - publish : Sending POST request to http://localhost:4503/bin/receive?sling:authRequestLogin=1
2018-10-19 22:34:11 - INFO - publish : sent. Response: 200 OK
2018-10-19 22:34:11 - INFO - publish : ------------------------------------------------
2018-10-19 22:34:11 - INFO - publish : Sending message to localhost:4503
2018-10-19 22:34:11 - INFO - publish : >> POST /bin/receive HTTP/1.0
2018-10-19 22:34:11 - INFO - publish : >> Action: Activate
2018-10-19 22:34:11 - INFO - publish : >> Path: /content/dam/we-retail/en/experiences/arctic-surfing-in-lofoten/northern-lights.jpg
2018-10-19 22:34:11 - INFO - publish : >> Handle: /content/dam/we-retail/en/experiences/arctic-surfing-in-lofoten/northern-lights.jpg
2018-10-19 22:34:11 - INFO - publish : >> Referer: about:blank
2018-10-19 22:34:11 - INFO - publish : >> ...spooling 568041 bytes...
2018-10-19 22:34:11 - INFO - publish : --
2018-10-19 22:34:11 - INFO - publish : << HTTP/1.1 200 OK
2018-10-19 22:34:11 - INFO - publish : << Date: Fri, 19 Oct 2018 17:04:11 GMT
2018-10-19 22:34:11 - INFO - publish : << X-Content-Type-Options: nosniff
2018-10-19 22:34:11 - INFO - publish : << Content-Type: text/plain;charset=utf-8
2018-10-19 22:34:11 - INFO - publish : << Content-Length: 30
2018-10-19 22:34:11 - INFO - publish : << 
2018-10-19 22:34:11 - INFO - publish : << ReplicationAction ACTIVATE ok.
2018-10-19 22:34:11 - INFO - publish : Message sent.
2018-10-19 22:34:11 - INFO - publish : ------------------------------------------------
2018-10-19 22:34:11 - INFO - publish : Replication (ACTIVATE) of /content/dam/we-retail/en/experiences/arctic-surfing-in-lofoten/northern-lights.jpg successful.

Conclusion

Congratulations!! 🙋 today we saw a programmatic approach to the Replication API and published a page through it. I hope you enjoyed this post.

You can find the complete code of this project on my GitHub in this commit. Feel free to fork or open issues, if any.

I would love to hear your thoughts on this and would like to have suggestions from you to make it better. 

Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Happy Coding 😊

Comments

  1. Thanks for sharing this great and comprehensive article. Actually all your sharing are awesome!

    I was wondering that why the replication is succeeded while I don't even have a publish instance running? What happened to those replications?

    ReplyDelete
    Replies
    1. Thanks for the feedback. If your publish instance is not running then your payload will be accumulated in author instance's replication queue.

      Delete

Post a Comment

Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see that the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.

Adobe was way ahead in this war and have gained a lot of market capture. They are leaders in the Digital Marketing platforms since the 2014-15. One of the flagship product in its Digital Marketing suite is Adobe Experience Manager (AEM).

Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.

When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the …

Day 01: Introduction to AEM

Day 04: Developing first OSGi bundle