1 22 package org.jboss.management.mejb; 23 24 import org.jboss.logging.Logger; 25 26 import javax.jms.*; 27 import javax.management.Notification ; 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingException ; 31 import javax.rmi.PortableRemoteObject ; 32 33 40 public class JMSNotificationListener 41 implements JMSNotificationListenerMBean 42 { 43 private static final Logger log = Logger.getLogger(JMSNotificationListener.class); 44 45 private transient QueueSender mSender; 49 private transient QueueSession mSession; 50 private String mJNDIName; 51 private Queue mQueue; 52 53 public JMSNotificationListener(String pJNDIName, 54 Queue pQueue) throws JMSException 55 { 56 mJNDIName = pJNDIName; 57 mQueue = pQueue; 58 } 59 60 67 public void handleNotification(Notification pNotification, 68 Object pHandback) 69 { 70 try 71 { 72 if (mSender == null) 73 { 74 QueueConnection lConnection = getQueueConnection(mJNDIName); 76 mSession = lConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 77 mSender = mSession.createSender(mQueue); 78 } 79 Message lMessage = mSession.createObjectMessage(pNotification); 81 mSender.send(lMessage); 82 } 83 catch (Exception e) 84 { 85 log.error("failed to handle notification", e); 86 } 87 } 88 89 97 public boolean equals(Object 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 116 public int hashCode() 117 { 118 return mQueue.hashCode(); 119 } 120 121 126 private QueueConnection getQueueConnection(String pJNDIName) 127 throws NamingException , JMSException 128 { 129 Context aJNDIContext = new InitialContext (); 130 Object 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 |