01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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.ha.jmx.examples;
23:
24: import java.util.Collection;
25: import java.util.LinkedList;
26:
27: import javax.management.Notification;
28: import javax.management.NotificationListener;
29:
30: import org.jboss.ha.jmx.HAServiceMBeanSupport;
31:
32: /**
33: *
34: * This MBean is an example showing how to extend a cluster notification broadcaster
35: * Use the sendNotiication() operation to trigger new clustered notifications.
36: * Observe the status of each instance of this mbean in the participating cluster partition nodes.
37: *
38: * @author Ivelin Ivanov <ivelin@jboss.org>
39: *
40: */
41: public class HANotificationBroadcasterExample extends
42: HAServiceMBeanSupport implements
43: HANotificationBroadcasterExampleMBean {
44:
45: /**
46: *
47: * On service start, subscribes to notification sent by this broadcaster or its remote peers.
48: *
49: */
50: protected void startService() throws Exception {
51: super .startService();
52: addNotificationListener(listener_, /* no need for filter */
53: null, /* no handback object */null);
54: }
55:
56: /**
57: *
58: * On service stop, unsubscribes to notification sent by this broadcaster or its remote peers.
59: *
60: */
61: protected void stopService() throws Exception {
62: removeNotificationListener(listener_);
63: super .stopService();
64: }
65:
66: /**
67: * Broadcasts a notification to the cluster partition.
68: *
69: * This example does not ensure that a notification sequence number
70: * is unique throughout the partition.
71: *
72: */
73: public void sendTextMessage(String message) {
74: long now = System.currentTimeMillis();
75: Notification notification = new Notification(
76: "hanotification.example.counter", super
77: .getServiceName(), now, now, message);
78: sendNotification(notification);
79: }
80:
81: /**
82: * Lists the notifications received on the cluster partition
83: */
84: public Collection getReceivedNotifications() {
85: return messages_;
86: }
87:
88: Collection messages_ = new LinkedList();
89:
90: NotificationListener listener_ = new NotificationListener() {
91: public void handleNotification(Notification notification,
92: java.lang.Object handback) {
93: messages_.add(notification.getMessage());
94: }
95: };
96:
97: }
|