2014-12-16 12 views
6

Ho bisogno di avviare a livello di codice un nuovo processo java e impostare dinamicamente la porta JMX. Così, invece di fare questoÈ possibile abilitare il monitoraggio remoto di jmx a livello di programmazione?

-Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.port=9995 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false 

vorrei fare quanto segue

System.setProperty("java.rmi.server.hostname", "127.0.0.1"); 
System.setProperty("com.sun.management.jmxremote", "true"); 
System.setProperty("com.sun.management.jmxremote.authenticate", "false"); 
System.setProperty("com.sun.management.jmxremote.ssl", "false"); 
System.setProperty("com.sun.management.jmxremote.port", "9995" ); 

ma non funziona. Qualche idea del perché?

+0

IMHO non è possibile. – SMA

+1

Vedere questo [risposta] (http://stackoverflow.com/questions/7276881/how-to-set-jmx-remote-port-system-environment-parameters-through-java-code-for-r). Puoi ancora monitorare da remoto la JVM usando [Java Attach API] (http://docs.oracle.com/javase/7/docs/technotes/guides/attach/index.html) se questo è il tuo obiettivo. – vsnyc

risposta

9

Quando viene chiamato il codice, hai perso la possibilità di configurare il connettore jmxremote.

Quello che dovete fare è creare il vostro registro rmi e un JMXConnectorServer per ascoltare le chiamate rmi e passarle su MBeanServer.

private void createJmxConnectorServer() throws IOException { 
    LocateRegistry.createRegistry(1234); 
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://localhost/jndi/rmi://localhost:1234/jmxrmi"); 
    JMXConnectorServer svr = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); 
    svr.start(); 
} 
+0

Grazie per questo snippet. Funziona molto bene per me. –

+0

Bello sapere che ho aiutato :) – AutomatedMike

Problemi correlati