1 22 package org.jboss.test.jmsra.bean; 23 24 import java.rmi.RemoteException ; 25 import javax.ejb.EJBException ; 26 import javax.ejb.SessionBean ; 27 import javax.ejb.SessionContext ; 28 import javax.jms.JMSException ; 29 import javax.jms.QueueConnection ; 30 import javax.jms.QueueConnectionFactory ; 31 import javax.jms.QueueSender ; 32 import javax.jms.QueueSession ; 33 import javax.jms.Session ; 34 import javax.jms.TextMessage ; 35 import javax.jms.Queue ; 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 39 import org.jboss.logging.Logger; 40 41 51 public class PublisherBean 52 implements SessionBean 53 { 54 private static final String CONNECTION_JNDI = 55 "java:comp/env/jms/MyQueueConnection"; 56 57 private static final String QUEUE_JNDI = 58 "java:comp/env/jms/QueueName"; 59 60 private static final String BEAN_JNDI = 61 "java:comp/env/ejb/PublisherCMP"; 62 63 private final Logger log = Logger.getLogger(this.getClass()); 64 65 private SessionContext ctx; private Queue queue; private QueueConnection queueConnection; 69 public PublisherBean() { 70 } 72 73 public void setSessionContext(final SessionContext ctx) { 74 this.ctx = ctx; 75 } 76 77 public void ejbCreate() { 78 try { 79 Context context = new InitialContext (); 80 queue = (Queue )context.lookup(QUEUE_JNDI); 81 82 QueueConnectionFactory factory = 83 (QueueConnectionFactory )context.lookup(CONNECTION_JNDI); 84 queueConnection = factory.createQueueConnection(); 85 } 86 catch (Exception e) { 87 log.error("failed to create bean", e); 89 throw new EJBException (e); 90 } 91 } 92 93 96 public void simple(int messageNr) { 97 log.info("sending message"); 98 sendMessage(messageNr); 99 log.info("sent"); 100 } 101 102 106 public void simpleFail(int messageNr) { 107 log.info("sending message"); 108 sendMessage(messageNr); 109 log.info("sent"); 110 111 log.info("Setting rollbackOnly"); 113 ctx.setRollbackOnly(); 114 log.info("rollback set: " + ctx.getRollbackOnly()); 115 } 116 117 public void beanOk(int messageNr) { 118 log.info("sending message"); 119 sendMessage(messageNr); 121 log.info("sent"); 122 123 PublisherCMPHome h = null; 124 try { 125 h = (PublisherCMPHome) new InitialContext ().lookup(BEAN_JNDI); 127 PublisherCMP b = h.create(new Integer (messageNr)); 128 log.info("calling bean"); 129 b.ok(messageNr); 130 log.info("called bean"); 131 } 132 catch (Exception e) { 133 log.error("failed to contact 3rdparty bean", e); 134 throw new EJBException (e); 135 } 136 finally { 137 try { 138 h.remove(new Integer (messageNr)); 139 } 140 catch (Exception e) { 141 log.error("failed to remove 3rdparty bean", e); 142 } 143 finally { 144 log.info("done"); 145 } 146 } 147 } 148 149 public void beanError(int messageNr) { 150 log.info("sending message"); 151 sendMessage(messageNr); 153 log.info("sent"); 154 155 PublisherCMPHome h = null; 156 try { 157 h = (PublisherCMPHome) new InitialContext ().lookup(BEAN_JNDI); 159 PublisherCMP b = h.create(new Integer (messageNr)); 160 log.info("calling bean"); 161 b.error(messageNr); 162 log.info("bean called (should never get here)"); 163 } 164 catch (Exception e) { 165 log.info("caught exception (as expected)"); 166 throw new EJBException ("Exception in erro: " + e); 167 } 168 finally { 169 try { 170 h.remove(new Integer (messageNr)); 171 } 172 catch (Exception e) { 173 log.error("failed to remove 3rdparty bean", e); 174 } 175 finally { 176 log.info("done"); 177 } 178 } 179 } 180 181 public void ejbRemove() throws RemoteException { 182 if (queueConnection != null) { 183 try { 184 queueConnection.close(); 185 } 186 catch (Exception e) { 187 log.error("failed to close connection", e); 188 } 189 } 190 } 191 192 public void ejbActivate() {} 193 public void ejbPassivate() {} 194 195 private void sendMessage(int messageNr) { 196 log.info("sending message wtih nr: " + messageNr); 197 QueueSession queueSession = null; 198 try { 199 QueueSender queueSender = null; 200 TextMessage message = null; 201 queueSession = 202 queueConnection.createQueueSession(true, 203 Session.AUTO_ACKNOWLEDGE); 204 queueSender = queueSession.createSender(queue); 205 206 message = queueSession.createTextMessage(); 207 message.setText(String.valueOf(messageNr)); 208 message.setIntProperty(Publisher.JMS_MESSAGE_NR, messageNr); 209 queueSender.send(message); 210 log.info("sent message with nr = " + messageNr); 211 } 212 catch (JMSException e) { 213 log.debug("failed to send", e); 214 ctx.setRollbackOnly(); 215 throw new EJBException (e); 216 } 217 finally { 218 if (queueSession != null) { 219 try { 220 queueSession.close(); 221 } 222 catch (Exception e) { 223 log.error("failed to close session", e); 224 } 225 finally { 226 log.info("done sending message"); 227 } 228 } 229 } 230 } 231 232 } 233 | Popular Tags |