public void testMultipleRemoteMBeanServers() throws Exception
{
// The 1st remote server
MBeanServer remoteServer1 = newMBeanServer();
MLet mlet = new MLet(new URL[]{new URL("http", "host", 80, "/path")});
ObjectName remoteMLetName = ObjectName.getInstance(":type=mlet");
remoteServer1.registerMBean(mlet, remoteMLetName);
JMXServiceURL address1 = new JMXServiceURL("rmi", "localhost", 0);
JMXConnectorServer connectorServer1 = JMXConnectorServerFactory.newJMXConnectorServer(address1, null, null);
ObjectName connectorServerName1 = ObjectName.getInstance(":type=connector,protocol=" + address1.getProtocol());
remoteServer1.registerMBean(connectorServer1, connectorServerName1);
connectorServer1.start();
address1 = connectorServer1.getAddress();
// The 2nd remote server
MBeanServer remoteServer2 = newMBeanServer();
Timer timer = new Timer();
ObjectName remoteTimerName = ObjectName.getInstance(":type=timer");
remoteServer2.registerMBean(timer, remoteTimerName);
timer.start();
JMXServiceURL address2 = new JMXServiceURL("rmi", "localhost", 0);
JMXConnectorServer connectorServer2 = JMXConnectorServerFactory.newJMXConnectorServer(address2, null, remoteServer2);
connectorServer2.start();
address2 = connectorServer2.getAddress();
// The local server
MBeanServer localServer = newMBeanServer();
RemoteMBeanProxy proxy1 = new RemoteMBeanProxy(remoteMLetName, address1, null, null);
JMXConnector cntor = JMXConnectorFactory.connect(address2);
RemoteMBeanProxy proxy2 = new RemoteMBeanProxy(remoteTimerName, cntor.getMBeanServerConnection());
ObjectName proxyName1 = ObjectName.getInstance(":proxy=" + ObjectName.quote(remoteMLetName.getCanonicalName()));
ObjectName proxyName2 = ObjectName.getInstance(":proxy=" + ObjectName.quote(remoteTimerName.getCanonicalName()));
localServer.registerMBean(proxy1, proxyName1);
localServer.registerMBean(proxy2, proxyName2);
JMXServiceURL address3 = new JMXServiceURL("local", "localhost", 0);
JMXConnectorServer connectorServer3 = JMXConnectorServerFactory.newJMXConnectorServer(address3, null, localServer);
connectorServer3.start();
address3 = connectorServer3.getAddress();
// The client
JMXConnector connector = JMXConnectorFactory.connect(address3);
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
// Tests
URL[] urls = (URL[])mbsc.getAttribute(proxyName1, "URLs");
if (!Utils.arrayEquals(urls, mlet.getURLs())) fail();
if (!timer.isActive()) fail();
mbsc.invoke(proxyName2, "stop", null, null);
if (timer.isActive()) fail();
}