Repository navigation
Expand file tree
/
Copy pathClient.java
More file actions
218 lines (182 loc) · 7.19 KB
/
Copy pathClient.java
File metadata and controls
218 lines (182 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Client.java - JMX client that interacts with the JMX agent. It gets
* attributes and performs operations on the Hello MBean and the QueueSampler
* MXBean example. It also listens for Hello MBean notifications.
*/
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import javax.management.AttributeChangeNotification;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationEmitter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class Client {
private static final String PACKAGE = "";
/**
* Inner class that will handle the notifications.
*/
public static class ClientListener implements NotificationListener {
public void handleNotification(Notification notification,
Object handback) {
echo("\nReceived notification:");
echo("\tClassName: " + notification.getClass().getName());
echo("\tSource: " + notification.getSource());
echo("\tType: " + notification.getType());
echo("\tMessage: " + notification.getMessage());
if (notification instanceof AttributeChangeNotification) {
AttributeChangeNotification acn =
(AttributeChangeNotification) notification;
echo("\tAttributeName: " + acn.getAttributeName());
echo("\tAttributeType: " + acn.getAttributeType());
echo("\tNewValue: " + acn.getNewValue());
echo("\tOldValue: " + acn.getOldValue());
}
}
}
/* For simplicity, we declare "throws Exception".
Real programs will usually want finer-grained exception handling. */
public static void main(String[] args) throws Exception {
// Create an RMI connector client and
// connect it to the RMI connector server
//
echo("\nCreate an RMI connector client and " +
"connect it to the RMI connector server");
JMXServiceURL url =
new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
// Create listener
//
ClientListener listener = new ClientListener();
// Get an MBeanServerConnection
//
echo("\nGet an MBeanServerConnection");
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
waitForEnterPressed();
// Get domains from MBeanServer
//
echo("\nDomains:");
String domains[] = mbsc.getDomains();
Arrays.sort(domains);
for (String domain : domains) {
echo("\tDomain = " + domain);
}
waitForEnterPressed();
// Get MBeanServer's default domain
//
echo("\nMBeanServer default domain = " + mbsc.getDefaultDomain());
// Get MBean count
//
echo("\nMBean count = " + mbsc.getMBeanCount());
// Query MBean names
//
echo("\nQuery MBeanServer MBeans:");
Set<ObjectName> names =
new TreeSet<ObjectName>(mbsc.queryNames(null, null));
for (ObjectName name : names) {
echo("\tObjectName = " + name);
}
waitForEnterPressed();
// ----------------------
// Manage the Hello MBean
// ----------------------
echo("\n>>> Perform operations on Hello MBean <<<");
// Construct the ObjectName for the Hello MBean
//
ObjectName mbeanName = new ObjectName(PACKAGE + ":type=Hello");
// Create a dedicated proxy for the MBean instead of
// going directly through the MBean server connection
//
HelloMBean mbeanProxy =
JMX.newMBeanProxy(mbsc, mbeanName, HelloMBean.class, true);
// Add notification listener on Hello MBean
//
echo("\nAdd notification listener...");
mbsc.addNotificationListener(mbeanName, listener, null, null);
// Get CacheSize attribute in Hello MBean
//
echo("\nCacheSize = " + mbeanProxy.getCacheSize());
// Set CacheSize attribute in Hello MBean
// Calling "reset" makes the Hello MBean emit a
// notification that will be received by the registered
// ClientListener.
//
mbeanProxy.setCacheSize(150);
// Sleep for 2 seconds to have time to receive the notification
//
echo("\nWaiting for notification...");
sleep(2000);
// Get CacheSize attribute in Hello MBean
//
echo("\nCacheSize = " + mbeanProxy.getCacheSize());
// Invoke "sayHello" in Hello MBean
//
echo("\nInvoke sayHello() in Hello MBean...");
mbeanProxy.sayHello();
// Invoke "add" in Hello MBean
//
echo("\nInvoke add(2, 3) in Hello MBean...");
echo("\nadd(2, 3) = " + mbeanProxy.add(2, 3));
waitForEnterPressed();
// ------------------------------
// Manage the QueueSampler MXBean
// ------------------------------
echo("\n>>> Perform operations on QueueSampler MXBean <<<");
// Construct the ObjectName for the QueueSampler MXBean
//
ObjectName mxbeanName =
new ObjectName(PACKAGE + ":type=QueueSampler");
// Create a dedicated proxy for the MXBean instead of
// going directly through the MBean server connection
//
QueueSamplerMXBean mxbeanProxy =
JMX.newMXBeanProxy(mbsc, mxbeanName, QueueSamplerMXBean.class);
// Get QueueSample attribute in QueueSampler MXBean
//
QueueSample queue1 = mxbeanProxy.getQueueSample();
echo("\nQueueSample.Date = " + queue1.getDate());
echo("QueueSample.Head = " + queue1.getHead());
echo("QueueSample.Size = " + queue1.getSize());
// Invoke "clearQueue" in QueueSampler MXBean
//
echo("\nInvoke clearQueue() in QueueSampler MXBean...");
mxbeanProxy.clearQueue();
// Get QueueSample attribute in QueueSampler MXBean
//
QueueSample queue2 = mxbeanProxy.getQueueSample();
echo("\nQueueSample.Date = " + queue2.getDate());
echo("QueueSample.Head = " + queue2.getHead());
echo("QueueSample.Size = " + queue2.getSize());
waitForEnterPressed();
// Close MBeanServer connection
//
echo("\nClose the connection to the server");
jmxc.close();
echo("\nBye! Bye!");
}
private static void echo(String msg) {
System.out.println(msg);
}
private static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void waitForEnterPressed() {
try {
echo("\nPress <Enter> to continue...");
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}