1 22 package org.jboss.test.jmsra.bean; 23 24 import javax.ejb.MessageDrivenBean ; 25 import javax.ejb.MessageDrivenContext ; 26 import javax.ejb.EJBException ; 27 28 import javax.naming.*; 29 import javax.jms.*; 30 31 import org.jboss.logging.Logger; 32 33 42 43 public class TopicAdapter implements MessageDrivenBean , MessageListener{ 44 45 private static final Logger log = Logger.getLogger(TopicAdapter.class); 46 47 private static final String CONNECTION_JNDI = "java:comp/env/jms/MyQueueConnection"; 48 private static final String QUEUE_JNDI = "java:comp/env/jms/QueueName"; 49 private MessageDrivenContext ctx = null; 50 private Queue queue = null; 51 private QueueConnection queueConnection = null; 52 53 public TopicAdapter() { 54 55 } 56 public void setMessageDrivenContext(MessageDrivenContext ctx) 57 { 58 this.ctx = ctx; 59 } 60 61 public void ejbCreate() { 62 try { 63 Context context = new InitialContext(); 64 queue = (Queue)context.lookup(QUEUE_JNDI); 65 66 QueueConnectionFactory factory = (QueueConnectionFactory)context.lookup(CONNECTION_JNDI); 67 queueConnection = factory.createQueueConnection(); 68 69 } catch (Exception ex) { 70 log.debug("failed", ex); 72 throw new EJBException (ex.toString()); 73 } 74 } 75 76 public void ejbRemove() { 77 if(queueConnection != null) { 78 try { 79 queueConnection.close(); 80 } catch (Exception e) { 81 log.debug("failed", e); 82 } 83 } 84 ctx=null; 85 } 86 87 public void onMessage(Message message) { 88 log.debug("TopicBean got message" + message.toString() ); 89 QueueSession queueSession = null; 90 try { 91 QueueSender queueSender = null; 92 93 queueSession = 94 queueConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); 95 queueSender = queueSession.createSender(queue); 96 queueSender.send(message); 97 98 99 } catch (JMSException ex) { 100 101 log.debug("failed", ex); 102 ctx.setRollbackOnly(); 103 throw new EJBException (ex.toString()); 104 } finally { 105 if (queueSession != null) { 106 try { 107 queueSession.close(); 108 } catch (Exception e) { 109 log.debug("failed", e); 110 } 111 } 112 } 113 } 114 } | Popular Tags |