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 import java.lang.management.ManagementFactory;
38 import javax.management.MBeanServer;
39 import javax.management.MBeanServerInvocationHandler;
40 import javax.management.Notification;
41 import javax.management.NotificationEmitter;
42 import javax.management.NotificationListener;
43 import javax.management.ObjectName;
44 import javax.management.monitor.CounterMonitor;
45 import javax.management.monitor.CounterMonitorMBean;
46 import javax.management.monitor.MonitorNotification;
47
48 public class CounterMonitorInitThresholdTest {
49
50 public interface TestMBean {
51 public int getCounter();
52 public void setCounter(int count);
53 }
54
55 public static class Test implements TestMBean {
56 public int getCounter() {
57 return count;
58 }
59 public void setCounter(int count) {
60 this.count = count;
61 }
62 private int count = 0;
63 }
64
65 public static class Listener implements NotificationListener {
66 public void handleNotification(Notification n, Object hb) {
67 System.out.println("\tReceived notification: " + n.getType());
68 if (n instanceof MonitorNotification) {
69 MonitorNotification mn = (MonitorNotification) n;
70 System.out.println("\tSource: " +
71 mn.getSource());
72 System.out.println("\tType: " +
73 mn.getType());
74 System.out.println("\tTimeStamp: " +
75 mn.getTimeStamp());
76 System.out.println("\tObservedObject: " +
77 mn.getObservedObject());
78 System.out.println("\tObservedAttribute: " +
79 mn.getObservedAttribute());
80 System.out.println("\tDerivedGauge: " +
81 mn.getDerivedGauge());
82 System.out.println("\tTrigger: " +
83 mn.getTrigger());
84 }
85 }
86 }
87
88 public static void runTest() throws Exception {
89
90
91 System.out.println("\nRetrieve the platform MBean server");
92 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
93 String domain = mbs.getDefaultDomain();
94
95
96
97 ObjectName name1 =
98 new ObjectName(domain +
99 ":type=" + Test.class.getName() +
100 ",name=1");
101 mbs.createMBean(Test.class.getName(), name1);
102 TestMBean mbean1 = (TestMBean)
103 MBeanServerInvocationHandler.newProxyInstance(
104 mbs, name1, TestMBean.class, false);
105 ObjectName name2 =
106 new ObjectName(domain +
107 ":type=" + Test.class.getName() +
108 ",name=2");
109 mbs.createMBean(Test.class.getName(), name2);
110 TestMBean mbean2 = (TestMBean)
111 MBeanServerInvocationHandler.newProxyInstance(
112 mbs, name2, TestMBean.class, false);
113
114
115
116 ObjectName cmn =
117 new ObjectName(domain +
118 ":type=" + CounterMonitor.class.getName());
119 CounterMonitor m = new CounterMonitor();
120 mbs.registerMBean(m, cmn);
121 CounterMonitorMBean cm = (CounterMonitorMBean)
122 MBeanServerInvocationHandler.newProxyInstance(
123 mbs, cmn, CounterMonitorMBean.class, true);
124 ((NotificationEmitter) cm).addNotificationListener(
125 new Listener(), null, null);
126 cm.setObservedAttribute("Counter");
127 cm.setGranularityPeriod(100);
128 cm.setInitThreshold(3);
129 cm.setNotify(true);
130
131
132
133 System.out.println("\nObservedObject \"" + name1 +
134 "\" registered before starting the monitor");
135 cm.addObservedObject(name1);
136
137
138
139 System.out.println("\nStart monitoring...");
140 cm.start();
141
142
143
144 System.out.println("\nTest ObservedObject \"" + name1 + "\"");
145 for (int i = 0; i < 4; i++) {
146 mbean1.setCounter(i);
147 System.out.println("\nCounter = " + mbean1.getCounter());
148 Thread.sleep(300);
149 Number thresholdValue = cm.getThreshold(name1);
150 System.out.println("Threshold = " + thresholdValue);
151 if (thresholdValue.intValue() != 3) {
152 System.out.println("Wrong threshold! Current value = " +
153 thresholdValue + " Expected value = 3");
154 System.out.println("\nStop monitoring...");
155 cm.stop();
156 throw new IllegalArgumentException("wrong threshold");
157 }
158 Thread.sleep(300);
159 }
160
161
162
163 System.out.println("\nObservedObject \"" + name2 +
164 "\" registered after starting the monitor");
165 cm.addObservedObject(name2);
166
167
168
169 System.out.println("\nTest ObservedObject \"" + name2 + "\"");
170 for (int i = 0; i < 4; i++) {
171 mbean2.setCounter(i);
172 System.out.println("\nCounter = " + mbean2.getCounter());
173 Thread.sleep(300);
174 Number thresholdValue = cm.getThreshold(name2);
175 System.out.println("Threshold = " + thresholdValue);
176 if (thresholdValue.intValue() != 3) {
177 System.out.println("Wrong threshold! Current value = " +
178 thresholdValue + " Expected value = 3");
179 System.out.println("\nStop monitoring...");
180 cm.stop();
181 throw new IllegalArgumentException("wrong threshold");
182 }
183 Thread.sleep(300);
184 }
185
186
187
188 System.out.println("\nStop monitoring...");
189 cm.stop();
190 }
191
192 public static void main(String[] args) throws Exception {
193 runTest();
194 }
195 }