1 22 package org.jboss.test.jmsra.bean; 23 24 import java.rmi.RemoteException ; 25 import java.util.*; 26 import javax.ejb.SessionBean ; 27 import javax.ejb.SessionContext ; 28 import javax.ejb.EJBException ; 29 import javax.naming.*; 30 import javax.jms.*; 31 32 33 public class TopicPublisherBean implements SessionBean { 34 35 static org.jboss.logging.Logger log = 36 org.jboss.logging.Logger.getLogger(TopicPublisherBean.class); 37 38 private static final String CONNECTION_JNDI = "java:comp/env/jms/MyTopicConnection"; 39 private static final String TOPIC_JNDI = "java:comp/env/jms/TopicName"; 40 41 private static final String BEAN_JNDI = "java:comp/env/ejb/PublisherCMP"; 42 43 private SessionContext ctx = null; 44 private Topic topic = null; 45 private TopicConnection topicConnection = null; 46 public TopicPublisherBean() { 47 } 48 49 public void setSessionContext(SessionContext ctx) { 50 this.ctx = ctx; 51 } 52 53 public void ejbCreate() { 54 try { 55 Context context = new InitialContext(); 56 topic = (Topic)context.lookup(TOPIC_JNDI); 57 58 TopicConnectionFactory factory = (TopicConnectionFactory)context.lookup(CONNECTION_JNDI); 59 topicConnection = factory.createTopicConnection(); 60 61 } catch (Exception ex) { 62 log.debug("failed", ex); 64 throw new EJBException (ex.toString()); 65 } 66 } 67 68 71 public void simple(int messageNr) { 72 sendMessage(messageNr); 73 } 74 78 public void simpleFail(int messageNr) { 79 sendMessage(messageNr); 80 log.debug("DEBUG: Setting rollbackOnly"); 82 ctx.setRollbackOnly(); 83 log.debug("DEBUG rollback set: " + ctx.getRollbackOnly()); 84 85 } 86 87 public void beanOk(int messageNr) { 88 sendMessage(messageNr); 90 PublisherCMPHome h = null; 91 try { 92 h = (PublisherCMPHome) new InitialContext().lookup(BEAN_JNDI); 94 PublisherCMP b = h.create(new Integer (messageNr)); 95 b.ok(messageNr); 96 }catch(Exception ex) { 97 throw new EJBException ("OPS exception in ok: " + ex); 98 } finally { 99 try { 100 h.remove(new Integer (messageNr)); 101 }catch(Exception e) { 102 log.debug("failed", e); 103 } 104 } 105 106 } 107 108 public void beanError(int messageNr) { 109 sendMessage(messageNr); 111 PublisherCMPHome h = null; 112 try { 113 h = (PublisherCMPHome) new InitialContext().lookup(BEAN_JNDI); 115 PublisherCMP b = h.create(new Integer (messageNr)); 116 b.error(messageNr); 117 } catch(Exception ex) { 118 throw new EJBException ("Exception in erro: " + ex); 119 }finally { 120 try { 121 h.remove(new Integer (messageNr)); 122 }catch(Exception ex) { 123 log.debug("failed", ex); 124 } 125 } 126 } 127 128 public void ejbRemove() throws RemoteException { 129 if(topicConnection != null) { 130 try { 131 topicConnection.close(); 132 } catch (Exception e) { 133 log.debug("failed", e); 134 } 135 } 136 } 137 138 public void ejbActivate() {} 139 public void ejbPassivate() {} 140 141 private void sendMessage(int messageNr) { 142 TopicSession topicSession = null; 143 try { 144 TopicPublisher topicPublisher = null; 145 TextMessage message = null; 146 147 topicSession = 148 topicConnection.createTopicSession(true, Session.AUTO_ACKNOWLEDGE); 149 topicPublisher = topicSession.createPublisher(topic); 150 151 message = topicSession.createTextMessage(); 152 message.setText(String.valueOf(messageNr)); 153 message.setIntProperty(Publisher.JMS_MESSAGE_NR, messageNr); 154 topicPublisher.publish(message); 155 156 157 } catch (JMSException ex) { 158 159 log.debug("failed", ex); 160 ctx.setRollbackOnly(); 161 throw new EJBException (ex.toString()); 162 } finally { 163 if (topicSession != null) { 164 try { 165 topicSession.close(); 166 } catch (Exception e) { 167 log.debug("failed", e); 168 } 169 } 170 } 171 } 172 173 } 174 | Popular Tags |