View Javadoc

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.19.2.1 2004/05/01 04:56:45 tanderson Exp $
44   */
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  
57  import org.exolab.jms.message.MessageId;
58  
59  
60  /***
61   * Client implementation of the <code>javax.jms.MessageProducer</code>
62   * interface
63   *
64   * @version     $Revision: 1.19.2.1 $ $Date: 2004/05/01 04:56:45 $
65   * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
66   */
67  abstract class JmsMessageProducer
68      implements MessageProducer {
69  
70      /***
71       * The default priority for messages.
72       */
73      private int _defaultPriority = Message.DEFAULT_PRIORITY;
74  
75      /***
76       * The default time to live for messages.
77       */
78      private long _defaultTtl = 0;
79  
80      /***
81       * The default delivery mode for messages.
82       */
83      private int _deliveryMode = DeliveryMode.PERSISTENT;
84  
85      /***
86       * This flag is used to indicate whether timestamps are enabled or
87       * disabled.
88       */
89      private boolean _disableTimestamp = false;
90  
91      /***
92       * This flag is used to indicate whether message ids are enabled or
93       * disabled
94       */
95      private boolean _disableMessageId = false;
96  
97      /***
98       * The session that created this producer
99       */
100     private JmsSession _session = null;
101 
102 
103     /***
104      * Construct a new <code>JmsMessageProducer</code>
105      *
106      * @param session session responsible for this producer
107      */
108     public JmsMessageProducer(JmsSession session) {
109         if (session == null) {
110             throw new IllegalArgumentException("Argument 'session' is null");
111         }
112         _session = session;
113     }
114 
115     /***
116      * Set whether message IDs are disabled.
117      *
118      * @param value indicates if message IDs are disabled
119      */
120     public void setDisableMessageID(boolean value) {
121         _disableMessageId = value;
122     }
123 
124     /***
125      * Returns if message IDs are disabled
126      *
127      * @return <code>true</code> if message IDs are disabled
128      */
129     public boolean getDisableMessageID() {
130         return _disableMessageId;
131     }
132 
133     /***
134      * Set whether message timestamps are disabled
135      *
136      * @param value indicates if message timestamps are disabled
137      */
138     public void setDisableMessageTimestamp(boolean value) {
139         _disableTimestamp = value;
140     }
141 
142     /***
143      * Returns if message timestamps are disabled
144      *
145      * @return <code>true</code> if message timestamps are disabled
146      */
147     public boolean getDisableMessageTimestamp() {
148         return _disableTimestamp;
149     }
150 
151     /***
152      * Set the producer's default delivery mode
153      *
154      * @param deliveryMode the delivery mode. Legal values are
155      * <code>DeliveryMode.NON_PERSISTENT</code> or
156      * <code>DeliveryMode.PERSISTENT</code>
157      */
158     public void setDeliveryMode(int deliveryMode) {
159         _deliveryMode = deliveryMode;
160     }
161 
162     /***
163      * Returns the producer's default delivery mode
164      *
165      * @return the default delivery mode
166      */
167     public int getDeliveryMode() {
168         return _deliveryMode;
169     }
170 
171     /***
172      * Set the producer's default priority
173      *
174      * @param priority the priority. Must be a value between 0 and 9
175      */
176     public void setPriority(int priority) {
177         _defaultPriority = priority;
178     }
179 
180     /***
181      * Returns the producer's default delivery mode
182      *
183      * @return the default delivery mode
184      */
185     public int getPriority() {
186         return _defaultPriority;
187     }
188 
189     /***
190      * Set the default time to live for messages
191      *
192      * @param timeToLive the message time to live in milliseconds; zero is
193      * unlimited
194      */
195     public void setTimeToLive(long timeToLive) {
196         _defaultTtl = timeToLive;
197     }
198 
199     /***
200      * Returns the default time to live for messages
201      *
202      * @return the default message time to live in milliseconds; zero is
203      * unlimited
204      */
205     public long getTimeToLive() {
206         return _defaultTtl;
207     }
208 
209     /***
210      * Close the producer
211      *
212      * @throws JMSException if the producer can't be closed
213      */
214     public synchronized void close() throws JMSException {
215         _session = null;
216     }
217 
218     /***
219      * Release all resources used by this consumer
220      */
221     public synchronized void destroy() {
222         _session = null;
223     }
224 
225     /***
226      * Determines if the producer is closed
227      *
228      * @return <code>true</code> if the producer is closed
229      */
230     public synchronized boolean isClosed() {
231         return (_session == null);
232     }
233 
234     /***
235      * Returns the session that created this producer
236      *
237      * @return the session that created this producer
238      */
239     protected JmsSession getSession() {
240         return _session;
241     }
242 
243     /***
244      * Send a message.
245      *
246      * @param destination the destination to send to
247      * @param message the message to send
248      * @param deliveryMode the delivery mode to use
249      * @param priority the priority for the message
250      * @param timeToLive the message's lifetime, in milliseconds
251      * @throws JMSException if the message cannot be sent
252      */
253     protected void sendMessage(Destination destination, Message message,
254                                int deliveryMode, int priority, long timeToLive)
255         throws JMSException {
256 
257         if (!(destination instanceof JmsDestination)) {
258             // don't support non-OpenJMS or null destinations
259             throw new InvalidDestinationException(
260                 "Invalid destination: " + destination);
261         }
262         if (message == null) {
263             throw new MessageFormatException("Null message");
264         }
265 
266         message.setJMSMessageID(MessageId.create());
267         message.setJMSDestination(destination);
268         message.setJMSTimestamp((new Date()).getTime());
269         message.setJMSPriority(priority);
270 
271         if (timeToLive > 0) {
272             message.setJMSExpiration(System.currentTimeMillis() + timeToLive);
273         } else {
274             message.setJMSExpiration(0);
275         }
276 
277         // if the destination is a temporary one, override the delivery
278         // mode to NON_PERSISTENT
279         if (destination instanceof JmsTemporaryDestination) {
280             message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
281         } else {
282             message.setJMSDeliveryMode(deliveryMode);
283         }
284 
285         _session.sendMessage(message);
286     }
287 
288 }