001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ha.jmx.examples;
023:
024: import java.security.InvalidParameterException;
025: import java.util.Collection;
026: import java.util.LinkedList;
027:
028: import javax.management.InstanceNotFoundException;
029: import javax.management.ListenerNotFoundException;
030: import javax.management.MBeanException;
031: import javax.management.MalformedObjectNameException;
032: import javax.management.Notification;
033: import javax.management.NotificationListener;
034: import javax.management.ObjectName;
035: import javax.management.ReflectionException;
036:
037: import org.jboss.system.ServiceMBeanSupport;
038:
039: /**
040: *
041: * This MBean is an example that shows how to delegate notification services to a HANotificationBroadcaster.
042: * Use the sendNotiication() operation to trigger new clustered notifications.
043: * Observe the status of each instance of this mbean in the participating cluster partition nodes.
044: *
045: * @author Ivelin Ivanov <ivelin@jboss.org>
046: *
047: */
048: public class HANotificationBroadcasterClientExample extends
049: ServiceMBeanSupport implements
050: HANotificationBroadcasterClientExampleMBean,
051: NotificationListener {
052:
053: /**
054: *
055: * On service start, subscribes to notification sent by this broadcaster or its remote peers.
056: *
057: */
058: protected void startService() throws Exception {
059: super .startService();
060: addHANotificationListener(this );
061: }
062:
063: /**
064: *
065: * On service stop, unsubscribes to notification sent by this broadcaster or its remote peers.
066: *
067: */
068: protected void stopService() throws Exception {
069: removeHANotificationListener(this );
070: super .stopService();
071: }
072:
073: /**
074: * Broadcasts a notification to the cluster partition.
075: *
076: * This example does not ensure that a notification sequence number
077: * is unique throughout the partition.
078: *
079: */
080: public void sendTextMessageViaHANBExample(String message)
081: throws InstanceNotFoundException, MBeanException,
082: ReflectionException {
083: long now = System.currentTimeMillis();
084: Notification notification = new Notification(
085: "hanotification.example.counter", super
086: .getServiceName(), now, now, message);
087: server.invoke(broadcasterName_, "sendNotification",
088: new Object[] { notification },
089: new String[] { Notification.class.getName() });
090: }
091:
092: /**
093: * Lists the notifications received on the cluster partition
094: */
095: public Collection getReceivedNotifications() {
096: return messages_;
097: }
098:
099: /**
100: * @return the name of the broadcaster MBean
101: */
102: public String getHANotificationBroadcasterName() {
103: return broadcasterName_ == null ? null : broadcasterName_
104: .toString();
105: }
106:
107: /**
108: *
109: * Sets the name of the broadcaster MBean.
110: *
111: * @param
112: */
113: public void setHANotificationBroadcasterName(
114: String newBroadcasterName) throws InvalidParameterException {
115: if (newBroadcasterName == null) {
116: throw new InvalidParameterException(
117: "Broadcaster MBean must be specified");
118: }
119: try {
120: broadcasterName_ = new ObjectName(newBroadcasterName);
121: } catch (MalformedObjectNameException mone) {
122: log.error("Broadcaster MBean Object Name is malformed",
123: mone);
124: throw new InvalidParameterException(
125: "Broadcaster MBean is not correctly formatted");
126: }
127: }
128:
129: protected void addHANotificationListener(
130: NotificationListener listener)
131: throws InstanceNotFoundException {
132: server.addNotificationListener(broadcasterName_, listener,
133: /* no need for filter */null,
134: /* no handback object */null);
135: }
136:
137: protected void removeHANotificationListener(
138: NotificationListener listener)
139: throws InstanceNotFoundException, ListenerNotFoundException {
140: server.removeNotificationListener(broadcasterName_, listener);
141: }
142:
143: public void handleNotification(Notification notification,
144: java.lang.Object handback) {
145: messages_.add(notification.getMessage());
146: }
147:
148: // Attributes ----------------------------------------------
149:
150: Collection messages_ = new LinkedList();
151:
152: /**
153: * The broadcaster MBean that this class listens to and delegates HA notifications to
154: */
155: ObjectName broadcasterName_ = null;
156:
157: }
|