我在将 Jolokia 与 RMI 服务结合使用时遇到问题。一旦启动 RMI 服务,Jolokia 就不再可以通过 http 访问。
我创建了一个示例类来重现该问题:
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();
}
}
}
如果我将主要方法更改为不启动 RMI 服务,则可以再次通过 http URL http://127.0.0.1:8778/jolokia/访问 Jolokia :
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();
}
}
}
该服务是一个可运行的 jar。这是我用来启动应用程序的命令:
java -javaagent:jolokia-jvm-1.3.7-agent.jar=port=8778,host=localhost -jar RMITest-0.0.1-SNAPSHOT.jar
我从官网下载了jolokia代理:jolokia代理下载