author | stefank |
Fri, 17 Apr 2015 17:10:38 +0000 | |
changeset 30266 | ef82cd1f2db3 |
parent 28054 | 8b0ef141b964 |
child 30376 | 2ccf2cf7ea48 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
2 |
* Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4981829 |
|
27 |
* @summary Test that the counter monitor, when running in difference mode, |
|
28 |
* emits a notification every time the threshold is exceeded. |
|
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
29 |
* @author Luis-Miguel Alventosa, Shanliang JIANG |
2 | 30 |
* @run clean CounterMonitorTest |
31 |
* @run build CounterMonitorTest |
|
32 |
* @run main CounterMonitorTest |
|
33 |
*/ |
|
34 |
||
35 |
import javax.management.*; |
|
36 |
import javax.management.monitor.*; |
|
37 |
||
38 |
public class CounterMonitorTest implements NotificationListener { |
|
39 |
||
40 |
// threshold number |
|
41 |
private Number threshold = new Integer(2); |
|
42 |
||
43 |
// modulus number |
|
44 |
private Number modulus = new Integer(7); |
|
45 |
||
46 |
// difference mode flag |
|
47 |
private boolean differenceModeFlag = true; |
|
48 |
||
49 |
// notify flag |
|
50 |
private boolean notifyFlag = true; |
|
51 |
||
52 |
// granularity period |
|
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
53 |
private int granularityperiod = 10; |
2 | 54 |
|
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
55 |
// derived gauge |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
56 |
private volatile int derivedGauge = 2; |
2 | 57 |
|
58 |
// flag to notify that a message has been received |
|
59 |
private volatile boolean messageReceived = false; |
|
60 |
||
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
61 |
private volatile Object observedValue = null; |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
62 |
|
2 | 63 |
// MBean class |
64 |
public class StdObservedObject implements StdObservedObjectMBean { |
|
65 |
public Object getNbObjects() { |
|
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
66 |
echo(">>> StdObservedObject.getNbObjects: " + count); |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
67 |
synchronized(CounterMonitorTest.class) { |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
68 |
observedValue = count; |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
69 |
CounterMonitorTest.class.notifyAll(); |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
70 |
} |
28054
8b0ef141b964
8066952: [TEST-BUG] javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
27796
diff
changeset
|
71 |
return observedValue; |
2 | 72 |
} |
73 |
public void setNbObjects(Object n) { |
|
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
74 |
echo(">>> StdObservedObject.setNbObjects: " + n); |
2 | 75 |
count = n; |
76 |
} |
|
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
77 |
private volatile Object count= null; |
2 | 78 |
} |
79 |
||
80 |
// MBean interface |
|
81 |
public interface StdObservedObjectMBean { |
|
82 |
public Object getNbObjects(); |
|
83 |
public void setNbObjects(Object n); |
|
84 |
} |
|
85 |
||
86 |
// Notification handler |
|
87 |
public void handleNotification(Notification notification, |
|
88 |
Object handback) { |
|
89 |
MonitorNotification n = (MonitorNotification) notification; |
|
90 |
echo("\tInside handleNotification..."); |
|
91 |
String type = n.getType(); |
|
92 |
try { |
|
93 |
if (type.equals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED)) { |
|
94 |
echo("\t\t" + n.getObservedAttribute() + |
|
95 |
" has reached or exceeded the threshold"); |
|
96 |
echo("\t\tDerived Gauge = " + n.getDerivedGauge()); |
|
26623
11289cf5c8fc
8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents:
5506
diff
changeset
|
97 |
|
2 | 98 |
synchronized (this) { |
26623
11289cf5c8fc
8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents:
5506
diff
changeset
|
99 |
messageReceived = true; |
2 | 100 |
notifyAll(); |
101 |
} |
|
102 |
} else { |
|
103 |
echo("\t\tSkipping notification of type: " + type); |
|
104 |
} |
|
105 |
} catch (Exception e) { |
|
106 |
echo("\tError in handleNotification!"); |
|
107 |
e.printStackTrace(System.out); |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Update the counter and check for notifications |
|
113 |
*/ |
|
114 |
public void thresholdNotification() throws Exception { |
|
115 |
||
116 |
CounterMonitor counterMonitor = new CounterMonitor(); |
|
117 |
try { |
|
118 |
MBeanServer server = MBeanServerFactory.newMBeanServer(); |
|
119 |
||
120 |
String domain = server.getDefaultDomain(); |
|
121 |
||
122 |
// Create a new CounterMonitor MBean and add it to the MBeanServer. |
|
123 |
// |
|
124 |
echo(">>> CREATE a new CounterMonitor MBean"); |
|
125 |
ObjectName counterMonitorName = new ObjectName( |
|
126 |
domain + ":type=" + CounterMonitor.class.getName()); |
|
127 |
server.registerMBean(counterMonitor, counterMonitorName); |
|
128 |
||
129 |
echo(">>> ADD a listener to the CounterMonitor"); |
|
130 |
counterMonitor.addNotificationListener(this, null, null); |
|
131 |
||
132 |
// |
|
133 |
// MANAGEMENT OF A STANDARD MBEAN |
|
134 |
// |
|
135 |
||
136 |
echo(">>> CREATE a new StdObservedObject MBean"); |
|
137 |
||
138 |
ObjectName stdObsObjName = |
|
139 |
new ObjectName(domain + ":type=StdObservedObject"); |
|
140 |
StdObservedObject stdObsObj = new StdObservedObject(); |
|
141 |
server.registerMBean(stdObsObj, stdObsObjName); |
|
142 |
||
143 |
echo(">>> SET the attributes of the CounterMonitor:"); |
|
144 |
||
145 |
counterMonitor.addObservedObject(stdObsObjName); |
|
146 |
echo("\tATTRIBUTE \"ObservedObject\" = " + stdObsObjName); |
|
147 |
||
148 |
counterMonitor.setObservedAttribute("NbObjects"); |
|
149 |
echo("\tATTRIBUTE \"ObservedAttribute\" = NbObjects"); |
|
150 |
||
151 |
counterMonitor.setNotify(notifyFlag); |
|
152 |
echo("\tATTRIBUTE \"Notify\" = " + notifyFlag); |
|
153 |
||
154 |
counterMonitor.setInitThreshold(threshold); |
|
155 |
echo("\tATTRIBUTE \"Threshold\" = " + threshold); |
|
156 |
||
157 |
counterMonitor.setGranularityPeriod(granularityperiod); |
|
158 |
echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod); |
|
159 |
||
160 |
counterMonitor.setModulus(modulus); |
|
161 |
echo("\tATTRIBUTE \"Modulus\" = " + modulus); |
|
162 |
||
163 |
counterMonitor.setDifferenceMode(differenceModeFlag); |
|
164 |
echo("\tATTRIBUTE \"DifferenceMode\" = " + differenceModeFlag); |
|
165 |
||
166 |
echo(">>> START the CounterMonitor"); |
|
167 |
counterMonitor.start(); |
|
168 |
||
169 |
// Set initial value |
|
170 |
// |
|
171 |
Integer data = new Integer(0); |
|
172 |
echo(">>> Set data = " + data.intValue()); |
|
173 |
||
174 |
Attribute attrib = new Attribute("NbObjects", data); |
|
175 |
server.setAttribute(stdObsObjName, attrib); |
|
176 |
||
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
177 |
waitObservation(data); |
2 | 178 |
|
179 |
// Loop through the values |
|
180 |
// |
|
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
181 |
while (derivedGauge++ < 10) { |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
182 |
System.out.print(">>> Set data from " + data.intValue()); |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
183 |
data = new Integer(data.intValue() + derivedGauge); |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
184 |
echo(" to " + data.intValue()); |
2 | 185 |
|
186 |
attrib = new Attribute("NbObjects", data); |
|
187 |
server.setAttribute(stdObsObjName, attrib); |
|
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
188 |
waitObservation(data); |
2 | 189 |
|
190 |
echo("\tdoWait in Counter Monitor"); |
|
191 |
doWait(); |
|
192 |
||
193 |
// Check if notification was received |
|
194 |
// |
|
195 |
if (messageReceived) { |
|
196 |
echo("\tOKAY: Notification received"); |
|
197 |
} else { |
|
198 |
echo("\tError: notification missed or not emitted"); |
|
199 |
throw new IllegalStateException("Notification lost"); |
|
200 |
} |
|
201 |
messageReceived = false; |
|
202 |
} |
|
203 |
} finally { |
|
204 |
counterMonitor.stop(); |
|
205 |
} |
|
206 |
||
207 |
echo(">>> Bye! Bye!"); |
|
208 |
} |
|
209 |
||
210 |
/* |
|
26623
11289cf5c8fc
8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents:
5506
diff
changeset
|
211 |
* Wait messageReceived to be true |
2 | 212 |
*/ |
26623
11289cf5c8fc
8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents:
5506
diff
changeset
|
213 |
synchronized void doWait() { |
11289cf5c8fc
8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents:
5506
diff
changeset
|
214 |
while (!messageReceived) { |
11289cf5c8fc
8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents:
5506
diff
changeset
|
215 |
try { |
11289cf5c8fc
8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents:
5506
diff
changeset
|
216 |
wait(); |
11289cf5c8fc
8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents:
5506
diff
changeset
|
217 |
} catch (InterruptedException e) { |
11289cf5c8fc
8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents:
5506
diff
changeset
|
218 |
System.err.println("Got unexpected exception: " + e); |
11289cf5c8fc
8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents:
5506
diff
changeset
|
219 |
e.printStackTrace(); |
2 | 220 |
break; |
221 |
} |
|
222 |
} |
|
223 |
} |
|
224 |
||
27796
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
225 |
private void waitObservation(Object value) { |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
226 |
synchronized (CounterMonitorTest.class) { |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
227 |
while (value != observedValue) { |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
228 |
try { |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
229 |
CounterMonitorTest.class.wait(); |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
230 |
} catch (InterruptedException e) { |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
231 |
System.err.println("Got unexpected exception: " + e); |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
232 |
e.printStackTrace(); |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
233 |
break; |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
234 |
} |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
235 |
} |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
236 |
} |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
237 |
} |
5052f749052a
8065764: javax/management/monitor/CounterMonitorTest.java hangs
sjiang
parents:
26623
diff
changeset
|
238 |
|
2 | 239 |
/* |
240 |
* Print message |
|
241 |
*/ |
|
242 |
void echo(String message) { |
|
243 |
System.out.println(message); |
|
244 |
} |
|
245 |
||
246 |
/* |
|
247 |
* Standalone entry point. |
|
248 |
* |
|
249 |
* Run the test and report to stdout. |
|
250 |
*/ |
|
251 |
public static void main (String args[]) throws Exception { |
|
252 |
CounterMonitorTest test = new CounterMonitorTest(); |
|
253 |
test.thresholdNotification(); |
|
254 |
} |
|
255 |
} |