KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
26 import java.util.LinkedList JavaDoc;
27
28 import javax.management.Notification JavaDoc;
29 import javax.management.NotificationListener JavaDoc;
30
31 import org.jboss.ha.jmx.HAServiceMBeanSupport;
32
33 /**
34  *
35  * This MBean is an example showing how to extend a cluster notification broadcaster
36  * Use the sendNotiication() operation to trigger new clustered notifications.
37  * Observe the status of each instance of this mbean in the participating cluster partition nodes.
38  *
39  * @author Ivelin Ivanov <ivelin@apache.org>
40  *
41  */

42 public class HANotificationBroadcasterExample
43   extends HAServiceMBeanSupport
44   implements HANotificationBroadcasterExampleMBean
45 {
46   
47   /**
48    *
49    * On service start, subscribes to notification sent by this broadcaster or its remote peers.
50    *
51    */

52   protected void startService() throws Exception JavaDoc
53   {
54     super.startService();
55     addNotificationListener(listener_, /* no need for filter */ null, /* no handback object */ null);
56   }
57   
58   /**
59    *
60    * On service stop, unsubscribes to notification sent by this broadcaster or its remote peers.
61    *
62    */

63   protected void stopService() throws Exception JavaDoc
64   {
65     removeNotificationListener(listener_);
66     super.stopService();
67   }
68   
69   /**
70    * Broadcasts a notification to the cluster partition.
71    *
72    * This example does not ensure that a notification sequence number
73    * is unique throughout the partition.
74    *
75    */

76   public void sendTextMessage(String JavaDoc message)
77   {
78     long now = System.currentTimeMillis();
79     Notification JavaDoc notification =
80       new Notification JavaDoc("hanotification.example.counter", super.getServiceName(), now, now, message);
81     sendNotification(notification);
82   }
83
84   /**
85    * Lists the notifications received on the cluster partition
86    */

87   public Collection JavaDoc getReceivedNotifications()
88   {
89     return messages_;
90   }
91
92
93   Collection JavaDoc messages_ = new LinkedList JavaDoc();
94  
95   NotificationListener JavaDoc listener_ = new NotificationListener JavaDoc()
96   {
97     public void handleNotification(Notification JavaDoc notification,
98                                    java.lang.Object JavaDoc handback)
99      {
100        messages_.add( notification.getMessage() );
101      }
102   };
103   
104 }
105
Popular Tags