1 45 package org.exolab.jms.client; 46 47 import java.util.Date ; 48 49 import javax.jms.DeliveryMode ; 50 import javax.jms.Destination ; 51 import javax.jms.InvalidDestinationException ; 52 import javax.jms.JMSException ; 53 import javax.jms.Message ; 54 import javax.jms.MessageFormatException ; 55 import javax.jms.MessageProducer ; 56 import javax.jms.Session ; 57 58 import org.exolab.jms.message.MessageId; 59 60 61 69 class JmsMessageProducer implements MessageProducer { 70 71 75 private final Destination _destination; 76 77 80 private int _defaultPriority = Message.DEFAULT_PRIORITY; 81 82 85 private long _defaultTtl = 0; 86 87 90 private int _deliveryMode = DeliveryMode.PERSISTENT; 91 92 96 private boolean _disableTimestamp = false; 97 98 102 private boolean _disableMessageId = false; 103 104 107 private JmsSession _session = null; 108 109 110 117 public JmsMessageProducer(JmsSession session, Destination destination) { 118 if (session == null) { 119 throw new IllegalArgumentException ("Argument 'session' is null"); 120 } 121 _session = session; 122 _destination = destination; 123 } 124 125 130 public Destination getDestination() { 131 return _destination; 132 } 133 134 139 public void setDisableMessageID(boolean value) { 140 _disableMessageId = value; 141 } 142 143 148 public boolean getDisableMessageID() { 149 return _disableMessageId; 150 } 151 152 157 public void setDisableMessageTimestamp(boolean value) { 158 _disableTimestamp = value; 159 } 160 161 166 public boolean getDisableMessageTimestamp() { 167 return _disableTimestamp; 168 } 169 170 177 public void setDeliveryMode(int deliveryMode) { 178 _deliveryMode = deliveryMode; 179 } 180 181 186 public int getDeliveryMode() { 187 return _deliveryMode; 188 } 189 190 195 public void setPriority(int priority) { 196 _defaultPriority = priority; 197 } 198 199 204 public int getPriority() { 205 return _defaultPriority; 206 } 207 208 214 public void setTimeToLive(long timeToLive) { 215 _defaultTtl = timeToLive; 216 } 217 218 224 public long getTimeToLive() { 225 return _defaultTtl; 226 } 227 228 246 public void send(Message message) throws JMSException { 247 send(getDestination(), message, getDeliveryMode(), getPriority(), 248 getTimeToLive()); 249 } 250 251 272 public void send(Message message, int deliveryMode, int priority, 273 long timeToLive) throws JMSException { 274 send(getDestination(), message, deliveryMode, priority, timeToLive); 275 } 276 277 301 public void send(Destination destination, Message message) 302 throws JMSException { 303 send(destination, message, getDeliveryMode(), getPriority(), 304 getTimeToLive()); 305 } 306 307 327 public void send(Destination destination, Message message, 328 int deliveryMode, int priority, long timeToLive) 329 throws JMSException { 330 331 if (!(destination instanceof JmsDestination)) { 332 throw new InvalidDestinationException ( 334 "Invalid destination: " + destination); 335 } 336 if (message == null) { 337 throw new MessageFormatException ("Null message"); 338 } 339 340 message.setJMSMessageID(MessageId.create()); 341 message.setJMSDestination(destination); 342 message.setJMSTimestamp((new Date ()).getTime()); 343 message.setJMSPriority(priority); 344 345 if (timeToLive > 0) { 346 message.setJMSExpiration(System.currentTimeMillis() + timeToLive); 347 } else { 348 message.setJMSExpiration(0); 349 } 350 351 if (destination instanceof JmsTemporaryDestination) { 354 message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT); 355 } else { 356 message.setJMSDeliveryMode(deliveryMode); 357 } 358 359 _session.sendMessage(message); 360 } 361 362 367 public synchronized void close() throws JMSException { 368 if (_session != null) { 369 _session.removeProducer(this); 370 } 371 _session = null; 372 } 373 374 } 375 | Popular Tags |