View Javadoc
1 // $Header: /cvsroot/daoexamples/daoexamples/src/java/daoexamples/moviedemo/MessagePublisher.java,v 1.3 2003/08/12 04:41:02 sullis Exp $ 2 3 /* 4 * 5 * 6 * 7 * 8 */ 9 10 package daoexamples.moviedemo; 11 12 import daoexamples.exception.*; 13 import javax.jms.*; 14 import javax.naming.*; 15 import javax.rmi.PortableRemoteObject; 16 import org.apache.commons.logging.*; 17 18 /*** 19 * 20 * publishes messages to a JMS Topic 21 * 22 * You can learn more about the Java Message Service at 23 * <a href="http://java.sun.com/products/jms/">http://java.sun.com/products/jms/</a> 24 * 25 * This class assumes that transactions are externally 26 * demaracated using JTA 27 * 28 * Instances of this class are not thread-safe. 29 * 30 * @author Sean C. Sullivan 31 * 32 */ 33 class MessagePublisher 34 { 35 static private final Log log = LogFactory.getLog(MessagePublisher.class); 36 static private final String TOPIC_CF_JNDI_NAME = "java:comp/env/jms/MovieTCF"; 37 static private final String TOPIC_JNDI_NAME = "java:comp/env/jms/MovieTopic"; 38 39 private TopicConnection tconn; 40 private TopicSession tsess; 41 private TopicPublisher publisher; 42 private Topic top; 43 private boolean bIsClosed = false; 44 45 /*** 46 * 47 * Before calling this constructor, you must have 48 * a JTA UserTransaction 49 * 50 * @throws daoexamples.exception.DAORuntimeException 51 * 52 */ 53 public MessagePublisher() 54 { 55 try 56 { 57 tconn = getTopicConnection(); 58 tsess = tconn.createTopicSession( 59 true /* true == session is transacted */, 60 TopicSession.AUTO_ACKNOWLEDGE); 61 top = getTopic(); 62 publisher = tsess.createPublisher(top); 63 } 64 catch (JMSException ex) 65 { 66 throw new DAORuntimeException(ex); 67 } 68 69 } 70 71 /*** 72 * 73 * @throws daoexamples.exception.DAORuntimeException 74 * 75 */ 76 static private InitialContext getInitialContext() 77 { 78 79 InitialContext ctx = null; 80 81 try 82 { 83 ctx = new InitialContext(); 84 } 85 catch (NamingException ex) 86 { 87 throw new DAORuntimeException(ex); 88 } 89 90 return ctx; 91 92 } 93 94 /*** 95 * 96 * @throws daoexamples.exception.DAORuntimeException 97 * 98 */ 99 static private Topic getTopic() 100 { 101 InitialContext ctx = getInitialContext(); 102 103 Topic result = null; 104 105 Object topicObject = null; 106 107 try 108 { 109 topicObject = ctx.lookup(TOPIC_JNDI_NAME); 110 result = (Topic) PortableRemoteObject.narrow( 111 topicObject, 112 Topic.class); 113 } 114 catch (NamingException ex) 115 { 116 throw new DAORuntimeException(ex); 117 } 118 119 return result; 120 } 121 122 /*** 123 * 124 * @throws daoexamples.exception.DAORuntimeException 125 * 126 */ 127 static private TopicConnection getTopicConnection() 128 { 129 TopicConnectionFactory tcf = getTopicConnectionFactory(); 130 131 TopicConnection tconn = null; 132 133 try 134 { 135 tconn = tcf.createTopicConnection(); 136 } 137 catch (JMSException ex) 138 { 139 throw new DAORuntimeException(ex); 140 } 141 142 return tconn; 143 144 } 145 146 /*** 147 * 148 * @throws daoexamples.exception.DAORuntimeException 149 * 150 */ 151 static private TopicConnectionFactory getTopicConnectionFactory() 152 { 153 InitialContext ctx = getInitialContext(); 154 155 TopicConnectionFactory tcf = null; 156 157 Object tcfObject = null; 158 159 try 160 { 161 tcfObject = ctx.lookup(TOPIC_CF_JNDI_NAME); 162 tcf = (TopicConnectionFactory) PortableRemoteObject.narrow( 163 tcfObject, 164 TopicConnectionFactory.class); 165 } 166 catch (NamingException ex) 167 { 168 throw new DAORuntimeException(ex); 169 } 170 171 return tcf; 172 } 173 174 175 /*** 176 * 177 * @param strText must be non-null 178 * 179 * @throws daoexamples.exception.DAORuntimeException 180 * 181 */ 182 public void publishTextMessage(final String strText) 183 { 184 if (isClosed()) 185 { 186 throw new java.lang.IllegalStateException("MessagePublisher is closed"); 187 } 188 189 if (null == strText) 190 { 191 throw new NullPointerException("strText parameter"); 192 } 193 194 try 195 { 196 TextMessage textMsg = tsess.createTextMessage(strText); 197 publisher.publish(textMsg); 198 } 199 catch (JMSException ex) 200 { 201 throw new DAORuntimeException(ex); 202 } 203 204 } 205 206 /*** 207 * 208 * @return true if this object is closed 209 * 210 * @see #close() 211 * 212 */ 213 public boolean isClosed() 214 { 215 return bIsClosed; 216 } 217 218 /*** 219 * 220 * @see #isClosed() 221 * 222 */ 223 public void close() 224 { 225 log.info("close() called"); 226 227 if ( ! isClosed() ) 228 { 229 bIsClosed = true; 230 231 top = null; 232 233 if (publisher != null) 234 { 235 try 236 { 237 publisher.close(); 238 } 239 catch (JMSException ex) 240 { 241 log.error("exception while closing a TopicPublisher", 242 ex); 243 } 244 finally 245 { 246 publisher = null; 247 } 248 } 249 250 if (tsess != null) 251 { 252 try 253 { 254 tsess.close(); 255 } 256 catch (JMSException ex) 257 { 258 log.error("exception while closing a TopicSession", 259 ex); 260 } 261 finally 262 { 263 tsess = null; 264 } 265 } 266 267 if (tconn != null) 268 { 269 try 270 { 271 tconn.close(); 272 } 273 catch (JMSException ex) 274 { 275 log.error("exception while closing a TopicConnection", 276 ex); 277 } 278 finally 279 { 280 tconn = null; 281 } 282 } 283 } 284 285 } 286 }

This page was automatically generated by Maven