KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > stats > NotificationStatisticsRecorder


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.stats;
10
11 import java.util.Date JavaDoc;
12 import javax.management.AttributeChangeNotification JavaDoc;
13 import javax.management.AttributeChangeNotificationFilter JavaDoc;
14 import javax.management.Notification JavaDoc;
15 import javax.management.NotificationListener JavaDoc;
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 ObserverStatisticsRecorder implements NotificationListener JavaDoc
25 {
26    protected boolean registered = false;
27
28    protected void startObserving() throws Exception JavaDoc
29    {
30       AttributeChangeNotificationFilter JavaDoc filter = new AttributeChangeNotificationFilter JavaDoc();
31       filter.enableAttribute(observedAttribute);
32       server.addNotificationListener(observedName, this, filter, null);
33       registered = true;
34    }
35
36    protected void stopObserving() throws Exception JavaDoc
37    {
38       if (registered)
39       {
40          server.removeNotificationListener(observedName, this);
41       }
42    }
43
44    public void handleNotification(Notification JavaDoc notification, Object JavaDoc object)
45    {
46       AttributeChangeNotification JavaDoc anot = (AttributeChangeNotification JavaDoc)notification;
47       addEntry(new Date JavaDoc(), (Number JavaDoc)anot.getNewValue());
48    }
49
50    public String JavaDoc toString()
51    {
52       return "NotificationStatisticsRecorder";
53    }
54 }
55
Popular Tags