KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > management > JmxRMIClientTester


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is lital kasif.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45 *
46 * Created on Oct 14, 2004
47 *
48 */

49 package sample.management;
50
51 /*===================================================================================
52   For instructions on how to run this sample please refer to the file
53   samples\src\sample\management\Readme.txt under the MantaRay installation directory.
54 =====================================================================================*/

55
56 /*
57  * @author lital kasif
58  */

59 import java.io.IOException JavaDoc;
60 import java.net.MalformedURLException JavaDoc;
61 import java.util.Set JavaDoc;
62
63 import javax.management.MBeanServerConnection JavaDoc;
64 import javax.management.MBeanServerInvocationHandler JavaDoc;
65 import javax.management.MalformedObjectNameException JavaDoc;
66 import javax.management.ObjectName JavaDoc;
67 import javax.management.remote.JMXConnector JavaDoc;
68 import javax.management.remote.JMXConnectorFactory JavaDoc;
69 import javax.management.remote.JMXServiceURL JavaDoc;
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 JavaDoc[] argv) throws MantaException
78     {
79         // The host, port and path where the rmiregistry runs.
80
String JavaDoc namingHost = "localhost";
81         int namingPort =1099;
82
83        // The RMI server's host: this is actually ignored by JSR 160
84
// since this information is stored in the RMI stub.
85
String JavaDoc serverHost = "host";
86        String JavaDoc jndiPath = "/jmxconnector";
87
88         // Is there anything to do?
89
if (argv.length == 0) {
90             printHelp();
91         }
92         else{
93            // Check parameters
94
for (int i = 0; i < argv.length; i++) {
95                String JavaDoc arg = argv[i];
96
97                // Options
98
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                    }//if
113

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                        }//if
119
namingPort = Integer.parseInt(argv[++i]);
120                        continue;
121                    }//if
122
}//else
123
}//for
124
}//else
125

126
127        // The address of the connector server
128
JMXServiceURL JavaDoc url;
129        try {
130            url = new JMXServiceURL JavaDoc("service:jmx:rmi" + "://" + serverHost + "/jndi/rmi://" + namingHost + ":" + namingPort + jndiPath);
131
132            // Connect a JSR 160 JMXConnector to the server side
133
JMXConnector JavaDoc connector = JMXConnectorFactory.connect(url);
134
135            // Retrieve an MBeanServerConnection that represent the MBeanServer the remote
136
// connector server is bound to
137
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
138
139            //Call the server side as if it is a local MBeanServer
140
ObjectName JavaDoc timeName = ObjectName.getInstance("MantaRay:time=gets the time");
141            GetCurrentTimeJMXMBean timer =(GetCurrentTimeJMXMBean) MBeanServerInvocationHandler.newProxyInstance(connection, timeName, GetCurrentTimeJMXMBean.class, true);
142            //The magic of JDK 1.3 dynamic proxy and JSR 160:
143
// delegate.getImplementationVendor() is actually a remote JMX call,
144
// but it looks like a local, old-style, java call.
145
System.out.println(timer.getTime());
146
147            ObjectName JavaDoc getParticipation;
148            Set JavaDoc[] result = new Set JavaDoc[2];
149
150            getParticipation = ObjectName.getInstance("MantaRay:services=services participation");
151            GetServiceParticipationJMXMBean participation =(GetServiceParticipationJMXMBean) MBeanServerInvocationHandler.newProxyInstance
152            (connection, getParticipation, GetServiceParticipationJMXMBean.class, true);
153            Set JavaDoc produced=participation.getProduced();
154            Set JavaDoc consumed=participation.getConsumed();
155           // Set nonDurablyConsumed=participation.getNonDurablyConsumed();
156
result[0] = produced;
157             result[1] = consumed;
158   // result[2] = durablyConsumed;
159

160        } catch (MalformedURLException JavaDoc e) {
161            System.out.println("IP or host are not correct.");
162            e.printStackTrace();
163        } catch (IOException JavaDoc e) {
164            e.printStackTrace();
165        } catch (MalformedObjectNameException JavaDoc e) {
166            e.printStackTrace();
167        }
168     }//main
169

170     /** Prints the usage. */
171     private static void printHelp() {
172         StringBuffer JavaDoc use = new StringBuffer JavaDoc();
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 JavaDoc e) {
186             e.printStackTrace();
187         }
188     }
189 }//JmxRMIClientTester
190
Popular Tags