/*
* RemoteServerTest
* $Id: RemoteServerTest.java,v 1.5 2003/01/09 07:12:40 telrod Exp $
*/
package samples.scenario1;
import org.jboss.mx.remote.connector.socket.SocketConnector;
import org.jboss.mx.remote.discovery.multicast.MulticastDetector;
import org.jboss.mx.remote.server.CascadingAgent;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.ObjectInstance;
import java.util.Set;
/**
* @version 2.5
* @since Sep 27, 2002
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
*/
public class RemoteServerTest
{
public static void runClassLoadingTest(MBeanServer server)
{
try
{
long timeout = 10000;
long time = System.currentTimeMillis();
CascadingAgent agent=new CascadingAgent();
server.registerMBean(agent,new ObjectName("jmx.remoting:type=CascadingAgent"));
agent.start();
while ( (System.currentTimeMillis()-time)<timeout )
{
Set beans=server.queryMBeans(new ObjectName("test:type=LocalTest,*"),null);
System.out.println("Found "+beans.size()+" beans..");
if (beans.isEmpty())
{
Thread.currentThread().sleep(1000);
// wait for it to show up
}
else
{
ObjectInstance oi=(ObjectInstance)beans.iterator().next();
Object obj=server.invoke(oi.getObjectName(),"test",new Object[]{new RemoteTestClass()},new String[]{Object.class.getName()});
System.out.println("SUCCESS!! Received from remote mbeanserver: "+obj);
return;
}
}
System.err.println("Error finding MBean LocalTest - 'runClassLoadingTest' test failed");
System.exit(1);
}
catch (Exception ex)
{
ex.printStackTrace();
System.exit(1);
}
}
/**
* This is the sample for scenario 1 where there is a
* remote MBeanServer, Detector, and Connector that will
* be discovered by the LocaServerTest.
*/
public static void main(String args[])
{
try
{
org.apache.log4j.BasicConfigurator.configure();
org.apache.log4j.Category.getRoot().setLevel(org.apache.log4j.Level.INFO);
MBeanServer server = MBeanServerFactory.createMBeanServer();
SocketConnector connector = new SocketConnector();
server.registerMBean(connector, new ObjectName("jmx.remoting:type=Connector,transport=socket"));
connector.start();
MulticastDetector detector = new MulticastDetector();
server.registerMBean(detector, new ObjectName("jmx.remoting:type=Detector,transport=multicast"));
detector.start();
ObjectInstance html = server.createMBean("com.sun.jdmk.comm.HtmlAdaptorServer",
null);
server.invoke(html.getObjectName(), "setPort",
new Object[]{new Integer("8084")}, new String[]{"java.lang.Integer"});
server.invoke(html.getObjectName(), "start",
new Object[0], new String[0]);
runClassLoadingTest(server);
System.exit(0);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}