1 22 23 package org.jboss.ha.jmx.examples; 24 25 import java.security.InvalidParameterException ; 26 import java.util.Collection ; 27 import java.util.LinkedList ; 28 29 import javax.management.InstanceNotFoundException ; 30 import javax.management.ListenerNotFoundException ; 31 import javax.management.MBeanException ; 32 import javax.management.MalformedObjectNameException ; 33 import javax.management.Notification ; 34 import javax.management.NotificationListener ; 35 import javax.management.ObjectName ; 36 import javax.management.ReflectionException ; 37 38 import org.jboss.system.ServiceMBeanSupport; 39 40 49 public class HANotificationBroadcasterClientExample 50 extends ServiceMBeanSupport 51 implements HANotificationBroadcasterClientExampleMBean, NotificationListener 52 { 53 54 59 protected void startService() throws Exception 60 { 61 super.startService(); 62 addHANotificationListener(this); 63 } 64 65 70 protected void stopService() throws Exception 71 { 72 removeHANotificationListener(this); 73 super.stopService(); 74 } 75 76 83 public void sendTextMessageViaHANBExample(String message) 84 throws InstanceNotFoundException , MBeanException , ReflectionException 85 { 86 long now = System.currentTimeMillis(); 87 Notification notification = 88 new Notification ( 89 "hanotification.example.counter", 90 super.getServiceName(), 91 now, 92 now, 93 message); 94 server.invoke( 95 broadcasterName_, 96 "sendNotification", 97 new Object [] { notification }, 98 new String [] { Notification .class.getName() } 99 ); 100 } 101 102 105 public Collection getReceivedNotifications() 106 { 107 return messages_; 108 } 109 110 113 public String getHANotificationBroadcasterName() 114 { 115 return broadcasterName_ == null ? null : broadcasterName_.toString(); 116 } 117 118 124 public void setHANotificationBroadcasterName(String newBroadcasterName) 125 throws InvalidParameterException 126 { 127 if (newBroadcasterName == null) 128 { 129 throw new InvalidParameterException ("Broadcaster MBean must be specified"); 130 } 131 try 132 { 133 broadcasterName_ = new ObjectName (newBroadcasterName); 134 } 135 catch (MalformedObjectNameException mone) 136 { 137 log.error("Broadcaster MBean Object Name is malformed", mone); 138 throw new InvalidParameterException ("Broadcaster MBean is not correctly formatted"); 139 } 140 } 141 142 protected void addHANotificationListener(NotificationListener listener) throws InstanceNotFoundException 143 { 144 server.addNotificationListener(broadcasterName_, listener, 145 null, 146 null); 147 } 148 149 protected void removeHANotificationListener(NotificationListener listener) throws InstanceNotFoundException , ListenerNotFoundException 150 { 151 server.removeNotificationListener(broadcasterName_, listener); 152 } 153 154 155 public void handleNotification( 156 Notification notification, 157 java.lang.Object handback) 158 { 159 messages_.add(notification.getMessage()); 160 } 161 162 164 Collection messages_ = new LinkedList (); 165 166 169 ObjectName broadcasterName_ = null; 170 171 } 172 | Popular Tags |