01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package mx4j.tools.stats;
10:
11: import java.util.Date;
12: import javax.management.AttributeChangeNotification;
13: import javax.management.AttributeChangeNotificationFilter;
14: import javax.management.Notification;
15: import javax.management.NotificationListener;
16:
17: /**
18: * NotificationStatisticsRecorder records statistics of an attribute
19: * based on notifications emitted when it changes. The observed MBean has to
20: * emit notifications when the value change
21: *
22: * @version $Revision: 1.4 $
23: */
24: public class NotificationStatisticsRecorder extends
25: ObserverStatisticsRecorder implements NotificationListener {
26: protected boolean registered = false;
27:
28: protected void startObserving() throws Exception {
29: AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
30: filter.enableAttribute(observedAttribute);
31: server
32: .addNotificationListener(observedName, this , filter,
33: null);
34: registered = true;
35: }
36:
37: protected void stopObserving() throws Exception {
38: if (registered) {
39: server.removeNotificationListener(observedName, this );
40: }
41: }
42:
43: public void handleNotification(Notification notification,
44: Object object) {
45: AttributeChangeNotification anot = (AttributeChangeNotification) notification;
46: addEntry(new Date(), (Number) anot.getNewValue());
47: }
48:
49: public String toString() {
50: return "NotificationStatisticsRecorder";
51: }
52: }
|