KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > pool > jmx > RemoteClient


1 /**
2 * Library name : Primrose - A Java Database Connection Pool.
3 * Published by Ben Keeping, http://primrose.org.uk .
4 * Copyright (C) 2004 Ben Keeping, primrose.org.uk
5 * Email: Use "Contact Us Form" on website
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */

21
22
23 package uk.org.primrose.pool.jmx;
24
25 import javax.management.Attribute JavaDoc;
26 import javax.management.MBeanServerConnection JavaDoc;
27 import javax.management.MBeanServerInvocationHandler JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.ObjectInstance JavaDoc;
30 import javax.management.remote.JMXConnector JavaDoc;
31 import javax.management.remote.JMXConnectorFactory JavaDoc;
32 import javax.management.remote.JMXServiceURL JavaDoc;
33
34 public class RemoteClient {
35
36     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
37         //if (args.length != 3) System.err.println("Usuage : java uk.org.primrose.reportdownload.RemoteClientUtil <action - stop|start> <RMI server name> <OS host name>");
38

39         String JavaDoc serverName = args[0];
40         String JavaDoc mbeanName = "PoolController Domain:name=PoolController";
41         RemoteClient rc = new RemoteClient();
42         rc.bindJMXConnector(args[0]);
43         rc.doIt(mbeanName, serverName);
44         rc.close();
45     }
46
47
48     private MBeanServerConnection JavaDoc mbsc = null;
49     private JMXConnector JavaDoc jmxc = null;
50
51     public RemoteClient() {
52     }
53
54     public MBeanServerConnection JavaDoc bindJMXConnector(String JavaDoc host) throws Exception JavaDoc {
55         //System.err.println("bindJMX");
56
// Create a JMXMP connector client and
57
// connect it to the JMXMP connector server
58
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("jmxmp", host, 8091);
59
60         jmxc = JMXConnectorFactory.connect(url, null);
61         // Get an MBeanServerConnection
62
mbsc = jmxc.getMBeanServerConnection();
63         // Get domains from MBeanServer
64
// This seems to break it ... not sure why ...
65
//System.out.println("\nDomains:");
66
//String domains[] = mbsc.getDomains();
67
//for (int i = 0; i < domains.length; i++) {
68
// System.out.println("\tDomain[" + i + "] = " + domains[i]);
69
//}
70

71         return mbsc;
72
73     }
74
75     public void close() throws java.io.IOException JavaDoc {
76         jmxc.close();
77     }
78
79     public void doIt(String JavaDoc mbeanName, String JavaDoc host) {
80         try {
81             if (mbsc == null) {
82                 mbsc = this.bindJMXConnector(host);
83             }
84             ObjectName JavaDoc on = new ObjectName JavaDoc(mbeanName);
85             Object JavaDoc[] params = new Object JavaDoc[] {
86                 new String JavaDoc("show tables")
87             };
88             String JavaDoc[] signature = new String JavaDoc[] {
89                 new String JavaDoc().getClass().getName()
90             };
91
92             System.err.println(mbsc.invoke(on, "invokeRemoteDatabaseProc", params, signature));
93             //mbsc.invoke(on, "reset", null, null);
94
} catch (Exception JavaDoc e) {
95             e.printStackTrace();
96         }
97     }
98
99     public void refreshPoolConfigFile(String JavaDoc mbeanName, String JavaDoc host, String JavaDoc poolName) {
100         try {
101             if (mbsc == null) {
102                 mbsc = this.bindJMXConnector(host);
103             }
104             ObjectName JavaDoc on = new ObjectName JavaDoc(mbeanName);
105
106             //
107
mbsc.invoke(on, "refreshPoolConfigFile", null, null);
108             //mbsc.invoke(on, "reset", null, null);
109
} catch (Exception JavaDoc e) {
110             e.printStackTrace();
111         }
112     }
113
114     public Queue getQueue(String JavaDoc mbeanName, String JavaDoc host, String JavaDoc poolName) {
115         try {
116             if (mbsc == null) {
117                 mbsc = this.bindJMXConnector(host);
118             }
119             ObjectName JavaDoc on = new ObjectName JavaDoc(mbeanName);
120             Object JavaDoc[] params = new Object JavaDoc[] {
121                 poolName
122             };
123             String JavaDoc[] signature = new String JavaDoc[] {
124                 new String JavaDoc().getClass().getName()
125             };
126
127             return (Queue)mbsc.invoke(on, "getQueue", params, signature);
128             //mbsc.invoke(on, "reset", null, null);
129
} catch (Exception JavaDoc e) {
130             e.printStackTrace(System.err);
131             return null;
132         }
133     }
134
135 }
136
Popular Tags