2017-08-17 78 views
0

我在使用Jolokia与RMI服务一起使用时遇到问题。一旦RMI服务启动,Jolokia不再可以通过http访问。使用Jolokia代理与RMI服务器

我创建了一个例子类来重现问题:

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/再次访问:

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 Agent Download

回答

0

我找到了一个解决方法通过在启动代理启动RMI-服务程序后, 。

它在以下#1 - 岗位描述如何做到这一点:在运行初始化方法后Starting a Java agent after program start

所以我在下面的静态方法:

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(); 
    } 
} 

还有一个依赖于“的tools.jar”必要的:

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

我还是喜欢直接从命令行启动代理。所以更好的解决方案非常感谢。