1 49 package sample.management; 50 51 55 56 59 import java.io.IOException ; 60 import java.net.MalformedURLException ; 61 import java.util.Set ; 62 63 import javax.management.MBeanServerConnection ; 64 import javax.management.MBeanServerInvocationHandler ; 65 import javax.management.MalformedObjectNameException ; 66 import javax.management.ObjectName ; 67 import javax.management.remote.JMXConnector ; 68 import javax.management.remote.JMXConnectorFactory ; 69 import javax.management.remote.JMXServiceURL ; 70 71 import org.mr.MantaException; 72 import org.mr.core.cmc.GetCurrentTimeJMXMBean; 73 import org.mr.kernel.world.cmc.GetServiceParticipationJMXMBean; 74 75 76 public class JmxRMIClientTester { 77 public static void main(String [] argv) throws MantaException 78 { 79 String namingHost = "localhost"; 81 int namingPort =1099; 82 83 String serverHost = "host"; 86 String jndiPath = "/jmxconnector"; 87 88 if (argv.length == 0) { 90 printHelp(); 91 } 92 else{ 93 for (int i = 0; i < argv.length; i++) { 95 String arg = argv[i]; 96 97 if (!arg.startsWith("-")) { 99 System.err.println ("error: unexpected argument - "+arg); 100 printHelp(); 101 waitForAnyKey(); 102 System.exit(1); 103 } 104 else { 105 if (arg.equals("-h")) { 106 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 107 System.err.println("error: missing user name"); 108 System.exit(1); 109 } 110 namingHost = argv[++i]; 111 continue; 112 } 114 if (arg.equals("-p")) { 115 if (i == argv.length - 1 || argv[i+1].startsWith("-")) { 116 System.err.println("error: missing receive queue parameter"); 117 System.exit(1); 118 } namingPort = Integer.parseInt(argv[++i]); 120 continue; 121 } } } } 126 127 JMXServiceURL url; 129 try { 130 url = new JMXServiceURL ("service:jmx:rmi" + "://" + serverHost + "/jndi/rmi://" + namingHost + ":" + namingPort + jndiPath); 131 132 JMXConnector connector = JMXConnectorFactory.connect(url); 134 135 MBeanServerConnection connection = connector.getMBeanServerConnection(); 138 139 ObjectName timeName = ObjectName.getInstance("MantaRay:time=gets the time"); 141 GetCurrentTimeJMXMBean timer =(GetCurrentTimeJMXMBean) MBeanServerInvocationHandler.newProxyInstance(connection, timeName, GetCurrentTimeJMXMBean.class, true); 142 System.out.println(timer.getTime()); 146 147 ObjectName getParticipation; 148 Set [] result = new Set [2]; 149 150 getParticipation = ObjectName.getInstance("MantaRay:services=services participation"); 151 GetServiceParticipationJMXMBean participation =(GetServiceParticipationJMXMBean) MBeanServerInvocationHandler.newProxyInstance 152 (connection, getParticipation, GetServiceParticipationJMXMBean.class, true); 153 Set produced=participation.getProduced(); 154 Set consumed=participation.getConsumed(); 155 result[0] = produced; 157 result[1] = consumed; 158 160 } catch (MalformedURLException e) { 161 System.out.println("IP or host are not correct."); 162 e.printStackTrace(); 163 } catch (IOException e) { 164 e.printStackTrace(); 165 } catch (MalformedObjectNameException e) { 166 e.printStackTrace(); 167 } 168 } 170 171 private static void printHelp() { 172 StringBuffer use = new StringBuffer (); 173 use.append("help: java JmxRMIClientTester (options) ...\n\n"); 174 use.append("options:\n"); 175 use.append(" -p port# Specify the port number of the RMI connection to the management system of managed MantaRay.\n"); 176 use.append(" -h host Specify the IP of the host, which is the IP of the managed MantRay.\n"); 177 use.append("since no host or port were entered the defaults are 'localhost' and port 1099."); 178 System.err.println (use); 179 } 180 181 private static void waitForAnyKey(){ 182 System.out.print("Press any key ..."); 183 try { 184 System.in.read(); 185 } catch (IOException e) { 186 e.printStackTrace(); 187 } 188 } 189 } | Popular Tags |