KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > client > JmsMessageProducer


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: JmsMessageProducer.java,v 1.2 2005/03/18 03:36:37 tanderson Exp $
44  */

45 package org.exolab.jms.client;
46
47 import java.util.Date JavaDoc;
48
49 import javax.jms.DeliveryMode JavaDoc;
50 import javax.jms.Destination JavaDoc;
51 import javax.jms.InvalidDestinationException JavaDoc;
52 import javax.jms.JMSException JavaDoc;
53 import javax.jms.Message JavaDoc;
54 import javax.jms.MessageFormatException JavaDoc;
55 import javax.jms.MessageProducer JavaDoc;
56 import javax.jms.Session JavaDoc;
57
58 import org.exolab.jms.message.MessageId;
59
60
61 /**
62  * Client implementation of the <code>javax.jms.MessageProducer</code>
63  * interface
64  *
65  * @version $Revision: 1.2 $ $Date: 2005/03/18 03:36:37 $
66  * @author <a HREF="mailto:jima@comware.com.au">Jim Alateras</a>
67  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
68  */

69 class JmsMessageProducer implements MessageProducer JavaDoc {
70
71     /**
72      * The destination that messages are delivered to, or <code>null</code>
73      * if this is an unidentified producer.
74      */

75     private final Destination JavaDoc _destination;
76
77     /**
78      * The default priority for messages.
79      */

80     private int _defaultPriority = Message.DEFAULT_PRIORITY;
81
82     /**
83      * The default time to live for messages.
84      */

85     private long _defaultTtl = 0;
86
87     /**
88      * The default delivery mode for messages.
89      */

90     private int _deliveryMode = DeliveryMode.PERSISTENT;
91
92     /**
93      * This flag is used to indicate whether timestamps are enabled or
94      * disabled.
95      */

96     private boolean _disableTimestamp = false;
97
98     /**
99      * This flag is used to indicate whether message ids are enabled or
100      * disabled.
101      */

102     private boolean _disableMessageId = false;
103
104     /**
105      * The session that created this producer.
106      */

107     private JmsSession _session = null;
108
109
110     /**
111      * Construct a new <code>JmsMessageProducer</code>.
112      *
113      * @param session session responsible for this producer
114      * @param destination the destination that messages are delivered to,
115      * or <code>null</code> if this is an unidentified producer
116      */

117     public JmsMessageProducer(JmsSession session, Destination JavaDoc destination) {
118         if (session == null) {
119             throw new IllegalArgumentException JavaDoc("Argument 'session' is null");
120         }
121         _session = session;
122         _destination = destination;
123     }
124
125     /**
126      * Gets the destination associated with this <code>MessageProducer</code>.
127      *
128      * @return this producer's <code>Destination/<code>
129      */

130     public Destination JavaDoc getDestination() {
131         return _destination;
132     }
133
134     /**
135      * Set whether message IDs are disabled.
136      *
137      * @param value indicates if message IDs are disabled
138      */

139     public void setDisableMessageID(boolean value) {
140         _disableMessageId = value;
141     }
142
143     /**
144      * Returns if message IDs are disabled
145      *
146      * @return <code>true</code> if message IDs are disabled
147      */

148     public boolean getDisableMessageID() {
149         return _disableMessageId;
150     }
151
152     /**
153      * Set whether message timestamps are disabled.
154      *
155      * @param value indicates if message timestamps are disabled
156      */

157     public void setDisableMessageTimestamp(boolean value) {
158         _disableTimestamp = value;
159     }
160
161     /**
162      * Returns if message timestamps are disabled.
163      *
164      * @return <code>true</code> if message timestamps are disabled
165      */

166     public boolean getDisableMessageTimestamp() {
167         return _disableTimestamp;
168     }
169
170     /**
171      * Set the producer's default delivery mode.
172      *
173      * @param deliveryMode the delivery mode. Legal values are
174      * <code>DeliveryMode.NON_PERSISTENT</code> or
175      * <code>DeliveryMode.PERSISTENT</code>
176      */

177     public void setDeliveryMode(int deliveryMode) {
178         _deliveryMode = deliveryMode;
179     }
180
181     /**
182      * Returns the producer's default delivery mode.
183      *
184      * @return the default delivery mode
185      */

186     public int getDeliveryMode() {
187         return _deliveryMode;
188     }
189
190     /**
191      * Set the producer's default priority.
192      *
193      * @param priority the priority. Must be a value between 0 and 9
194      */

195     public void setPriority(int priority) {
196         _defaultPriority = priority;
197     }
198
199     /**
200      * Returns the producer's default delivery mode.
201      *
202      * @return the default delivery mode
203      */

204     public int getPriority() {
205         return _defaultPriority;
206     }
207
208     /**
209      * Set the default time to live for messages.
210      *
211      * @param timeToLive the message time to live in milliseconds; zero is
212      * unlimited
213      */

214     public void setTimeToLive(long timeToLive) {
215         _defaultTtl = timeToLive;
216     }
217
218     /**
219      * Returns the default time to live for messages.
220      *
221      * @return the default message time to live in milliseconds; zero is
222      * unlimited
223      */

224     public long getTimeToLive() {
225         return _defaultTtl;
226     }
227
228     /**
229      * Sends a message using the <code>MessageProducer</code>'s default delivery
230      * mode, priority, and time to live.
231      *
232      * @param message the message to send
233      * @throws JMSException if the JMS provider fails to send
234      * the message due to some internal
235      * error.
236      * @throws MessageFormatException if an invalid message is
237      * specified.
238      * @throws InvalidDestinationException if a client uses this method with a
239      * <code>MessageProducer</code> with
240      * an invalid destination.
241      * @throws UnsupportedOperationException if a client uses this method with a
242      * <code>MessageProducer</code> that
243      * did not specify a destination at
244      * creation time.
245      */

246     public void send(Message JavaDoc message) throws JMSException JavaDoc {
247         send(getDestination(), message, getDeliveryMode(), getPriority(),
248              getTimeToLive());
249     }
250
251     /**
252      * Sends a message to the destination, specifying delivery mode, priority,
253      * and time to live.
254      *
255      * @param message the message to send
256      * @param deliveryMode the delivery mode to use
257      * @param priority the priority for this message
258      * @param timeToLive the message's lifetime (in milliseconds)
259      * @throws JMSException if the JMS provider fails to send
260      * the message due to some internal
261      * error.
262      * @throws MessageFormatException if an invalid message is
263      * specified.
264      * @throws InvalidDestinationException if a client uses this method with a
265      * <code>MessageProducer</code> with
266      * an invalid destination.
267      * @throws UnsupportedOperationException if a client uses this method with a
268      * <code>MessageProducer</code> that
269      * did not specify a destination at
270      * creation time.
271      */

272     public void send(Message JavaDoc message, int deliveryMode, int priority,
273                      long timeToLive) throws JMSException JavaDoc {
274         send(getDestination(), message, deliveryMode, priority, timeToLive);
275     }
276
277     /**
278      * Sends a message to a destination for an unidentified message producer.
279      * Uses the <code>MessageProducer</code>'s default delivery mode, priority,
280      * and time to live.
281      * <p/>
282      * <P>Typically, a message producer is assigned a destination at creation
283      * time; however, the JMS API also supports unidentified message producers,
284      * which require that the destination be supplied every time a message is
285      * sent.
286      *
287      * @param destination the destination to send this message to
288      * @param message the message to send
289      * @throws JMSException if the JMS provider fails to send
290      * the message due to some internal
291      * error.
292      * @throws MessageFormatException if an invalid message is
293      * specified.
294      * @throws InvalidDestinationException if a client uses this method with
295      * an invalid destination.
296      * @throws UnsupportedOperationException if a client uses this method with a
297      * <code>MessageProducer</code> that
298      * specified a destination at creation
299      * time.
300      */

301     public void send(Destination JavaDoc destination, Message JavaDoc message)
302             throws JMSException JavaDoc {
303         send(destination, message, getDeliveryMode(), getPriority(),
304              getTimeToLive());
305     }
306
307     /**
308      * Sends a message to a destination for an unidentified message producer,
309      * specifying delivery mode, priority and time to live.
310      * <p/>
311      * <P>Typically, a message producer is assigned a destination at creation
312      * time; however, the JMS API also supports unidentified message producers,
313      * which require that the destination be supplied every time a message is
314      * sent.
315      *
316      * @param destination the destination to send this message to
317      * @param message the message to send
318      * @param deliveryMode the delivery mode to use
319      * @param priority the priority for this message
320      * @param timeToLive the message's lifetime (in milliseconds)
321      * @throws JMSException if the JMS provider fails to send the
322      * message due to some internal error.
323      * @throws MessageFormatException if an invalid message is specified.
324      * @throws InvalidDestinationException if a client uses this method with an
325      * invalid destination.
326      */

327     public void send(Destination JavaDoc destination, Message JavaDoc message,
328                      int deliveryMode, int priority, long timeToLive)
329             throws JMSException JavaDoc {
330
331         if (!(destination instanceof JmsDestination)) {
332             // don't support non-OpenJMS or null destinations
333
throw new InvalidDestinationException JavaDoc(
334                 "Invalid destination: " + destination);
335         }
336         if (message == null) {
337             throw new MessageFormatException JavaDoc("Null message");
338         }
339
340         message.setJMSMessageID(MessageId.create());
341         message.setJMSDestination(destination);
342         message.setJMSTimestamp((new Date JavaDoc()).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 the destination is a temporary one, override the delivery
352
// mode to NON_PERSISTENT
353
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     /**
363      * Close the producer.
364      *
365      * @throws JMSException if the producer can't be closed
366      */

367     public synchronized void close() throws JMSException JavaDoc {
368         if (_session != null) {
369             _session.removeProducer(this);
370         }
371         _session = null;
372     }
373
374 }
375
Popular Tags