0

I'm having problems with using Jolokia in conjunction with an RMI-Service. As soon as the RMI-Service is started Jolokia is no longer accessible via http.

I created an example class to reproduce the problem:

package com.example.rmi;

import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

public class RMITest {

    private class RMIService extends UnicastRemoteObject {

        private static final long serialVersionUID = 1L;

        protected RMIService() throws RemoteException {
            super();
        }

        public void doStuff() {
            System.out.println("Processing...");

        }

    }

    public static void main(String[] args) throws RemoteException {
        RMITest rmiServer = new RMITest();
        rmiServer.init();
    }

    private void init() throws RemoteException {
        RMIService rmiService = new RMIService();

        try {
            System.out.println("Starting RMI-Service...");
            String hostname = InetAddress.getLocalHost().getHostName();
            System.setProperty("java.rmi.server.hostname", hostname);

            int port = 2005;
            LocateRegistry.createRegistry(port);
            Naming.rebind("rmi://" + hostname + ":" + port
                    + "/RMIService", rmiService);
            System.out.println("RMI-Service started!");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

If I change the main method to not start the RMI-Service, Jolokia is accessible via the http URL http://127.0.0.1:8778/jolokia/ again:

public static void main(String[] args) throws RemoteException {
    RMITest rmiServer = new RMITest();
    //rmiServer.init();

    while(true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
   }

The service is a runnable jar. Here is the command I'm using to start the application:

java -javaagent:jolokia-jvm-1.3.7-agent.jar=port=8778,host=localhost -jar RMITest-0.0.1-SNAPSHOT.jar

I downloaded the jolokia agent from the official website: Jolokia Agent Download

JanTheGun
  • 2,165
  • 17
  • 25

1 Answers1

0

I found a workaround by starting the agent programmatically after starting the RMI-Service.

It is described in the following Stackoverflow-Post how to do it: Starting a Java agent after program start

So I'm starting the following static method after running the init-method:

public static void attachGivenAgentToThisVM(String pathToAgentJar) {
    try {
        String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
        String pid = nameOfRunningVM.substring(0, nameOfRunningVM.indexOf('@'));
        VirtualMachine vm = VirtualMachine.attach(pid);
        vm.loadAgent(pathToAgentJar, "");
        vm.detach();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

There is also a dependency to "tools.jar" necessary:

<dependency>
    <groupId>com.sun</groupId>
    <artifactId>tools</artifactId>
    <version>1.8</version>
    <scope>system</scope>
    <systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>

I would still prefer to start the agent directly from command line. So a better solution is much appreciated.

JanTheGun
  • 2,165
  • 17
  • 25