KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > services > NotificationListener


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.monitor.services;
23
24 import javax.management.Notification JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26
27 import org.jboss.logging.DynamicLogger;
28 import org.jboss.system.ListenerServiceMBeanSupport;
29
30 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
31
32 /**
33  * A simple JMX notification listener that outputs notifications as log.INFO
34  * messages, and demonstrates the usefulness of ListenerServiceMBeanSupport.
35  *
36  * @jmx:mbean
37  * extends="org.jboss.system.ListenerServiceMBean"
38  *
39  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
40  * @version $Revision: 37459 $
41  */

42 public class NotificationListener extends ListenerServiceMBeanSupport
43    implements NotificationListenerMBean
44 {
45    
46    // Private Data --------------------------------------------------
47

48    /** Number of processed JMX notifications */
49    private SynchronizedLong notificationCount;
50    
51    /** Dynamic subscriptions flag */
52    private boolean dynamicSubscriptions;
53    
54    /** Listener MBean */
55    private ObjectName JavaDoc notificationListener;
56    
57    // Protected Data ------------------------------------------------
58

59    /** The dynamic logger */
60    protected DynamicLogger log = DynamicLogger.getDynamicLogger(super.log.getName());
61    
62    // Constructors --------------------------------------------------
63

64    /**
65     * CTOR
66     */

67    public NotificationListener()
68    {
69       notificationCount = new SynchronizedLong(0);
70       dynamicSubscriptions = true;
71       notificationListener = null;
72       log.setLogLevel(DynamicLogger.LOG_LEVEL_INFO);
73    }
74    
75    // NotificationListenerMBean Implementation -----------------------
76

77    /**
78     * Number of notifications received.
79     *
80     * @jmx:managed-attribute
81     */

82    public long getNotificationCount()
83    {
84       return this.notificationCount.get();
85    }
86
87    /**
88     * Enables/disables dynamic subscriptions
89     *
90     * @jmx:managed-attribute
91     */

92    public void setDynamicSubscriptions(boolean dynamicSubscriptions)
93    {
94       this.dynamicSubscriptions = dynamicSubscriptions;
95    }
96
97    /**
98     * Gets the dynamic subscriptions status
99     *
100     * @jmx:managed-attribute
101     */

102    public boolean getDynamicSubscriptions()
103    {
104       return this.dynamicSubscriptions;
105    }
106    
107    /**
108     * Sets listener of notifications
109     *
110     * @jmx:managed-attribute
111     */

112    public void setNotificationListener(ObjectName JavaDoc notificationListener)
113    {
114       this.notificationListener = notificationListener;
115    }
116    
117    /**
118     * Gets listener of notifications
119     *
120     * @jmx:managed-attribute
121     */

122    public ObjectName JavaDoc getNotificationListener()
123    {
124       return this.notificationListener;
125    }
126    
127    /**
128     * Sets the dynamic log level
129     *
130     * @jmx:managed-attribute
131     */

132    public void setLogLevel(String JavaDoc logLevel)
133    {
134       log.setLogLevelAsString(logLevel);
135    }
136    
137    /**
138     * Gets the dynamic log level
139     *
140     * @jmx:managed-attribute
141     */

142    public String JavaDoc getLogLevel()
143    {
144       return log.getLogLevelAsString();
145    }
146    
147    // Lifecycle control (ServiceMBeanSupport) -----------------------
148

149    /**
150     * Start
151     */

152    public void startService() throws Exception JavaDoc
153    {
154       if (this.notificationListener == null)
155       {
156          super.subscribe(this.dynamicSubscriptions); // listener is me!
157
}
158       else
159       {
160          super.subscribe(this.dynamicSubscriptions, this.notificationListener);
161       }
162    }
163    
164    /**
165     * Stop
166     */

167    public void stopService() throws Exception JavaDoc
168    {
169       // unsubscribe for notifications
170
super.unsubscribe();
171    }
172    
173    /**
174     * Overriden to add handling!
175     */

176    public void handleNotification2(Notification JavaDoc notification, Object JavaDoc handback)
177    {
178       log.log("Got notification (#" + Long.toString(this.notificationCount.increment())
179              + "): " + notification + ", handback: " + handback);
180    }
181 }
182
Popular Tags