001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package example.jmx.monitor;
009:
010: import javax.management.NotificationListener;
011: import javax.management.Notification;
012: import javax.management.monitor.MonitorNotification;
013: import javax.management.monitor.Monitor;
014:
015: /**
016: *
017: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
018: */
019:
020: public class AgentListener2 implements NotificationListener {
021:
022: /*
023: * ------------------------------------------
024: * CONSTRUCTORS
025: * ------------------------------------------
026: */
027:
028: public AgentListener2() {
029: super ();
030: }
031:
032: /*
033: * ------------------------------------------
034: * PUBLIC METHODS
035: * ------------------------------------------
036: */
037:
038: public void handleNotification(Notification notification,
039: Object handback) {
040:
041: MonitorNotification notif = (MonitorNotification) notification;
042:
043: // Get a handle on the CounterMonitor responsible for the notification emmited.
044: //
045: Monitor monitor = (Monitor) notif.getSource();
046:
047: // Process the different types of notifications fired by the CounterMonitor.
048: //
049: String type = notif.getType();
050:
051: try {
052: if (type.equals(MonitorNotification.OBSERVED_OBJECT_ERROR)) {
053: System.out.println("\n\t>> "
054: + notif.getObservedObject().getClass()
055: .getName()
056: + " is not registered in the server");
057: System.out
058: .println("\t>> Stopping the CounterMonitor...\n");
059: monitor.stop();
060: } else if (type
061: .equals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR)) {
062: System.out.println("\n\t>> "
063: + notif.getObservedAttribute()
064: + " attribute is not contained in "
065: + notif.getObservedObject().getClass()
066: .getName());
067: System.out
068: .println("\t>> Stopping the CounterMonitor...\n");
069: monitor.stop();
070: } else if (type
071: .equals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR)) {
072: System.out.println("\n\t>> The type of "
073: + notif.getObservedAttribute()
074: + " attribute is not correct");
075: System.out
076: .println("\t>> Stopping the CounterMonitor...\n");
077: monitor.stop();
078:
079: } else if (type.equals(MonitorNotification.THRESHOLD_ERROR)) {
080: System.out
081: .println("\n\t>> Threshold type is not <Integer>");
082: System.out
083: .println("\t>> Stopping the CounterMonitor...\n");
084: monitor.stop();
085: } else if (type.equals(MonitorNotification.RUNTIME_ERROR)) {
086: System.out.println("\n\t>> Runtime error (?)");
087: System.out
088: .println("\t>> Stopping the CounterMonitor...\n");
089: monitor.stop();
090: } else if (type
091: .equals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED)) {
092: System.out.println("\n\t>> "
093: + notif.getObservedAttribute()
094: + " has reached the threshold\n");
095: } else {
096: System.out.println("\n\t>> Unknown event type (?)\n");
097: }
098: } catch (Exception e) {
099: e.printStackTrace();
100: System.exit(1);
101: }
102: }
103: }
|