KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.logging.Logger;
25
26 import javax.jms.*;
27 import javax.management.Notification JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.rmi.PortableRemoteObject JavaDoc;
32
33 /**
34  * Remote Listener using JMS to send the event
35  *
36  * @author ???
37  * @version $Revision: 37459 $
38  * @jmx:mbean extends="org.jboss.management.mejb.ListenerMBean"
39  */

40 public class JMSNotificationListener
41         implements JMSNotificationListenerMBean
42 {
43    private static final Logger log = Logger.getLogger(JMSNotificationListener.class);
44
45    // JMS Queue Session and Sender must be created on the server-side
46
// therefore they are transient and created on the first notification
47
// call
48
private transient QueueSender mSender;
49    private transient QueueSession mSession;
50    private String JavaDoc mJNDIName;
51    private Queue mQueue;
52
53    public JMSNotificationListener(String JavaDoc pJNDIName,
54                                   Queue pQueue) throws JMSException
55    {
56       mJNDIName = pJNDIName;
57       mQueue = pQueue;
58    }
59
60    /**
61     * Handles the given notification by sending this to the remote
62     * client listener
63     *
64     * @param pNotification Notification to be send
65     * @param pHandback Handback object
66     */

67    public void handleNotification(Notification JavaDoc pNotification,
68                                   Object JavaDoc pHandback)
69    {
70       try
71       {
72          if (mSender == null)
73          {
74             // Get QueueConnectionFactory, create Connection, Session and then Sender
75
QueueConnection lConnection = getQueueConnection(mJNDIName);
76             mSession = lConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
77             mSender = mSession.createSender(mQueue);
78          }
79          // Create a message and send to the Queue
80
Message lMessage = mSession.createObjectMessage(pNotification);
81          mSender.send(lMessage);
82       }
83       catch (Exception JavaDoc e)
84       {
85          log.error("failed to handle notification", e);
86       }
87    }
88
89    /**
90     * Test if this and the given Object are equal. This is true if the given
91     * object both refer to the same local listener
92     *
93     * @param pTest Other object to test if equal
94     * @return True if both are of same type and
95     * refer to the same local listener
96     */

97    public boolean equals(Object JavaDoc pTest)
98    {
99       if (pTest instanceof JMSNotificationListener)
100       {
101          try
102          {
103             return mQueue.getQueueName().equals(((JMSNotificationListener) pTest).mQueue.getQueueName());
104          }
105          catch (JMSException e)
106          {
107             log.error("unexpcted failure while tetsing equality", e);
108          }
109       }
110       return false;
111    }
112
113    /**
114     * @return Hashcode of the local listener
115     */

116    public int hashCode()
117    {
118       return mQueue.hashCode();
119    }
120
121    /**
122     * Creates a SurveyManagement bean.
123     *
124     * @return Returns a SurveyManagement bean for use by the Survey handler.
125     */

126    private QueueConnection getQueueConnection(String JavaDoc pJNDIName)
127            throws NamingException JavaDoc, JMSException
128    {
129       Context JavaDoc aJNDIContext = new InitialContext JavaDoc();
130       Object JavaDoc aRef = aJNDIContext.lookup(pJNDIName);
131       QueueConnectionFactory aFactory = (QueueConnectionFactory)
132               PortableRemoteObject.narrow(aRef, QueueConnectionFactory.class);
133       QueueConnection lConnection = aFactory.createQueueConnection();
134       lConnection.start();
135       return lConnection;
136    }
137 }
138
Popular Tags