1 22 package org.jboss.test.jmsra.bean; 23 24 import java.rmi.RemoteException ; 25 26 import javax.ejb.SessionBean ; 27 import javax.ejb.SessionContext ; 28 import javax.ejb.EJBException ; 29 30 import javax.naming.InitialContext ; 31 import javax.naming.Context ; 32 33 import javax.jms.QueueConnectionFactory ; 34 import javax.jms.QueueConnection ; 35 import javax.jms.QueueSession ; 36 import javax.jms.QueueReceiver ; 37 import javax.jms.Queue ; 38 import javax.jms.Session ; 39 import javax.jms.Message ; 40 import javax.jms.JMSException ; 41 42 import org.jboss.logging.Logger; 43 44 53 public class QueueRecBean implements SessionBean 54 { 55 56 private final Logger log = Logger.getLogger(this.getClass()); 57 58 61 private static final String CONNECTION_JNDI = "java:comp/env/jms/MyQueueConnection"; 62 63 private QueueConnectionFactory factory = null; 64 65 68 private static final String QUEUE_JNDI = "java:comp/env/jms/QueueName"; 69 70 private SessionContext ctx = null; 71 72 private Queue queue = null; 73 74 public QueueRecBean() 75 { 76 } 77 78 public void setSessionContext(SessionContext ctx) 79 { 80 this.ctx = ctx; 81 } 82 83 public void ejbCreate() 84 { 85 try 86 { 87 Context context = new InitialContext (); 88 89 queue = (Queue ) context.lookup(QUEUE_JNDI); 91 92 factory = (QueueConnectionFactory ) context.lookup(CONNECTION_JNDI); 94 95 } 97 catch (Exception ex) 98 { 99 log.debug("failed", ex); 101 throw new EJBException (ex.toString()); 102 } 103 } 104 105 public void ejbRemove() throws RemoteException 106 { 107 } 108 109 public void ejbActivate() 110 { 111 } 112 113 public void ejbPassivate() 114 { 115 } 116 117 123 public int getMessage() 124 { 125 QueueConnection queueConnection = null; 126 QueueSession queueSession = null; 127 int ret; 128 try 129 { 130 131 queueConnection = factory.createQueueConnection(); 133 queueConnection.start(); 134 queueSession = queueConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); 135 QueueReceiver queueReceiver = queueSession.createReceiver(queue); 137 log.info("Waiting for message"); 138 Message msg = queueReceiver.receive(500L); 139 if (msg != null) 140 { 141 log.info("Recived message: " + msg); 142 int nr = msg.getIntProperty(Publisher.JMS_MESSAGE_NR); 143 log.debug("nr: " + nr); 144 ret = nr; 145 } 146 else 147 { 148 log.info("NO message recived"); 149 ret = -1; 150 } 151 152 } 153 catch (JMSException ex) 154 { 155 156 log.warn("failed", ex); 157 ctx.setRollbackOnly(); 158 throw new EJBException (ex.toString()); 159 } 160 finally 161 { 162 if (queueConnection != null) 164 { 165 try 166 { 167 queueConnection.close(); 168 } 169 catch (Exception e) 170 { 171 log.debug("failed", e); 172 } 173 } 174 } 175 return ret; 176 } 177 178 } 179 | Popular Tags |