KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > mx > remoting > pingpong > PingPong


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package org.jboss.test.mx.remoting.pingpong;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Timer JavaDoc;
15 import java.util.TimerTask JavaDoc;
16
17 import javax.management.AttributeChangeNotification JavaDoc;
18 import javax.management.ListenerNotFoundException JavaDoc;
19 import javax.management.MBeanException JavaDoc;
20 import javax.management.MBeanNotificationInfo JavaDoc;
21 import javax.management.MBeanRegistration JavaDoc;
22 import javax.management.MBeanServer JavaDoc;
23 import javax.management.Notification JavaDoc;
24 import javax.management.NotificationFilter JavaDoc;
25 import javax.management.NotificationListener JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 import org.jboss.logging.Logger;
29 import org.jboss.mx.remoting.MBeanLocator;
30 import org.jboss.mx.remoting.tracker.MBeanTracker;
31 import org.jboss.mx.remoting.tracker.MBeanTrackerAction;
32 import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
33 import org.jboss.remoting.ConnectionFailedException;
34 import org.jboss.remoting.ident.Identity;
35
36 /**
37  * PingPong is a simple test mbean that will call ping on other peers
38  * in the JBoss remoting network and will fire a notification and
39  * an attribute change notification when ping is invoked back to listeners.
40  *
41  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
42  * @version $Revision: 30251 $
43  */

