1 22 package org.jboss.management.mejb; 23 24 import javax.jms.*; 25 import javax.management.JMException ; 26 import javax.management.Notification ; 27 import javax.management.NotificationFilter ; 28 import javax.management.NotificationListener ; 29 import javax.management.ObjectName ; 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 33 import javax.rmi.PortableRemoteObject ; 34 import java.rmi.RemoteException ; 35 import java.util.Hashtable ; 36 37 43 public class JMSClientNotificationListener 44 extends ClientNotificationListener 45 implements MessageListener 46 { 47 public JMSClientNotificationListener(ObjectName pSender, 48 NotificationListener pClientListener, 49 Object pHandback, 50 NotificationFilter pFilter, 51 String pQueueJNDIName, 52 String pServerName, 53 MEJB pConnector) throws 54 JMSException, 55 JMException , 56 NamingException , 57 RemoteException 58 { 59 super(pSender, pClientListener, pHandback); 60 61 QueueConnection lConnection = getQueueConnection(pServerName, pQueueJNDIName); 63 QueueSession lSession = lConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 65 Queue lQueue = lSession.createTemporaryQueue(); 66 createListener(pConnector, 68 "org.jboss.management.mejb.JMSNotificationListener", 69 new Object []{pQueueJNDIName, lQueue}, 70 new String []{String .class.getName(), Queue.class.getName()}); 71 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 Notification lNotification = (Notification ) ((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 98 private QueueConnection getQueueConnection(String pServerName, String pQueueJNDIName) 99 throws NamingException , JMSException 100 { 101 Context lJNDIContext = null; 102 if (pServerName != null) 103 { 104 Hashtable lProperties = new Hashtable (); 105 lProperties.put(Context.PROVIDER_URL, pServerName); 106 lJNDIContext = new InitialContext (lProperties); 107 } 108 else 109 { 110 lJNDIContext = new InitialContext (); 111 } 112 Object 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 |