Contents
Objective
After reading this Article, You should have an Understanding of –
- Replication via a selected replication agent in Java
Introduction
In AEM, sometimes we come across the scenario of replicating the content with a specific replication agent. For example, if we want to replicate the content from one instance to another AEM instance, we can’t achieve this with the default replication agent. To achieve this, we have to create the replication agent and replicate with the help of the Replication API and Replication Options.
Hence, without any further delay, let’s get started:
Custom Replication Agent Creation
We can create the replication agent following the below steps:
- Navigate to miscadmin tool and select the replication agent on Author – http://localhost:4502/miscadmin#/etc/replication/agents.author
- Click on Create, insert the title, select the template as Replication Agent, and click on Create
- Now edit the replication agent to configure it.
- In the Setting Tab, Enabled = true, Serialization Type = default, Retry Delay = 6000, Log Level = info.
- In the Transport Tab, configure proper URI, User Name, and Password.
- In the Triggers Tab, Ignore Default = true and save the changes.
- Check the test connection and if you get a success message, that’s it. We are done.
- If we encounter an issue, we can view the log by clicking View log.
Workflow Process Step for Replication via Custom Replication Agent
AEM provides a Replication API which replicates the content from an AEM Author to the AEM Publish instance or any other author instance. Here, we will see how we can replicate content programmatically. We can accomplish this with Sling Servlet or Workflow Process Step.
So let’s see the implementation code with Workflow Process Step
CustomerReplicationStep.java
package com.adobe.learning.core.workflow;
import com.day.cq.replication.*;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.metadata.MetaDataMap;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Session;
/**
* @author Shiv Prakash
*/
@Component(service = WorkflowProcess.class, property = {Constants.SERVICE_DESCRIPTION + "= Custom Replication Step",
Constants.SERVICE_VENDOR + "= workflow.com",
"process.label" + "= Custom Replication Step"})
public class CustomerReplicationStep implements WorkflowProcess {
private static final Logger logger = LoggerFactory.getLogger(CustomerReplicationStep.class);
@Reference
Replicator replicator;
@Reference
AgentManager agentManager;
Session session;
//Hard Coded selected Replication Agent Name
//(Name available in Settings Tab of Custom Replication Agent)
String replicationAgentName = "Custom Replication Agent";
//Hard coded payload for replication
String payloadPath = "/content/learning/us/en/samplePage";
@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
try {
//Getting Session form Workflow Session
session = workflowSession.getSession();
//Getting Replication Agent with help of Agent Manager
for (final Agent replicationAgent : agentManager.getAgents().values()) {
//Filtering the replication agent with the help of Name
if (replicationAgentName.equals(replicationAgent.getConfiguration().getName())) {
ReplicationOptions replicationOptions = new ReplicationOptions();
//Setting replication Options for replicating via Custom Replication Agent
replicationOptions.setFilter(agent -> replicationAgentName.equals(agent.getConfiguration().getName()));
replicationOptions.setSynchronous(false);
/* PayLoad Replication*/
replicator.replicate(session, ReplicationActionType.ACTIVATE, payloadPath, replicationOptions);
logger.info("Replicated via Agent : {} for Payload {}", replicationAgentName, payloadPath);
}
}
} catch (ReplicationException e) {
logger.error("Exception Occurred in Replication {}", e.getMessage());
}
}
}
When we run the workflow for a given payload It will replicate the content with the selected payload. We can see below the logger entry after successful replication:
02.08.2022 09:16:11.067 *INFO* [JobHandler: /var/workflow/instances/server0/2022-08-01/contentmigration:/content/learning/us/en/samplePage]
com.adobe.learning.core.workflow.PackageReplicationStep Replicated via Agent : Custom Replication Agent for Payload /content/learning/us/en/samplePage
Conclusion
- So in this post, we tried to create a custom replication agent and replicate a content with it. We can use the same for packages and assets as well. I hope you enjoyed this post. If you find it useful, leave us a comment. I would love to hear your thoughts and suggestions to make it better. Also, you can connect with me on LinkedIn, Instagram or Facebook.