001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.management.mejb;
023:
024: import javax.jms.*;
025: import javax.management.JMException;
026: import javax.management.Notification;
027: import javax.management.NotificationFilter;
028: import javax.management.NotificationListener;
029: import javax.management.ObjectName;
030: import javax.naming.Context;
031: import javax.naming.InitialContext;
032: import javax.naming.NamingException;
033: import javax.rmi.PortableRemoteObject;
034: import java.rmi.RemoteException;
035: import java.util.Hashtable;
036:
037: /**
038: * Local JMS Listener to receive the message and send to the listener
039: *
040: * @author ???
041: * @version $Revision: 57197 $
042: */
043: public class JMSClientNotificationListener extends
044: ClientNotificationListener implements MessageListener {
045: public JMSClientNotificationListener(ObjectName pSender,
046: NotificationListener pClientListener, Object pHandback,
047: NotificationFilter pFilter, String pQueueJNDIName,
048: String pServerName, MEJB pConnector) throws JMSException,
049: JMException, NamingException, RemoteException {
050: super (pSender, pClientListener, pHandback);
051:
052: // Get the JMS QueueConnectionFactory from the J2EE server
053: QueueConnection lConnection = getQueueConnection(pServerName,
054: pQueueJNDIName);
055: // Create JMS Session and create temporary Queue
056: QueueSession lSession = lConnection.createQueueSession(false,
057: Session.AUTO_ACKNOWLEDGE);
058: Queue lQueue = lSession.createTemporaryQueue();
059: // Register the listener as MBean on the remote JMX server
060: createListener(pConnector,
061: "org.jboss.management.mejb.JMSNotificationListener",
062: new Object[] { pQueueJNDIName, lQueue }, new String[] {
063: String.class.getName(), Queue.class.getName() });
064: // Create JMS message receiver, create local message listener and set it as message
065: // listener to the receiver
066: QueueReceiver lReceiver = lSession.createReceiver(lQueue, null);
067: lReceiver.setMessageListener(this );
068: addNotificationListener(pConnector, pFilter);
069: }
070:
071: public void onMessage(Message pMessage) {
072: try {
073: // Unpack the Notification from the Message and hand it over to the clients
074: // Notification Listener
075: Notification lNotification = (Notification) ((ObjectMessage) pMessage)
076: .getObject();
077: mClientListener
078: .handleNotification(lNotification, mHandback);
079: } catch (JMSException e) {
080: log.error("failed to handle notification", e);
081: }
082: }
083:
084: /**
085: * Creates a SurveyManagement bean.
086: *
087: * @return Returns a SurveyManagement bean for use by the Survey handler.
088: */
089: private QueueConnection getQueueConnection(String pServerName,
090: String pQueueJNDIName) throws NamingException, JMSException {
091: Context lJNDIContext = null;
092: if (pServerName != null) {
093: Hashtable lProperties = new Hashtable();
094: lProperties.put(Context.PROVIDER_URL, pServerName);
095: lJNDIContext = new InitialContext(lProperties);
096: } else {
097: lJNDIContext = new InitialContext();
098: }
099: Object aRef = lJNDIContext.lookup(pQueueJNDIName);
100: QueueConnectionFactory aFactory = (QueueConnectionFactory) PortableRemoteObject
101: .narrow(aRef, QueueConnectionFactory.class);
102: QueueConnection lConnection = aFactory.createQueueConnection();
103: lConnection.start();
104: return lConnection;
105: }
106:
107: }
|