View Javadoc
1 /* 2 * @(#)MessageProducerImpl.java 3 * 4 * JFoxMQ the open source JMS MOM. 5 * 6 * Corpyright 2002-2003 Huihoo Power, Inc. All Rights Reserved. This software 7 * is licensed under LGPL license. 8 * 9 * For more information, please visit: http://www.huihoo.org 10 */ 11 12 package org.huihoo.jfox.ms.jms.base; 13 14 import java.io.Serializable; 15 import java.util.HashSet; 16 17 import javax.jms.BytesMessage; 18 import javax.jms.IllegalStateException; 19 import javax.jms.JMSException; 20 import javax.jms.MapMessage; 21 import javax.jms.Message; 22 import javax.jms.MessageListener; 23 import javax.jms.ObjectMessage; 24 import javax.jms.Session; 25 import javax.jms.StreamMessage; 26 import javax.jms.TextMessage; 27 28 import org.huihoo.jfox.ms.jms.message.BytesMessageImpl; 29 import org.huihoo.jfox.ms.jms.message.MapMessageImpl; 30 import org.huihoo.jfox.ms.jms.message.MessageImpl; 31 import org.huihoo.jfox.ms.jms.message.ObjectMessageImpl; 32 import org.huihoo.jfox.ms.jms.message.StreamMessageImpl; 33 import org.huihoo.jfox.ms.jms.message.TextMessageImpl; 34 35 /*** 36 * <p> 37 * Description: This class implements the JMS session. A Session object is a 38 * single-threaded context for producing and consuming messages. Although it 39 * may allocate provider resources outside the Java virtual machine (JVM), it 40 * is considered a lightweight JMS object. 41 * </p> 42 * 43 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a> 44 * @version Revision: 1.1 Date: 2003-06-14 21:16:07 45 */ 46 47 public abstract class SessionImpl implements Session { 48 49 // The state of this session. 50 private boolean closed = false; 51 52 // The statu of transactionality. 53 private boolean transacted = false; 54 55 // MessageConsumers created by this session 56 private HashSet consumers = new HashSet(); 57 58 // The acknowledgement mode of this session. 59 private int acknowledgeMode = Session.AUTO_ACKNOWLEDGE; 60 61 private ConnectionImpl connection; 62 63 // The message listener of the session. 64 private MessageListener messageListener; 65 66 /*** 67 * Constructe a sessin with params. 68 * 69 * @param connection 70 * @param transacted 71 * @param acknowledgeMode 72 */ 73 public SessionImpl( 74 ConnectionImpl connection, 75 boolean transacted, 76 int acknowledgeMode) { 77 this.acknowledgeMode = acknowledgeMode; 78 this.connection = connection; 79 this.transacted = transacted; 80 } 81 82 /*** 83 * Creates a BytesMessage object. A BytesMessage object is used to send a 84 * message containing a stream of uninterpreted bytes. 85 * 86 * @return BytesMessage 87 * @throws JMSException 88 */ 89 public BytesMessage createBytesMessage() throws JMSException { 90 if (isClosed()) { 91 throw new IllegalStateException("The session is closed."); 92 } 93 return new BytesMessageImpl(); 94 } 95 96 /*** 97 * Creates a MapMessage object. A MapMessage object is used to send a 98 * self-defining set of name-value pairs, where names are String objects 99 * and values are primitive values in the Java programming language. 100 * 101 * @return MapMessage 102 * @throws JMSException 103 */ 104 public MapMessage createMapMessage() throws JMSException { 105 if (isClosed()) { 106 throw new IllegalStateException("The session is closed."); 107 } 108 return new MapMessageImpl(); 109 } 110 111 /*** 112 * Creates a Message object. The Message interface is the root interface of 113 * all JMS messages. Message object holds all the standard message header 114 * information. It can be sent when a message containing only header 115 * information is sufficient. 116 * 117 * @return Message 118 * @throws JMSException 119 */ 120 public Message createMessage() throws JMSException { 121 if (isClosed()) { 122 throw new IllegalStateException("The session is closed."); 123 } 124 return new MessageImpl(); 125 } 126 127 /*** 128 * Creates an ObjectMessage object. An ObjectMessage object is used to send 129 * a message that contains a serializable Java object. 130 * 131 * @return ObjectMessage 132 * @throws JMSException 133 */ 134 public ObjectMessage createObjectMessage() throws JMSException { 135 if (isClosed()) { 136 throw new IllegalStateException("The session is closed."); 137 } 138 return new ObjectMessageImpl(); 139 } 140 141 /*** 142 * Creates an initialized ObjectMessage object. An ObjectMessage object is 143 * used to send a message that contains a serializable Java object. 144 * 145 * @param object 146 * the object to use to initialize this message 147 * @return ObjectMessage 148 * @throws JMSException 149 */ 150 public ObjectMessage createObjectMessage(Serializable object) 151 throws JMSException { 152 if (isClosed()) { 153 throw new IllegalStateException("The session is closed."); 154 } 155 ObjectMessageImpl objectMessage = new ObjectMessageImpl(); 156 objectMessage.setObject(object); 157 return objectMessage; 158 } 159 160 /*** 161 * Creates a StreamMessage object. A StreamMessage object is used to send a 162 * self-defining stream of primitive values in the Java programming 163 * language. 164 * 165 * @return StreamMessage 166 * @throws JMSException 167 */ 168 public StreamMessage createStreamMessage() throws JMSException { 169 if (isClosed()) { 170 throw new IllegalStateException("The session is closed."); 171 } 172 return new StreamMessageImpl(); 173 } 174 175 /*** 176 * Creates a TextMessage object. A TextMessage object is used to send a 177 * message containing a String object. 178 * 179 * @return TextMessage 180 * @throws JMSException 181 */ 182 public TextMessage createTextMessage() throws JMSException { 183 if (isClosed()) { 184 throw new IllegalStateException("The session is closed."); 185 } 186 return new TextMessageImpl(); 187 } 188 189 /*** 190 * Creates an initialized TextMessage object. A TextMessage object is used 191 * to send message containing a String. 192 * 193 * @param text 194 * the string used to initialize this message 195 * @return TextMessage 196 * @throws JMSException 197 */ 198 public TextMessage createTextMessage(String text) throws JMSException { 199 if (isClosed()) { 200 throw new IllegalStateException("The session is closed."); 201 } 202 TextMessageImpl textMessage = new TextMessageImpl(); 203 textMessage.setText(text); 204 return textMessage; 205 } 206 207 /*** 208 * Indicates whether the session is in transacted mode. 209 * 210 * @return true if the session is in transacted mode 211 * @throws JMSException 212 */ 213 public boolean getTransacted() throws JMSException { 214 return transacted; 215 } 216 217 /*** 218 * Commits all messages done in this transaction and releases any locks 219 * currently held. 220 * 221 * @throws JMSException 222 */ 223 public void commit() throws JMSException { 224 } 225 226 /*** 227 * Rolls back any messages done in this transaction and releases any locks 228 * currently held. 229 * 230 * @throws JMSException 231 */ 232 public void rollback() throws JMSException { 233 } 234 235 /*** 236 * Closes the session. 237 * 238 * <p> 239 * Since a provider may allocate some resources on behalf of a session 240 * outside the JVM, clients should close the resources when they are not 241 * needed. Relying on garbage collection to eventually reclaim these 242 * resources may not be timely enough. 243 * 244 * <p> 245 * There is no need to close the producers and consumers of a closed 246 * session. 247 * 248 * <p> 249 * This call will block until a receive call or message listener in 250 * progress has completed. A blocked message consumer receive call returns 251 * null when this session is closed. 252 * 253 * <p> 254 * Closing a transacted session must roll back the transaction in progress. 255 * 256 * <p> 257 * This method is the only Session method that can be called concurrently. 258 * 259 * <p> 260 * Invoking any other Session method on a closed session must throw a 261 * JMSException.IllegalStateException. Closing a closed session must not 262 * throw an exception. 263 * 264 * @throws JMSException 265 */ 266 public void close() throws JMSException { 267 } 268 269 /*** 270 * Stops message delivery in this session, and restarts message delivery 271 * with the oldest unacknowledged message. 272 * 273 * <p> 274 * All consumers deliver messages in a serial order. Acknowledging a 275 * received message automatically acknowledges all messages that have been 276 * delivered to the client. 277 * 278 * <p> 279 * Restarting a session causes it to take the following actions: 280 * 281 * <ul> 282 * <li>Stop message delivery 283 * <li>Mark all messages that might have been delivered but not 284 * acknowledged as "redelivered" 285 * <li>Restart the delivery sequence including all unacknowledged messages 286 * that had been previously delivered. Redelivered messages do not have to 287 * be delivered in exactly their original delivery order. 288 * </ul> 289 * 290 * @throws JMSException 291 */ 292 public void recover() throws JMSException { 293 } 294 295 /*** 296 * Returns the session's distinguished message listener (optional). 297 * 298 * @return the message listener associated with this session 299 * @throws JMSException 300 * @see javax.jms.ServerSessionPool 301 * @see javax.jms.ServerSession 302 */ 303 public MessageListener getMessageListener() throws JMSException { 304 if (isClosed()) { 305 throw new IllegalStateException("The session is closed."); 306 } 307 return this.messageListener; 308 } 309 310 /*** 311 * Sets the session's distinguished message listener (optional). 312 * 313 * <P> 314 * When the distinguished message listener is set, no other form of message 315 * receipt in the session can be used; however, all forms of sending 316 * messages are still supported. 317 * 318 * <P> 319 * This is an expert facility not used by regular JMS clients. 320 * 321 * @param listener 322 * the message listener to associate with this session 323 * @throws JMSException 324 * @see javax.jms.ServerSessionPool 325 * @see javax.jms.ServerSession 326 */ 327 public void setMessageListener(MessageListener listener) 328 throws JMSException { 329 this.messageListener = listener; 330 } 331 332 /*** 333 * Optional operation, intended to be used only by Application Servers, not 334 * by ordinary JMS clients. 335 * 336 * @see javax.jms.ServerSession 337 */ 338 public void run() { 339 } 340 341 /*** 342 * Send message to server. 343 * 344 * @param message 345 * @throws JMSException 346 */ 347 public void sendMessage(Message message) throws JMSException { 348 if (isClosed()) { 349 throw new IllegalStateException("The session is closed."); 350 } 351 352 if (message == null) { 353 throw new JMSException("Cannot send null Message"); 354 } 355 356 if (transacted) { 357 throw new JMSException("Does not support transaction"); 358 } else { 359 // TODO send message to server 360 } 361 } 362 363 /*** 364 * Indicates whether the session is in transacted mode. 365 * 366 * @return true if the session is in transacted mode 367 */ 368 public boolean isTransacted() { 369 return transacted; 370 } 371 372 /*** 373 * Retrieve the acknowledgement mode of this session. 374 * 375 * @return acknowledgeMode 376 */ 377 public int getAcknowledgeMode() { 378 return acknowledgeMode; 379 } 380 381 /*** 382 * Retrieve connection. 383 * 384 * @return ConnectionImpl 385 */ 386 public ConnectionImpl getConnection() { 387 return connection; 388 } 389 390 /*** 391 * Indicates the status of this session. 392 * 393 * @return true if the session is closed 394 */ 395 public boolean isClosed() { 396 return closed; 397 } 398 399 /*** 400 * Add the specified receiver to the list of managed receivers. 401 * 402 * @param messageConsumer 403 * @throws JMSException 404 */ 405 protected void addConsumer(MessageConsumerImpl messageConsumer) 406 throws JMSException { 407 408 if (isClosed()) { 409 throw new IllegalStateException("The session is closed."); 410 } 411 synchronized (consumers) { 412 consumers.add(messageConsumer); 413 } 414 // TODO add comuser to the server side 415 416 } 417 418 /*** 419 * Starts the asynchronous deliveries in the session. 420 * 421 * @exception IllegalStateException 422 * If the session is closed. 423 */ 424 public void start() throws JMSException { 425 if (isClosed()) { 426 throw new IllegalStateException("The session is closed."); 427 } 428 // TODO stop the session 429 } 430 431 /*** 432 * Starts the asynchronous deliveries in the session. 433 * 434 * @exception IllegalStateException 435 * If the session is closed. 436 */ 437 public void stop() throws JMSException { 438 if (isClosed()) { 439 throw new IllegalStateException("The session is closed."); 440 } 441 // TODO stop the session 442 } 443 444 }

This page was automatically generated by Maven