KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > mejb > PollingClientNotificationListener


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 package org.jboss.management.mejb;
23
24 import javax.management.JMException JavaDoc;
25 import javax.management.Notification JavaDoc;
26 import javax.management.NotificationFilter JavaDoc;
27 import javax.management.NotificationListener JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import java.rmi.RemoteException JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * Local Polling Listener to receive the message and send to the listener
35  *
36  * @author Andreas Schaefer
37  * @version $Revision: 37459 $
38  */

39 public class PollingClientNotificationListener
40         extends ClientNotificationListener
41         implements Runnable JavaDoc
42 {
43
44    private MEJB mConnector;
45    private int mSleepingPeriod = 2000;
46
47    public PollingClientNotificationListener(ObjectName JavaDoc pSender,
48                                             NotificationListener JavaDoc pClientListener,
49                                             Object JavaDoc pHandback,
50                                             NotificationFilter JavaDoc pFilter,
51                                             int pSleepingPeriod,
52                                             int pMaximumListSize,
53                                             MEJB pConnector) throws
54            JMException JavaDoc,
55            RemoteException JavaDoc
56    {
57       super(pSender, pClientListener, pHandback);
58       if (pSleepingPeriod > 0)
59       {
60          mSleepingPeriod = pSleepingPeriod;
61       }
62       mConnector = pConnector;
63       // Register the listener as MBean on the remote JMX server
64
createListener(pConnector,
65               "org.jboss.management.mejb.PollingNotificationListener",
66               new Object JavaDoc[]{new Integer JavaDoc(pMaximumListSize), new Integer JavaDoc(pMaximumListSize)},
67               new String JavaDoc[]{Integer.TYPE.getName(), Integer.TYPE.getName()});
68       addNotificationListener(pConnector, pFilter);
69       new Thread JavaDoc(this).start();
70    }
71
72    public void run()
73    {
74       while (true)
75       {
76          try
77          {
78             try
79             {
80                List JavaDoc lNotifications = (List JavaDoc) mConnector.invoke(getRemoteListenerName(),
81                        "getNotifications",
82                        new Object JavaDoc[]{},
83                        new String JavaDoc[]{});
84                Iterator JavaDoc i = lNotifications.iterator();
85                while (i.hasNext())
86                {
87                   Notification JavaDoc lNotification = (Notification JavaDoc) i.next();
88                   mClientListener.handleNotification(lNotification,
89                           mHandback);
90                }
91             }
92             catch (Exception JavaDoc e)
93             {
94                log.error("PollingClientNotificationListener.getNotifications() failed", e);
95             }
96             Thread.sleep(mSleepingPeriod);
97          }
98          catch (InterruptedException JavaDoc e)
99          {
100          }
101       }
102    }
103 }
104
Popular Tags