KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > jmx > examples > HANotificationBroadcasterClientExample


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
23 package org.jboss.ha.jmx.examples;
24
25 import java.security.InvalidParameterException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.LinkedList JavaDoc;
28
29 import javax.management.InstanceNotFoundException JavaDoc;
30 import javax.management.ListenerNotFoundException JavaDoc;
31 import javax.management.MBeanException JavaDoc;
32 import javax.management.MalformedObjectNameException JavaDoc;
33 import javax.management.Notification JavaDoc;
34 import javax.management.NotificationListener JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36 import javax.management.ReflectionException JavaDoc;
37
38 import org.jboss.system.ServiceMBeanSupport;
39
40 /**
41  *
42  * This MBean is an example that shows how to delegate notification services to a HANotificationBroadcaster.
43  * Use the sendNotiication() operation to trigger new clustered notifications.
44  * Observe the status of each instance of this mbean in the participating cluster partition nodes.
45  *
46  * @author Ivelin Ivanov <ivelin@apache.org>
47  *
48  */

49 public class HANotificationBroadcasterClientExample
50   extends ServiceMBeanSupport
51   implements HANotificationBroadcasterClientExampleMBean, NotificationListener JavaDoc
52 {
53
54   /**
55    *
56    * On service start, subscribes to notification sent by this broadcaster or its remote peers.
57    *
58    */

59   protected void startService() throws Exception JavaDoc
60   {
61     super.startService();
62     addHANotificationListener(this);
63   }
64
65   /**
66    *
67    * On service stop, unsubscribes to notification sent by this broadcaster or its remote peers.
68    *
69    */

70   protected void stopService() throws Exception JavaDoc
71   {
72     removeHANotificationListener(this);
73     super.stopService();
74   }
75
76   /**
77    * Broadcasts a notification to the cluster partition.
78    *
79    * This example does not ensure that a notification sequence number
80    * is unique throughout the partition.
81    *
82    */

83   public void sendTextMessageViaHANBExample(String JavaDoc message)
84     throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
85   {
86     long now = System.currentTimeMillis();
87     Notification JavaDoc notification =
88       new Notification JavaDoc(
89         "hanotification.example.counter",
90         super.getServiceName(),
91         now,
92         now,
93         message);
94     server.invoke(
95         broadcasterName_,
96         "sendNotification",
97         new Object JavaDoc[] { notification },
98         new String JavaDoc[] { Notification JavaDoc.class.getName() }
99         );
100   }
101
102   /**
103    * Lists the notifications received on the cluster partition
104    */

105   public Collection JavaDoc getReceivedNotifications()
106   {
107     return messages_;
108   }
109
110   /**
111    * @return the name of the broadcaster MBean
112    */

113   public String JavaDoc getHANotificationBroadcasterName()
114   {
115     return broadcasterName_ == null ? null : broadcasterName_.toString();
116   }
117
118   /**
119    *
120    * Sets the name of the broadcaster MBean.
121    *
122    * @param
123    */

124   public void setHANotificationBroadcasterName(String JavaDoc newBroadcasterName)
125     throws InvalidParameterException JavaDoc
126   {
127     if (newBroadcasterName == null)
128     {
129       throw new InvalidParameterException JavaDoc("Broadcaster MBean must be specified");
130     }
131     try
132     {
133       broadcasterName_ = new ObjectName JavaDoc(newBroadcasterName);
134     }
135     catch (MalformedObjectNameException JavaDoc mone)
136     {
137       log.error("Broadcaster MBean Object Name is malformed", mone);
138       throw new InvalidParameterException JavaDoc("Broadcaster MBean is not correctly formatted");
139     }
140   }
141
142   protected void addHANotificationListener(NotificationListener JavaDoc listener) throws InstanceNotFoundException JavaDoc
143   {
144     server.addNotificationListener(broadcasterName_, listener,
145       /* no need for filter */ null,
146       /* no handback object */ null);
147   }
148
149   protected void removeHANotificationListener(NotificationListener JavaDoc listener) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc
150   {
151     server.removeNotificationListener(broadcasterName_, listener);
152   }
153
154
155   public void handleNotification(
156     Notification JavaDoc notification,
157     java.lang.Object JavaDoc handback)
158   {
159     messages_.add(notification.getMessage());
160   }
161   
162   // Attributes ----------------------------------------------
163

164   Collection JavaDoc messages_ = new LinkedList JavaDoc();
165
166   /**
167    * The broadcaster MBean that this class listens to and delegates HA notifications to
168    */

169   ObjectName JavaDoc broadcasterName_ = null;
170
171 }
172
Popular Tags