View Javadoc
1   /*
2    * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
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   *
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.
22   */
23  
24  /*
25   * @test
26   * @bug 6333528
27   * @summary Check that the initial threshold is properly used by the observed
28   *          objects added before the counter monitor is started as well as by
29   *          the observed objects which are added once the monitor is started.
30   * @author Luis-Miguel Alventosa
31   *
32   * @run clean CounterMonitorInitThresholdTest
33   * @run build CounterMonitorInitThresholdTest
34   * @run main CounterMonitorInitThresholdTest
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          // Retrieve the platform MBean server
90          //
91          System.out.println("\nRetrieve the platform MBean server");
92          MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
93          String domain = mbs.getDefaultDomain();
94  
95          // Create and register TestMBeans
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         // Create and register CounterMonitorMBean
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         // Add observed object name1
132         //
133         System.out.println("\nObservedObject \"" + name1 +
134             "\" registered before starting the monitor");
135         cm.addObservedObject(name1);
136 
137         // Start the monitor
138         //
139         System.out.println("\nStart monitoring...");
140         cm.start();
141 
142         // Play with counter for name1
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         // Add observed object name2
162         //
163         System.out.println("\nObservedObject \"" + name2 +
164             "\" registered after starting the monitor");
165         cm.addObservedObject(name2);
166 
167         // Play with counter for name2
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         // Stop the monitor
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 }