RMI Connection to AEM 6.0 via JCR API | Community
Skip to main content
Premkarthic-7WP
Level 4
October 16, 2015
Solved

RMI Connection to AEM 6.0 via JCR API

  • October 16, 2015
  • 11 replies
  • 4173 views

Hi,

AEM version: 6.0

Tried to make an rmi connection, from java application.

We followed the steps, as exactly as mentioned on the kb (https://helpx.adobe.com/experience-manager/kb/rmi-connection-crx-cq-561.html) and always we are getting the connection refused exception.

Apart from enabling RMI Connection listener, is we have to do anything at server level for enabling RMI?

In the code, we are referring to a parameter crx-application-name,  where we have to configure the same?

Kindly advice us on this.

error.log (After enabling rmi listener):

 *INFO* [CM Event Dispatcher (Fire ConfigurationEvent: pid=org.apache.sling.jcr.jackrabbit.server.RmiRegistrationSupport)] org.apache.sling.jcr.registration Using RMI Registry port 1199
 *INFO* [JcrInstaller.1] org.apache.sling.installer.provider.jcr.impl.JcrInstaller Registering resource with OSGi installer: [InstallableResource, priority=200, id=/apps/system/config/org.apache.sling.jcr.jackrabbit.server.RmiRegistrationSupport.config]

Code Snippet:

public static void main (String[] args){

        System.out.println("RMI to CRX Test Connection:");

        ClientRepositoryFactory factory = new ClientRepositoryFactory();

        Repository repository;

        String crxApplicationName = "virtual-crx";

        String repoUrl = "//localhost:1199/"+crxApplicationName;

        String workspace = "crx.default";

        String username = "admin";

        char [] password = "admin".toCharArray();

        try {

               repository = factory.getRepository(repoUrl);

               Session s = repository.login(new SimpleCredentials(username,password), workspace);

               System.out.println("Workspace: " + s.getWorkspace().getName());

               System.out.println("userId: "+s.getUserID());

        } catch (Exception e) {

               e.printStackTrace();

        }

 }

Exception:

RMI to CRX Test Connection:
Exception in thread "main" org.apache.jackrabbit.rmi.client.RemoteRuntimeException: java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at org.apache.jackrabbit.rmi.client.SafeClientRepository.getRemoteRepository(SafeClientRepository.java:85)
    at org.apache.jackrabbit.rmi.client.SafeClientRepository.<init>(SafeClientRepository.java:56)
    at org.apache.jackrabbit.rmi.client.ClientRepositoryFactory$1.<init>(ClientRepositoryFactory.java:90)
    at org.apache.jackrabbit.rmi.client.ClientRepositoryFactory.getRepository(ClientRepositoryFactory.java:90)
    at com.wbg.connectrepo.connectRepo.main(connectRepo.java:39)
Caused by: java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
    at sun.rmi.server.UnicastRef.newCall(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at java.rmi.Naming.lookup(Unknown Source)
    at org.apache.jackrabbit.rmi.client.ClientRepositoryFactory$1.getRemoteRepository(ClientRepositoryFactory.java:95)
    at org.apache.jackrabbit.rmi.client.SafeClientRepository.getRemoteRepository(SafeClientRepository.java:83)
    ... 4 more

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

We also tried to make an rmi connection as mentioned on (http://dev.day.com/docs/en/crx/current/developing/accessing_the_crx.html#Accessing a Repository via RMI), in that case also we are getting the same exception

Code Snippet:

public static void main(String[] args) throws Exception{
        
        
            System.out.println("Test Rmi Connection.");
           
            Repository repository = JcrUtils.getRepository("rmi://localhost:1199/crx");

            SimpleCredentials creds = new SimpleCredentials("admin", "admin".toCharArray());
            Session session = null;
            
            
            session = repository.login(creds, "crx.default");
    }

Exception:

Exception in thread "main" javax.jcr.RepositoryException: Unable to access a repository with the following settings:
    org.apache.jackrabbit.repository.uri: rmi://localhost:1199/crx
The following RepositoryFactory classes were consulted:
    org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory: declined
    org.apache.jackrabbit.jcr2spi.Jcr2spiRepositoryFactory: declined
    org.apache.jackrabbit.commons.JndiRepositoryFactory: declined
    org.apache.jackrabbit.rmi.repository.RmiRepositoryFactory: failed
        because of RemoteRuntimeException: java.rmi.RemoteException: Failed to look up the RMI resource //localhost:1199/crx; nested exception is: 
    java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect
        because of RemoteException: Failed to look up the RMI resource //localhost:1199/crx; nested exception is: 
    java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect
        because of ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect
        because of ConnectException: Connection refused: connect
Perhaps the repository you are trying to access is not available at the moment.
    at org.apache.jackrabbit.commons.JcrUtils.getRepository(JcrUtils.java:223)
    at org.apache.jackrabbit.commons.JcrUtils.getRepository(JcrUtils.java:263)
    at com.wbg.connectrepo.connectRepo.main(connectRepo.java:21)

Thank,

Karthi

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ogill
 
 

Thanks for the input Opkar!

Use Case: Connect crx remotely to execute query.

 

Is there any reason why it must be RMI? Could you not create a REST service for this request?

Regards,

Opkar

11 replies

smacdonald2008
Level 10
October 16, 2015

If RMI does not work - as suggested - you can log a bug. 

Another way to communicate from an external Java app (without using JCR or RMI) that would work is to write a series of custom Sling Servlets on AEM. The Sling Servlet can perform CRUD operations on the JCR. FRom within the Servlet - you can use the following code (which shows a more secure way to get data) that uses the getServiceResourceResolver API call.

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
ResourceResolver resolver = null;

try {
           
    //Invoke the adaptTo method to create a Session used to create a QueryManager
 resolver = resolverFactory.getServiceResourceResolver(param);
    session = resolver.adaptTo(Session.class);

You can use HTTP libs in the Java client to perform GET and POST operations. 

For example, the client can perform a GET, the servlet would get a result set and pass back the JCR data in JSON and the client would read the JSON and display. 

See this article as an example of a Java app using HTTP operations to interact with CQ (this posts files to a Sling Servlet): 

http://scottsdigitalcommunity.blogspot.ca/2014/03/creating-java-swing-applications-that.html