KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > jmx > listener > BasicListener


1 /***
2  * Fractal JMX
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  */

21 package org.objectweb.fractal.jmx.listener;
22
23 import javax.management.Notification JavaDoc;
24 import javax.management.monitor.MonitorNotification JavaDoc;
25 import javax.management.NotificationListener JavaDoc;
26
27 import javax.management.monitor.PeriodicMonitor JavaDoc;
28
29 import java.util.Date JavaDoc;
30
31 /**
32  * Provides a simple listener component.
33  * An object of this class can be used to receive notifications sent out by standard monitoring services
34  * (counter, gauge and string) of the JMX specification. All notifications received will be write to user screen.
35  *
36  * @version 0.1
37  */

38 public class BasicListener implements NotificationListener JavaDoc {
39
40     // -------------------------------------------------------------------------
41
// Implementation of the NotificationListener interface
42
// -------------------------------------------------------------------------
43
/**
44      * Invoked when a JMX notification occurs. Currently handles {@link MonitorNotification monitor notifications}
45      * sent out by standard monitoring services (counter, gauge and string) of the JMX specification.
46      * This listener will print to user screen the following information (part of the received notification):
47      * <ul>
48      * <li>the {@link Date} on which the Event initially occurred (initialized from the notification timestamp),</li>
49      * <li>the notification type,</li>
50      * <li>the monitor source on which the Event initially occurred,</li>
51      * <li>the observed object of this monitor notification,</li>
52      * <li>the observed attribute of this monitor notification,</li>
53      * <li>the threshold/string (depending on the monitor type) that triggered off this monitor notification,</li>
54      * <li>the derived gauge of this monitor notification.</li>
55      * </ul>
56      *
57      * @param n The notification (should be an instance of {@link MonitorNotification}).
58      * @param handback An opaque object, not used by this simple implementation.
59      *
60      * @throws ClassCastException if the received notification is not instance of {@link MonitorNotification}.
61      */

62     public void handleNotification(Notification JavaDoc n, Object JavaDoc handback) {
63         MonitorNotification JavaDoc notif = (MonitorNotification JavaDoc) n;
64         StringBuffer JavaDoc msg = new StringBuffer JavaDoc(150);
65         msg.append("\n");
66         if (notif.getType().equals(MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED)
67             || notif.getType().equals(MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED)) {
68             msg.append("***** String Monitor");
69         } else if (notif.getType().equals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED)) {
70             msg.append("***** Counter Monitor");
71         } else if (notif.getType().equals(MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED)
72             || notif.getType().equals(MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED)) {
73                 msg.append("***** Gauge Monitor");
74         } else if (notif.getType().equals(PeriodicMonitor.PERIODIC_SAMPLING)) {
75             msg.append("***** Periodic Monitor");
76         } else
77             msg.append("***** Unexpected Monitor notication !!!");
78         msg.append(" (" + new Date JavaDoc(notif.getTimeStamp()) + ")");
79         msg.append("\n* NotificationType: " + notif.getType());
80         msg.append("\n* Src: " + notif.getSource());
81         msg.append("\n* ObservedObject: " + notif.getObservedObject());
82         msg.append("\n* ObservedAttribute: " + notif.getObservedAttribute());
83         msg.append("\n* Trigger: " + notif.getTrigger());
84         msg.append("\n* DerivedGauge: " + notif.getDerivedGauge());
85         //msg.append("\n* handback: " + handback);
86
msg.append("\n*****");
87         System.out.println(msg);
88     }
89 }
90
91
Popular Tags