>

29 junio 2014

Conectándote a la Máquina Virtual de Java (JVM)

La entrada de hoy va de como te conectas a la máquina virtual de java mediante programación, y te preguntarás ¿para qué me voy a conectar a la maquina virtual?, pues muy fácil, podrás controlar la memoria, threads y leer las propiedades del sistema.


package es.jnovo;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import com.sun.tools.attach.spi.AttachProvider;

public class Jvm {

 public static void main(String[] args) {
  systema();
 }

 public static String systema() {
  StringBuilder salida = new StringBuilder();
  try {
   final AttachProvider attachProvider = AttachProvider.providers().get(0);

   VirtualMachineDescriptor descriptor = null;
   for (VirtualMachineDescriptor virtualMachineDescriptor : attachProvider.listVirtualMachines()) {

    // System.out.println(virtualMachineDescriptor.displayName());

    if (virtualMachineDescriptor.displayName().contains("eclipse")) {
     descriptor = virtualMachineDescriptor;
    }
   }

   if (descriptor == null)
    throw new RuntimeException("No tienes descriptor de la maquina virtual");

   VirtualMachine virtualMachine;

   virtualMachine = attachProvider.attachVirtualMachine(descriptor);

   final String home = virtualMachine.getSystemProperties().getProperty("java.home");
   final String agent = home + File.separator + "lib" + File.separator + "management-agent.jar";

   virtualMachine.loadAgent(agent);

   final Object portObject = virtualMachine.getAgentProperties().get("com.sun.management.jmxremote.localConnectorAddress");

   final JMXServiceURL target = new JMXServiceURL(portObject + "");
   final JMXConnector connector = JMXConnectorFactory.connect(target);
   final MBeanServerConnection remote = connector.getMBeanServerConnection();

   // listamos los threads del sistema
   ObjectName objName = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
   Set mbeans = remote.queryNames(objName, null);
   for (ObjectName name : mbeans) {
    ThreadMXBean threadBean = ManagementFactory.newPlatformMXBeanProxy(remote, name.toString(), ThreadMXBean.class);
    long threadIds[] = threadBean.getAllThreadIds();
    for (long threadId : threadIds) {
     ThreadInfo threadInfo = threadBean.getThreadInfo(threadId);
     System.out.println(threadInfo.getThreadName() + " / " + threadInfo.getThreadState());
    }
   }
  } catch (AttachNotSupportedException e) {
  } catch (IOException e) {

  } catch (AgentLoadException e) {
  } catch (AgentInitializationException e) {
  } catch (MalformedObjectNameException e) {
  }
  return salida.toString();
 }
}