The following example queries the local VMM agent for all HostMXBeans. For each host, it lists all virtual machines residing on this host. Then it attempts to create a new virtual machine asynchronously and receives a notification upon successful creation.
try {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
mbsc = jmxc.getMBeanServerConnection();
Set names = mbsc.queryNames(new ObjectName("org.ow2.jasmine.vmm.api:type=Host,*"), null);
for (ObjectName hostObjectName : names) {
HostMXBean host = (HostMXBean) JMX.newMXBeanProxy(mbsc, hostObjectName, HostMXBean.class);
System.out.println("Host hostname="+host.getHostName());
for (VirtualMachineMXBean vm : host.getResidentVMs())
System.out.println("\tVM name="+ vm.getNameLabel());
NotificationListener listener = new NotificationListener() {
public void handleNotification(javax.management.Notification notification, Object handback) {
if (notification.getType().equals(NotificationTypes.VM_ADD)) {
ObjectName vmObjectName = (ObjectName) notification.getUserData();
VirtualMachineMXBean vm = (VirtualMachineMXBean) JMX.newMXBeanProxy(mbsc, vmObjectName, VirtualMachineMXBean.class);
try {
System.out.println("New VM: "+vm.getNameLabel());
} catch (InstanceNotFoundException ex) {
}
}
}
};
mbsc.addNotificationListener(hostObjectName, listener, null, null);
VMConfigSpec vmSpec = new VMConfigSpec("ServerInstance01", 128, 1, 512, "Debian");
System.out.println("CREATING VM...");
host.createVM(vmSpec, false);
Thread.sleep(Integer.MAX_VALUE);
}
jmxc.close();
} catch (Exception e) {
e.printStackTrace();
}
|