44 public class PingPong implements PingPongMBean, MBeanTrackerAction, MBeanRegistration JavaDoc
45 {
46     private static final transient Logger log = Logger.getLogger(PingPong.class.getName());
47     private MBeanTracker tracker;
48     private Timer JavaDoc timer=new Timer JavaDoc(false);
49     private Map JavaDoc friends=new HashMap JavaDoc();
50     private MBeanServer JavaDoc server;
51     private ObjectName JavaDoc objectName;
52     private JBossNotificationBroadcasterSupport broadcaster=new JBossNotificationBroadcasterSupport();
53
54     public Object JavaDoc ping (Object JavaDoc pong)
55     {
56         Notification JavaDoc notification = new Notification JavaDoc("pong",objectName,System.currentTimeMillis());
57         broadcaster.sendNotification(notification);
58             log.debug("ping called: "+pong+", sending notification: "+notification+" for objectName: "+objectName);
59         Notification JavaDoc stateChange = new AttributeChangeNotification JavaDoc(objectName,System.currentTimeMillis(),System.currentTimeMillis(),"State Changed","State",Integer JavaDoc.class.getName(),new Integer JavaDoc(1),new Integer JavaDoc(2));
60         broadcaster.sendNotification(stateChange);
61         return pong;
62     }
63
64     /**
65      * Add a listener to an MBean.
66      *
67      * @param listener implementation of the listener object
68      * @param filter implementation of the filter object or <tt>null</tt>
69      * if no filtering is required
70      * @param handback A handback object associated with each notification
71      * sent by this notification broadcaster.
72      *
73      * @throws IllegalArgumentException if listener is <tt>null</tt>
74      */

75     public void addNotificationListener ( NotificationListener JavaDoc listener,
76                                           NotificationFilter JavaDoc filter,
77                                           Object JavaDoc handback )
78             throws IllegalArgumentException JavaDoc
79     {
80             log.debug("addNotificationListener - listener: "+listener);
81         broadcaster.addNotificationListener(listener,filter,handback);
82     }
83
84     /**
85      * Removes a listener from an MBean.
86      *
87      * @param listener the listener object to remove
88      *
89      * @throws ListenerNotFoundException if the listener was not found
90      */

91     public void removeNotificationListener ( NotificationListener JavaDoc listener )
92             throws ListenerNotFoundException JavaDoc
93     {
94             log.debug("removeNotificationListener - listener: "+listener);
95         broadcaster.removeNotificationListener(listener);
96     }
97
98     /**
99      * Returns the notification metadata associated with the MBean.
100      *
101      * @see MBeanNotificationInfo
102      *
103      * @return MBean's notification metadata
104      */

105     public MBeanNotificationInfo JavaDoc[] getNotificationInfo ()
106     {
107         return new MBeanNotificationInfo JavaDoc[0];
108     }
109
110     /**
111      * This method is called by the MBeanServer after deregistration takes
112      * place.
113      */

114     public void postDeregister ()
115     {
116     }
117
118     /**
119      * This method is called by the MBeanServer after registration takes
120      * place or when registration fails.
121      *
122      * @param registrationDone the MBeanServer passes true when the
123      * MBean was registered, false otherwise.
124      */

125     public void postRegister (Boolean JavaDoc registrationDone)
126     {
127     }
128
129     /**
130      * This method is called by the MBeanServer before deregistration takes
131      * place.<p>
132      *
133      * The MBean can throw an exception, this will stop the deregistration.
134      * The exception is forwarded to the invoker wrapped in
135      * an MBeanRegistrationException.
136      */

137     public void preDeregister ()
138             throws Exception JavaDoc
139     {
140         stop();
141     }
142
143     /**
144      * This method is called by the MBeanServer before registration takes
145      * place. The MBean is passed a reference of the MBeanServer it is
146      * about to be registered with. The MBean must return the ObjectName it
147      * will be registered with. The MBeanServer can pass a suggested object
148      * depending upon how the MBean is registered.<p>
149      *
150      * The MBean can stop the registration by throwing an exception.The
151      * exception is forwarded to the invoker wrapped in an
152      * MBeanRegistrationException.
153      *
154      * @param MBeanServer the MBeanServer the MBean is about to be
155      * registered with.
156      * @param ObjectName the suggested ObjectName supplied by the
157      * MBeanServer.
158      * @return the actual ObjectName to register this MBean with.
159      * @exception Exception for any error, the MBean is not registered.
160      */

161     public ObjectName JavaDoc preRegister (MBeanServer JavaDoc server, ObjectName JavaDoc name)
162             throws Exception JavaDoc
163     {
164         this.server = server;
165         this.objectName = name;
166         start();
167         return name;
168     }
169
170
171     /**
172      * Describe <code>start</code> method here.
173      *
174      * @jmx.managed-operation description="Second lifecycle method called after mbeans attributes are set. During this method declared mbean dependencies are available and may be used. After completion the mbean should be completely usable."
175      * impact="ACTION"
176      */

177     public void start () throws Exception JavaDoc
178     {
179         tracker = new MBeanTracker(server,new Class JavaDoc[]{PingPongMBean.class},null,false,null,true,this);
180         timer.scheduleAtFixedRate(new Pinger(),5000L,5000L);
181     }
182
183     /**
184      * Describe <code>stop</code> method here.
185      *
186      * @jmx.managed-operation description="First shutdown lifecycle method. This method should undo the effects of start"
187      * impact="ACTION"
188      */

189     public void stop ()
190     {
191         if (tracker!=null)
192         {
193             tracker.destroy();
194             tracker=null;
195         }
196     }
197
198     /**
199      * called when a mbean notification is fired
200      *
201      * @param locator
202      * @param notification
203      * @param handback
204      */

205     public void mbeanNotification (MBeanLocator locator, Notification JavaDoc notification, Object JavaDoc handback)
206     {
207         log.info("received notification from: "+locator+", notification: "+notification);
208     }
209
210     /**
211      * called when an MBean is registered with the MBeanServer
212      *
213      * @param locator
214      */

215     public void mbeanRegistered (MBeanLocator locator)
216     {
217         if (Identity.get(server).equals(locator.getIdentity()))
218         {
219             // ignore myself
220
return;
221         }
222         log.info("found a new friend to play ping pong with: "+locator);
223         PingPongMBean friend=null;
224         try
225         {
226             friend=(PingPongMBean)locator.narrow(PingPongMBean.class);
227         }
228         catch (Exception JavaDoc e)
229         {
230             log.error("error casting my friend to PingPongMBean - his locator is: "+locator,e);
231             return;
232         }
233         synchronized(friends)
234         {
235             friends.put(locator,friend);
236         }
237     }
238
239     /**
240      * called when the mbean state changes. Note: this method will only be called on MBeans that have a
241      * <tt>State</tt> attribute and where state change attribute notifications are fired
242      *
243      * @param locator
244      * @param oldState
245      * @param newState
246      */

247     public void mbeanStateChanged (MBeanLocator locator, int oldState, int newState)
248     {
249         log.info("one of my partners ("+locator+") changed its state from: "+oldState+" to: "+newState);
250     }
251
252     /**
253      * called when an MBean is unregistered with the MBeanServer
254      *
255      * @param locator
256      */

257     public void mbeanUnregistered (MBeanLocator locator)
258     {
259         log.info("I lost a friend, "+locator);
260         friends.remove(locator);
261     }
262
263     final class Pinger extends TimerTask JavaDoc
264     {
265         /**
266          * The action to be performed by this timer task.
267          */

268         public void run ()
269         {
270             Map JavaDoc copy = null;
271             synchronized (friends)
272             {
273                 copy = new HashMap JavaDoc(friends);
274             }
275             if (copy.isEmpty())
276             {
277                 log.info("I don't have any friends on the network, how boring...");
278                 return;
279             }
280             Iterator JavaDoc iter = copy.keySet().iterator();
281             Integer JavaDoc myhash=new Integer JavaDoc(hashCode());
282             while(iter.hasNext())
283             {
284                 MBeanLocator l=(MBeanLocator)iter.next();
285                 try
286                 {
287                     Object JavaDoc obj = l.getServerLocator().getMBeanServer().invoke(l.getObjectName(),"ping",new Object JavaDoc[]{myhash},new String JavaDoc[]{Object JavaDoc.class.getName()});
288                     //PingPongMBean friend=(PingPongMBean)copy.get(l);
289
log.info("pinging my friend at: "+l+", with: "+myhash);
290                     // send my friend a ping
291
//Object obj = friend.ping(myhash);
292
if (!obj.equals(myhash))
293                     {
294                         log.warn("My friend passed me back something I don't understand?! I passed him: "+myhash+", I received: "+obj);
295                     }
296                 }
297                 catch (Throwable JavaDoc ex)
298                 {
299                     if (ex instanceof MBeanException JavaDoc)
300                     {
301                         MBeanException JavaDoc mbe=(MBeanException JavaDoc)ex;
302                         if (mbe.getTargetException() instanceof ConnectionFailedException)
303                         {
304                             log.info("my friend died during a ping, "+l);
305                             return;
306                         }
307                     }
308                     log.warn("My friend doesn't like me, he gave me an exception back",ex);
309                 }
310             }
311             copy=null;
312         }
313     }
314 }
315
316
Popular Tags