KickJava   Java API By Example, From Geeks To Geeks.

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


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.jms.*;
25 import javax.management.JMException JavaDoc;
26 import javax.management.Notification JavaDoc;
27 import javax.management.NotificationFilter JavaDoc;
28 import javax.management.NotificationListener JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.rmi.PortableRemoteObject JavaDoc;
34 import java.rmi.RemoteException JavaDoc;
35 import java.util.Hashtable JavaDoc;
36
37 /**
38  * Local JMS Listener to receive the message and send to the listener
39  *
40  * @author ???
41  * @version $Revision: 37459 $
42  */

43 public class JMSClientNotificationListener
44         extends ClientNotificationListener
45         implements MessageListener
46 {
47    public JMSClientNotificationListener(ObjectName JavaDoc pSender,
48                                         NotificationListener JavaDoc pClientListener,
49                                         Object JavaDoc pHandback,
50                                         NotificationFilter JavaDoc pFilter,
51                                         String JavaDoc pQueueJNDIName,
52                                         String JavaDoc pServerName,
53                                         MEJB pConnector) throws
54            JMSException,
55            JMException JavaDoc,
56            NamingException JavaDoc,
57            RemoteException JavaDoc
58    {
59       super(pSender, pClientListener, pHandback);
60
61       // Get the JMS QueueConnectionFactory from the J2EE server
62
QueueConnection lConnection = getQueueConnection(pServerName, pQueueJNDIName);
63       // Create JMS Session and create temporary Queue
64
QueueSession lSession = lConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
65       Queue lQueue = lSession.createTemporaryQueue();
66       // Register the listener as MBean on the remote JMX server
67
createListener(pConnector,
68               "org.jboss.management.mejb.JMSNotificationListener",
69               new Object JavaDoc[]{pQueueJNDIName, lQueue},
70               new String JavaDoc[]{String JavaDoc.class.getName(), Queue.class.getName()});
71       // Create JMS message receiver, create local message listener and set it as message
72
// listener to the receiver
73
QueueReceiver lReceiver = lSession.createReceiver(lQueue, null);
74       lReceiver.setMessageListener(this);
75       addNotificationListener(pConnector, pFilter);
76    }
77
78    public void onMessage(Message pMessage)
79    {
80       try
81       {
82          // Unpack the Notification from the Message and hand it over to the clients
83
// Notification Listener
84
Notification JavaDoc lNotification = (Notification JavaDoc) ((ObjectMessage) pMessage).getObject();
85          mClientListener.handleNotification(lNotification, mHandback);
86       }
87       catch (JMSException e)
88       {
89          log.error("failed to handle notification", e);
90       }
91    }
92
93    /**
94     * Creates a SurveyManagement bean.
95     *
96     * @return Returns a SurveyManagement bean for use by the Survey handler.
97     */

98    private QueueConnection getQueueConnection(String JavaDoc pServerName, String JavaDoc pQueueJNDIName)
99            throws NamingException JavaDoc, JMSException
100    {
101       Context JavaDoc lJNDIContext = null;
102       if (pServerName != null)
103       {
104          Hashtable JavaDoc lProperties = new Hashtable JavaDoc();
105          lProperties.put(Context.PROVIDER_URL, pServerName);
106          lJNDIContext = new InitialContext JavaDoc(lProperties);
107       }
108       else
109       {
110          lJNDIContext = new InitialContext JavaDoc();
111       }
112       Object JavaDoc aRef = lJNDIContext.lookup(pQueueJNDIName);
113       QueueConnectionFactory aFactory = (QueueConnectionFactory)
114               PortableRemoteObject.narrow(aRef, QueueConnectionFactory.class);
115       QueueConnection lConnection = aFactory.createQueueConnection();
116       lConnection.start();
117       return lConnection;
118    }
119
120 }
121
Popular Tags