Source code: org/mom4j/jms/MessageProducerImpl.java
1 package org.mom4j.jms;
2
3 import javax.jms.DeliveryMode;
4 import javax.jms.Destination;
5 import javax.jms.JMSException;
6 import javax.jms.Message;
7 import javax.jms.MessageProducer;
8 import javax.jms.Queue;
9 import javax.jms.QueueSender;
10 import javax.jms.Topic;
11 import javax.jms.TopicPublisher;
12
13
14 public class MessageProducerImpl implements MessageProducer, QueueSender, TopicPublisher {
15
16 private DestinationImpl destination;
17 private SessionImpl session;
18 private long timeToLive;
19 private int priority;
20 private int deliveryMode;
21 private boolean disableMessageTimestamp;
22 private boolean disableMessageId;
23
24
25 MessageProducerImpl(DestinationImpl destination, SessionImpl session) {
26 this.destination = destination;
27 this.session = session;
28 this.priority = Message.DEFAULT_PRIORITY;
29 this.timeToLive = Message.DEFAULT_TIME_TO_LIVE;
30 this.deliveryMode = DeliveryMode.PERSISTENT;
31 this.disableMessageTimestamp = false;
32 this.disableMessageId = false;
33 }
34
35 public boolean getDisableMessageTimestamp()
36 throws javax.jms.JMSException
37 {
38 return disableMessageTimestamp;
39 }
40
41
42 public int getPriority()
43 throws javax.jms.JMSException
44 {
45 return priority;
46 }
47
48
49 public void setDeliveryMode(int mode)
50 throws javax.jms.JMSException
51 {
52 if(mode != DeliveryMode.NON_PERSISTENT && mode != DeliveryMode.PERSISTENT) {
53 throw new JMSException("illegal delivery-mode:" + mode);
54 }
55 deliveryMode = mode;
56 }
57
58
59 public void setDisableMessageTimestamp(boolean value)
60 throws javax.jms.JMSException
61 {
62 disableMessageTimestamp = value;
63 }
64
65
66 public void setTimeToLive(long timetolive)
67 throws javax.jms.JMSException
68 {
69 timeToLive = timetolive;
70 }
71
72
73 public long getTimeToLive()
74 throws javax.jms.JMSException
75 {
76 return timeToLive;
77 }
78
79
80 public int getDeliveryMode()
81 throws javax.jms.JMSException
82 {
83 return deliveryMode;
84 }
85
86
87 public boolean getDisableMessageID()
88 throws javax.jms.JMSException
89 {
90 return disableMessageId;
91 }
92
93
94 public void setDisableMessageID(boolean value)
95 throws javax.jms.JMSException
96 {
97 disableMessageId = value;
98 }
99
100
101 public void setPriority(int value)
102 throws javax.jms.JMSException
103 {
104 if(value < 0 || value > 9) {
105 throw new JMSException("illegal priority:" + value);
106 }
107 priority = value;
108 }
109
110
111 public Destination getDestination()
112 throws javax.jms.JMSException
113 {
114 return this.destination;
115 }
116
117
118 public void send(Message msg, int deliveryMode, int priority, long timeToLive)
119 throws javax.jms.JMSException
120 {
121 this.send(this.destination,
122 msg,
123 deliveryMode,
124 priority,
125 timeToLive);
126 }
127
128
129 public void send(Message msg)
130 throws javax.jms.JMSException
131 {
132 this.send(this.destination,
133 msg,
134 this.getDeliveryMode(),
135 this.getPriority(),
136 this.getTimeToLive());
137 }
138
139
140 public void send(Destination destination, Message msg)
141 throws javax.jms.JMSException
142 {
143 this.send(destination,
144 msg,
145 this.getDeliveryMode(),
146 this.getPriority(),
147 this.getTimeToLive());
148 }
149
150
151 public void send(Destination destination,
152 Message msg,
153 int deliveryMode,
154 int priority,
155 long timeToLive)
156 throws javax.jms.JMSException
157 {
158 if(destination == null) {
159 throw new JMSException("destination may not be null");
160 }
161 if(msg == null) {
162 throw new JMSException("msg may not be null");
163 }
164 if(deliveryMode != DeliveryMode.PERSISTENT &&
165 deliveryMode != DeliveryMode.NON_PERSISTENT)
166 {
167 throw new JMSException(
168 "unknown value for deliveryMode:" + deliveryMode);
169 }
170 if(priority < 0 || priority > 9) {
171 throw new JMSException("illegal value for priority:" + priority);
172 }
173 long tt = System.currentTimeMillis();
174
175 msg.setJMSDestination(destination);
176 msg.setJMSDeliveryMode(deliveryMode);
177 msg.setJMSPriority(priority);
178 if(timeToLive > 0) {
179 msg.setJMSExpiration(tt + timeToLive);
180 } else {
181 msg.setJMSExpiration(timeToLive);
182 }
183 if(!this.getDisableMessageTimestamp()) {
184 msg.setJMSTimestamp(tt);
185 }
186
187 this.session.send((MessageImpl)msg, this.getDisableMessageID());
188 }
189
190
191 public void close()
192 throws javax.jms.JMSException
193 {
194 this.session.closeProducer(this);
195 }
196
197
198 //
199 // QueueSender specific methods
200 //
201
202 public Queue getQueue()
203 throws javax.jms.JMSException
204 {
205 return (Queue)this.destination;
206 }
207
208
209 public void send(Queue queue, Message msg)
210 throws javax.jms.JMSException
211 {
212 this.send((Destination)queue, msg);
213 }
214
215
216 public void send(Queue queue,
217 Message msg,
218 int deliveryMode,
219 int priority,
220 long timeToLive)
221 throws javax.jms.JMSException
222 {
223 this.send((Destination)queue, msg, deliveryMode, priority, timeToLive);
224 }
225
226
227 //
228 // TopicPublisher specific methods
229 //
230
231 public Topic getTopic()
232 throws JMSException
233 {
234 return (Topic)this.destination;
235 }
236
237
238 public void publish(Message msg, int deliveryMode, int priority, long timeToLive)
239 throws JMSException
240 {
241 this.publish((Topic)this.destination,
242 msg,
243 deliveryMode,
244 priority,
245 timeToLive);
246 }
247
248
249 public void publish(Topic topic, Message msg)
250 throws JMSException
251 {
252 this.publish(topic,
253 msg,
254 this.getDeliveryMode(),
255 this.getPriority(),
256 this.getTimeToLive());
257 }
258
259
260 public void publish(Message msg)
261 throws JMSException
262 {
263 this.publish((Topic)this.destination,
264 msg,
265 this.getDeliveryMode(),
266 this.getPriority(),
267 this.getTimeToLive());
268 }
269
270
271 public void publish(Topic topic, Message msg, int deliveryMode, int priority, long timeToLive)
272 throws JMSException
273 {
274 this.send((Destination)topic, msg, deliveryMode, priority, timeToLive);
275 }
276
277 }