1 6 7 package org.jfox.jms; 8 9 import javax.jms.DeliveryMode ; 10 import javax.jms.Destination ; 11 import javax.jms.JMSException ; 12 import javax.jms.Message ; 13 import javax.jms.MessageProducer ; 14 import javax.jms.Queue ; 15 import javax.jms.QueueSender ; 16 import javax.jms.Topic ; 17 import javax.jms.TopicPublisher ; 18 19 import org.jfox.ioc.util.UUID; 20 21 24 25 public class JMSProducer implements MessageProducer , QueueSender , TopicPublisher { 26 29 private int deliveryMode = Message.DEFAULT_DELIVERY_MODE; 30 33 private int priority = Message.DEFAULT_PRIORITY; 34 35 private long timeToLive = Message.DEFAULT_TIME_TO_LIVE; 36 37 40 private boolean disableMessageID = false; 41 44 private boolean disableTimestamp = false; 45 46 private Destination destination; 47 private JMSSession session; 48 49 private boolean closed = false; 50 51 public JMSProducer(JMSSession session, Destination destination) { 52 this.session = session; 53 this.destination = destination; 54 } 55 56 public void setDisableMessageID(boolean disableMessageID) throws JMSException { 57 checkClosed(); 58 this.disableMessageID = disableMessageID; 59 } 60 61 public boolean getDisableMessageID() throws JMSException { 62 return disableMessageID; 63 } 64 65 public void setDisableMessageTimestamp(boolean disableMessageTimestamp) throws JMSException { 66 checkClosed(); 67 this.disableTimestamp = disableMessageTimestamp; 68 } 69 70 public boolean getDisableMessageTimestamp() throws JMSException { 71 return disableTimestamp; 72 } 73 74 public void setDeliveryMode(int deliveryMode) throws JMSException { 75 checkClosed(); 76 validateDeliveryMode(deliveryMode); 77 this.deliveryMode = deliveryMode; 78 } 79 80 public int getDeliveryMode() throws JMSException { 81 return deliveryMode; 82 } 83 84 public void setPriority(int priority) throws JMSException { 85 checkClosed(); 86 validatePriority(priority); 87 this.priority = priority; 88 } 89 90 public int getPriority() throws JMSException { 91 return priority; 92 } 93 94 public void setTimeToLive(long timeToLive) throws JMSException { 95 checkClosed(); 96 this.timeToLive = timeToLive; 97 } 98 99 public long getTimeToLive() throws JMSException { 100 return timeToLive; 101 } 102 103 public Destination getDestination() throws JMSException { 104 return destination; 105 } 106 107 public void close() throws JMSException { 108 this.closed = true; 109 } 110 111 public void send(Message message) throws JMSException { 112 send(destination, message, deliveryMode, priority, timeToLive); 113 } 114 115 public void send(Message message, int deliveryMode, int priority, long timeToLive) throws JMSException { 116 send(destination, message, deliveryMode, priority, timeToLive); 117 } 118 119 public void send(Destination destination, Message message) throws JMSException { 120 send(destination, message, deliveryMode, priority, timeToLive); 121 } 122 123 public void send(Destination destination, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException { 124 checkClosed(); 125 if (destination == null) 126 throw new JMSException ("Null destination"); 127 if (message == null) 128 throw new JMSException ("Null message"); 129 if (disableMessageID == false) { 130 message.setJMSMessageID("ID:" + UUID.randomUUID().toString()); 131 } 132 133 if (disableTimestamp == false) { 134 message.setJMSTimestamp(System.currentTimeMillis()); 135 } 136 137 message.setJMSDestination(destination); 138 message.setJMSDeliveryMode(deliveryMode); 139 message.setJMSPriority(priority); 140 141 if (disableTimestamp == false && timeToLive != 0) { 142 message.setJMSExpiration(message.getJMSTimestamp() + timeToLive); 143 } 144 session.sendMessage(message); 145 } 147 148 public Queue getQueue() throws JMSException { 149 return (Queue ) destination; 150 } 151 152 public void send(Queue queue, Message message) throws JMSException { 153 send(queue, message, deliveryMode, priority, timeToLive); 154 } 155 156 public void send(Queue queue, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException { 157 send((Destination ) queue, message, deliveryMode, priority, timeToLive); 158 } 159 160 public Topic getTopic() throws JMSException { 161 return (Topic ) destination; 162 } 163 164 public void publish(Message message) throws JMSException { 165 send(message); 166 } 167 168 public void publish(Message message, int deliveryMode, int priority, long timeToLive) throws JMSException { 169 send(destination, message, deliveryMode, priority, timeToLive); 170 } 171 172 public void publish(Topic topic, Message message) throws JMSException { 173 send(topic, message); 174 } 175 176 public void publish(Topic topic, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException { 177 send((Destination ) topic, message, deliveryMode, priority, timeToLive); 178 } 179 180 186 static void validateDeliveryMode(int deliveryMode) throws JMSException { 187 if (deliveryMode != DeliveryMode.NON_PERSISTENT && 188 deliveryMode != DeliveryMode.PERSISTENT) 189 throw new JMSException ("Invalid delivery mode " + deliveryMode); 190 } 191 192 195 static void validatePriority(int priority) throws JMSException { 196 if (priority < 0 || priority > 9) { 197 throw new JMSException ("Invalid priority " + priority); 198 } 199 } 200 201 private void checkClosed() throws javax.jms.IllegalStateException { 202 if (closed) { 203 throw new javax.jms.IllegalStateException ("MessageProducer closed"); 204 } 205 } 206 207 public static void main(String [] args) { 208 209 } 210 } 211 | Popular Tags |