2017-08-17 4 views
0

J'ai des problèmes avec l'utilisation de Jolokia conjointement avec un service RMI. Dès que le service RMI est démarré, Jolokia n'est plus accessible via http.Utilisation de l'agent Jolokia avec un serveur RMI

J'ai créé une classe d'exemple pour reproduire le problème:

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

Si je change la méthode principale pour ne pas démarrer le RMI-Service, Jolokia est accessible via l'URL http http://127.0.0.1:8778/jolokia/ nouveau:

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

Le service est un pot exécutable. Voici la commande que je utilise pour démarrer l'application:

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

J'ai téléchargé l'agent Jolokia sur le site officiel: Jolokia Agent Download

Répondre

0

J'ai trouvé une solution de contournement en démarrant l'agent après le démarrage du programme RMI-Service .

Il est décrit dans le Stackoverflow-Post comment faire le suivant: Starting a Java agent after program start

Alors je commence la méthode statique suivante après l'exécution de la méthode init:

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

Il y a aussi un dépendance à "tools.jar" nécessaire:

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

Je préférerais toujours démarrer l'agent directement à partir de la ligne de commande. Donc, une meilleure solution est très appréciée.