1 package net.walend.somnifugi; 2 3 import javax.jms.MessageProducer ; 4 import javax.jms.JMSException ; 5 import javax.jms.DeliveryMode ; 6 import javax.jms.Message ; 7 import javax.jms.Destination ; 8 9 14 15 public abstract class SomniMessageProducer 16 implements MessageProducer 17 { 18 private boolean useMessageIDs = true; 19 private boolean useTimestamps = true; 20 private int defaultPriority = 0; 21 private long timeToLive = 0; 22 private SomniDestination destination; 23 private String connectionClientID; 24 private String name; 25 26 private boolean closed = false; 27 28 protected final Object guard = new Object (); 29 30 protected SomniMessageProducer(SomniDestination destination,String name,String connectionClientID) 31 { 32 this.destination = destination; 33 this.name = name; 34 this.connectionClientID = connectionClientID; 35 } 36 37 public boolean isClosed() 38 { 39 synchronized(guard) 40 { 41 return closed; 42 } 43 } 44 45 65 public void setDisableMessageID(boolean value) 66 throws JMSException 67 { 68 synchronized(guard) 69 { 70 useMessageIDs = !value; 71 } 72 } 73 74 82 public boolean getDisableMessageID() 83 throws JMSException 84 { 85 synchronized(guard) 86 { 87 return !useMessageIDs; 88 } 89 } 90 91 110 public void setDisableMessageTimestamp(boolean value) 111 throws JMSException 112 { 113 synchronized(guard) 114 { 115 useTimestamps = !value; 116 } 117 } 118 119 127 public boolean getDisableMessageTimestamp() 128 throws JMSException 129 { 130 synchronized(guard) 131 { 132 return !useTimestamps; 133 } 134 } 135 136 152 public void setDeliveryMode(int deliveryMode) 153 throws JMSException 154 { 155 synchronized(guard) 156 { 157 if(deliveryMode!=DeliveryMode.NON_PERSISTENT) 158 { 159 throw new IllegalStateException ("DeliveryMode can only be DeliveryMode.NON_PERSISTENT."); 160 } 161 } 162 } 163 164 173 public int getDeliveryMode() 174 throws JMSException 175 { 176 synchronized(guard) 177 { 178 return DeliveryMode.NON_PERSISTENT; 179 } 180 } 181 182 199 public void setPriority(int defaultPriority) 200 throws JMSException 201 { 202 if((defaultPriority<0)||(defaultPriority>9)) 203 { 204 throw new IllegalStateException ("defaultPriority must be between 0 and 9, not "+defaultPriority); 205 } 206 synchronized(guard) 207 { 208 this.defaultPriority = defaultPriority; 209 } 210 } 211 212 221 public int getPriority() 222 throws JMSException 223 { 224 synchronized(guard) 225 { 226 return defaultPriority; 227 } 228 } 229 230 244 public void setTimeToLive(long timeToLive) 245 throws JMSException 246 { 247 if(timeToLive < 0) 248 { 249 throw new IllegalStateException ("timeToLive must be 0 or larger, not "+timeToLive); 250 } 251 synchronized(guard) 252 { 253 this.timeToLive = timeToLive; 254 } 255 } 256 257 267 public long getTimeToLive() 268 throws JMSException 269 { 270 synchronized(guard) 271 { 272 return timeToLive; 273 } 274 } 275 276 285 286 public abstract Destination getDestination() throws JMSException ; 287 288 299 public void close() 300 throws JMSException 301 { 302 synchronized(guard) 303 { 304 closed=true; 305 SomniLogger.IT.finer(name+" closed."); 306 } 307 } 308 309 private static final String PREFIX = "ID:"; 310 private int counter = 0; 311 312 private String createMessageID(int counterValue) 313 { 314 StringBuffer buffy = new StringBuffer (); 315 buffy.append(PREFIX); 316 buffy.append(name); 317 buffy.append(":"+counterValue); 318 319 return buffy.toString(); 320 } 321 322 protected void setMessageDetails(SomniMessage message,int deliveryMode,int priority,long timeToLive) 323 throws JMSException 324 { 325 if(useMessageIDs) 326 { 327 message.setSomniProducerCount(counter); 328 counter++; 329 message.setJMSMessageID(createMessageID(counter)); 330 } 331 else 332 { 333 message.setJMSMessageID(null); 334 } 335 if(useTimestamps) 336 { 337 message.setJMSTimestamp(System.currentTimeMillis()); 338 if(timeToLive>0) 339 { 340 message.setJMSExpiration(message.getJMSTimestamp()+timeToLive); 341 } 342 else 343 { 344 message.setJMSExpiration(0); 345 } 346 } 347 else 348 { 349 message.setJMSTimestamp(0); 350 message.setJMSExpiration(0); 351 } 352 message.setJMSDeliveryMode(deliveryMode); 353 if(message.getJMSPriority() < priority) 354 { 355 message.setJMSPriority(priority); 356 } 357 message.setJMSDestination(destination); 358 message.setSomniProducerConnectionClientID(connectionClientID); 359 message.setReadOnly(); 361 } 362 363 protected void logSent(Message message,String sendOrPublish) 364 { 365 if(message!=null) 366 { 367 StringBuffer buffy = new StringBuffer (); 369 370 buffy.append(name); 371 buffy.append(" "); 372 buffy.append(sendOrPublish); 373 buffy.append(" "); 374 buffy.append(message.toString()); 375 376 SomniLogger.IT.finest(buffy.toString()); 377 } 378 } 379 380 protected String getName() 381 { 382 return name; 383 } 384 385 } 386 387 407 | Popular Tags |