Connecting AEM from remote AEM instance (via JCR API)

The best way to allow communication between two AEM instance is by:

  • Exposing a service from the AEM instance which acts a source of information  (say Source-AEM)
  • Consuming the exported service in the required remote AEM (Consumer-AEM).


In case, you have a use-case where:

  1. A Service cannot be exposed via source AEM
  2. A utility needs to be deployed in Consumer-AEM to access Source-AEM’s information

Then, you can use the following steps to achieve the same.

Please note: AEM does not allow remote connection OOTB. Hence, we would be embedding jcr2dav bundles. No performance testing has been conducted for the shared code.

Step-1: Create a user in Source-AEM Instance.

Create a user in source-AEM Instance with bare minimum access needed by the utility.


Step-2: Add dependencies to your maven project 

Add following dependencies to the maven project of the utility.


Since, the above bundles are not available in AEM, embed dependency for the above bundles in your pom.xml.

<!– <Embed-Dependency> artifactId1, artifactId2;inline=true </Embed-Dependency> –>

Step-3: Access source AEM instance from utlity.

Create an instance of “Jcr2davRepositoryFactory” to access the source AEM Instance.

package blog.techrevel.servlet;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(service = Servlet.class, property = { "sling.servlet.paths=/bin/testRemoteConnection",
"sling.servlet.methods=GET" })
public class RemoteConnectionServlet extends SlingSafeMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(RemoteConnectionServlet.class);
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
throws ServletException, IOException {
try {
//Connect to repository
Jcr2davRepositoryFactory repoFactory = new Jcr2davRepositoryFactory();
Map<String, String> params = new HashMap<String, String>();
params.put("org.apache.jackrabbit.repository.uri", "http://localhost:4502/crx/server&quot;);
Repository repository = repoFactory.getRepository(params);
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()), "crx.default");
//Execute Query
QueryManager qm = session.getWorkspace().getQueryManager();
String stmt = "select * from [cq:Component] where isdescendantnode('/apps')";
Query q = qm.createQuery(stmt, Query.JCR_SQL2);
NodeIterator results = q.execute().getNodes();
while (results.hasNext()) {
Node node = (Node);
resp.getWriter().write("<br />" + node.getPath());
} catch (RepositoryException e) {
LOG.error("Exception while connecting to AEM", e);

For the sample code, access the servlet via URL (http://<Processor-AEM-HOST:Processor-AEM-port>/bin/testRemoteConnection), to get list of all components below “/apps/” folder on Source-AEM Instance.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s