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 javax.jms.DeliveryMode; 15 import javax.jms.JMSException; 16 import javax.jms.Message; 17 import javax.jms.MessageProducer; 18 19 /*** 20 * <p> 21 * This class implements the JMS message producer. A client uses a 22 * MessageProducer object to send messages to a destination. A MessageProducer 23 * object is created by passing a Destination object to a message-producer 24 * creation method supplied by a session. 25 * </p> 26 * 27 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a> 28 * @version Revision: 1.1 Date: 2003-05-20 21:35:56 29 */ 30 31 public abstract class MessageProducerImpl implements MessageProducer { 32 33 protected long defaultTimeToLive = Message.DEFAULT_TIME_TO_LIVE; 34 protected int defaultDeliveryMode = Message.DEFAULT_DELIVERY_MODE; 35 protected int defaultPriority = Message.DEFAULT_PRIORITY; 36 protected boolean disableMessageID = false; 37 protected boolean disableMessageTimestamp = false; 38 private boolean closed = false; 39 private SessionImpl session; 40 41 public MessageProducerImpl(SessionImpl session) { 42 this.session = session; 43 } 44 45 /*** 46 * Gets the producer's default delivery mode. 47 * 48 * @return the message delivery mode for this message producer 49 * @throws JMSException 50 */ 51 public int getDeliveryMode() throws JMSException { 52 if (isClosed()) { 53 throw new javax.jms.IllegalStateException( 54 "The message producer is closed."); 55 } 56 return this.defaultDeliveryMode; 57 } 58 59 /*** 60 * Sets the producer's default delivery mode. 61 * 62 * <p> 63 * Delivery mode is set to PERSISTENT by default. 64 * 65 * @param deliveryMode 66 * the message delivery mode for this message producer; legal 67 * values are DeliveryMode.NON_PERSISTENT and 68 * DeliveryMode.PERSISTENT 69 * @throws JMSException 70 */ 71 public void setDeliveryMode(int deliveryMode) throws JMSException { 72 if (isClosed()) { 73 throw new javax.jms.IllegalStateException( 74 "The message producer is closed."); 75 } 76 if (defaultDeliveryMode != DeliveryMode.NON_PERSISTENT 77 && defaultDeliveryMode != DeliveryMode.PERSISTENT) { 78 throw new JMSException("Illegal DeliveryMode value"); 79 } else { 80 this.defaultDeliveryMode = deliveryMode; 81 } 82 } 83 84 /*** 85 * Gets an indication of whether message IDs are disabled. 86 * 87 * @return an indication of whether message IDs are disabled 88 * @throws JMSException 89 */ 90 public boolean getDisableMessageID() throws JMSException { 91 if (isClosed()) { 92 throw new javax.jms.IllegalStateException( 93 "The message producer is closed."); 94 } 95 return this.disableMessageID; 96 } 97 98 /*** 99 * Sets whether message IDs are disabled. 100 * 101 * @param value 102 * indicates if message IDs are disabled 103 * @throws JMSException 104 */ 105 public void setDisableMessageID(boolean value) throws JMSException { 106 if (isClosed()) { 107 throw new javax.jms.IllegalStateException( 108 "The message producer is closed."); 109 } 110 this.disableMessageID = value; 111 } 112 113 /*** 114 * Gets an indication of whether message timestamps are disabled. 115 * 116 * @return an indication of whether message timestamps are disabled 117 * @throws JMSException 118 */ 119 public boolean getDisableMessageTimestamp() throws JMSException { 120 if (isClosed()) { 121 throw new javax.jms.IllegalStateException( 122 "The message producer is closed."); 123 } 124 return this.disableMessageTimestamp; 125 } 126 127 /*** 128 * Sets whether message timestamps are disabled. 129 * <P> 130 * Message timestamps are enabled by default. 131 * 132 * @param value 133 * indicates if message timestamps are disabled 134 * @throws JMSException 135 */ 136 public void setDisableMessageTimestamp(boolean value) throws JMSException { 137 if (isClosed()) { 138 throw new javax.jms.IllegalStateException( 139 "The message producer is closed."); 140 } 141 this.disableMessageTimestamp = value; 142 } 143 144 /*** 145 * Gets the producer's default priority. 146 * 147 * @return the message priority for this message producer 148 * @throws JMSException 149 */ 150 public int getPriority() throws JMSException { 151 if (isClosed()) { 152 throw new javax.jms.IllegalStateException( 153 "The message producer is closed."); 154 } 155 return this.defaultPriority; 156 } 157 158 /*** 159 * Sets the default length of time in milliseconds from its dispatch time 160 * that a produced message should be retained by the message system. 161 * 162 * <p> 163 * Time to live is set to zero by default. 164 * 165 * @param priority 166 * @throws JMSException 167 */ 168 public void setPriority(int priority) throws JMSException { 169 if (isClosed()) { 170 throw new javax.jms.IllegalStateException( 171 "The message producer is closed."); 172 } 173 if (defaultPriority < 0 || defaultPriority > 9) { 174 throw new JMSException("Illegal priority value"); 175 } else { 176 this.defaultPriority = priority; 177 } 178 } 179 180 /*** 181 * Gets the default length of time in milliseconds from its dispatch time 182 * that a produced message should be retained by the message system. 183 * 184 * @return the message time to live in milliseconds; zero is unlimited 185 * @throws JMSException 186 */ 187 public long getTimeToLive() throws JMSException { 188 if (isClosed()) { 189 throw new javax.jms.IllegalStateException( 190 "The message producer is closed."); 191 } 192 return this.defaultTimeToLive; 193 } 194 195 /*** 196 * Sets the default length of time in milliseconds from its dispatch time 197 * that a produced message should be retained by the message system. 198 * 199 * <P> 200 * Time to live is set to zero by default. 201 * 202 * @param TimeToLive 203 * @throws JMSException 204 */ 205 public void setTimeToLive(long TimeToLive) throws JMSException { 206 if (isClosed()) { 207 throw new javax.jms.IllegalStateException( 208 "The message producer is closed."); 209 } 210 if (defaultTimeToLive < 0) { 211 throw new JMSException("Illegal timeToLive value"); 212 } else { 213 this.defaultTimeToLive = TimeToLive; 214 } 215 } 216 217 /*** 218 * Send message to server. 219 * 220 * @param message 221 * @throws JMSException 222 */ 223 protected void sendMessage(Message message) throws JMSException { 224 session.sendMessage(message); 225 } 226 227 /*** 228 * Closes the message producer. 229 * 230 * <p> 231 * Since a provider may allocate some resources on behalf of a 232 * MessageProducer outside the Java virtual machine, clients should close 233 * them when they are not needed. Relying on garbage collection to 234 * eventually reclaim these resources may not be timely enough. 235 * 236 * @throws JMSException 237 */ 238 public void close() throws JMSException { 239 if (isClosed()) { 240 return; 241 } 242 this.session = null; 243 } 244 245 /*** 246 * Indicates the status of this message producer. 247 * 248 * @return true if the message producer is closed 249 */ 250 public boolean isClosed() { 251 return closed; 252 } 253 }

This page was automatically generated by Maven