KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > MessageProducer


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s): ScalAgent Distributed Technologies
23  */

24 package org.objectweb.joram.client.jms;
25
26 import javax.jms.IllegalStateException JavaDoc;
27 import javax.jms.MessageFormatException JavaDoc;
28 import javax.jms.JMSException JavaDoc;
29
30 import org.objectweb.joram.shared.client.*;
31
32 import org.objectweb.util.monolog.api.BasicLevel;
33 import org.objectweb.joram.shared.JoramTracing;
34
35 /**
36  * Implements the <code>javax.jms.MessageProducer</code> interface.
37  */

38 public class MessageProducer implements javax.jms.MessageProducer JavaDoc {
39   /** Default delivery mode. */
40   private int deliveryMode = javax.jms.DeliveryMode.PERSISTENT;
41
42   /** Default priority. */
43   private int priority = 4;
44
45   /** Default time to live. */
46   private long timeToLive = 0;
47
48   /**
49    * <code>true</code> if the client requests not to use the message
50    * identifiers; however it is not taken into account, as our MOM needs
51    * message identifiers for managing acknowledgements.
52    */

53   private boolean messageIDDisabled = false;
54
55   /** <code>true</code> if the time stamp is disabled. */
56   private boolean timestampDisabled = false;
57
58   /** <code>true</code> if the producer's destination is identified. */
59   private boolean identified = true;
60
61   /** <code>true</code> if the producer is closed. */
62   protected boolean closed = false;
63
64   /** The session the producer belongs to. */
65   protected Session sess;
66
67   /** The destination the producer sends messages to. */
68   protected Destination dest = null;
69
70   /**
71    * Constructs a producer.
72    *
73    * @param sess The session the producer belongs to.
74    * @param dest The destination the producer sends messages to.
75    *
76    * @exception IllegalStateException If the connection is broken.
77    * @exception JMSException If the creation fails for any other reason.
78    */

79   MessageProducer(Session sess,
80                   Destination dest)
81     throws JMSException JavaDoc {
82     this.sess = sess;
83     this.dest = dest;
84     if (dest == null)
85       identified = false;
86
87     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
88       JoramTracing.dbgClient.log(BasicLevel.DEBUG, this + ": created.");
89   }
90
91   /**
92    * API method; not taken into account.
93    *
94    * @exception IllegalStateException If the producer is closed.
95    */

96   public synchronized void setDisableMessageID(boolean value) throws JMSException JavaDoc
97   {
98     if (closed)
99       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
100   }
101
102   /**
103    * Sets the producer's default delivery mode.
104    * API method.
105    *
106    * @exception IllegalStateException If the producer is closed.
107    * @exception JMSException When setting an invalid delivery mode.
108    */

109   public synchronized void setDeliveryMode(int deliveryMode) throws JMSException JavaDoc
110   {
111     if (closed)
112       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
113
114     if (deliveryMode != javax.jms.DeliveryMode.PERSISTENT
115         && deliveryMode != javax.jms.DeliveryMode.NON_PERSISTENT)
116       throw new JMSException JavaDoc("Can't set invalid delivery mode.");
117
118     this.deliveryMode = deliveryMode;
119   }
120
121   /**
122    * Sets the producer's default priority.
123    * API method.
124    *
125    * @exception IllegalStateException If the producer is closed.
126    * @exception JMSException When setting an invalid priority.
127    */

128   public synchronized void setPriority(int priority) throws JMSException JavaDoc
129   {
130     if (closed)
131       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
132
133     if (priority < 0 || priority > 9)
134       throw new JMSException JavaDoc("Can't set invalid priority value.");
135
136     this.priority = priority;
137   }
138
139   /**
140    * Sets the default duration of time in milliseconds that a produced
141    * message should be retained by the provider.
142    * API method.
143    *
144    * @exception IllegalStateException If the producer is closed.
145    */

146   public synchronized void setTimeToLive(long timeToLive) throws JMSException JavaDoc
147   {
148     if (closed)
149       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
150
151     this.timeToLive = timeToLive;
152   }
153
154   /**
155    * API method.
156    *
157    * @exception IllegalStateException If the producer is closed.
158    */

159   public synchronized void setDisableMessageTimestamp(boolean value) throws JMSException JavaDoc
160   {
161     if (closed)
162       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
163
164     this.timestampDisabled = value;
165   }
166
167   /**
168    * Gets the destination associated with this MessageProducer.
169    * API method.
170    *
171    * @exception IllegalStateException If the producer is closed.
172    */

173   public synchronized javax.jms.Destination JavaDoc getDestination() throws JMSException JavaDoc
174   {
175     if (closed)
176       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
177
178     return dest;
179   }
180
181   /**
182    * API method.
183    *
184    * @exception IllegalStateException If the producer is closed.
185    */

186   public synchronized boolean getDisableMessageID() throws JMSException JavaDoc
187   {
188     if (closed)
189       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
190
191     return messageIDDisabled;
192   }
193
194   /**
195    * Gets the producer's default delivery mode.
196    * API method.
197    *
198    * @exception IllegalStateException If the producer is closed.
199    */

200   public synchronized int getDeliveryMode() throws JMSException JavaDoc
201   {
202     if (closed)
203       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
204
205     return deliveryMode;
206   }
207
208   /**
209    * Gets the producer's default priority.
210    * API method.
211    *
212    * @exception IllegalStateException If the producer is closed.
213    */

214   public synchronized int getPriority() throws JMSException JavaDoc
215   {
216     if (closed)
217       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
218
219     return priority;
220   }
221
222   /**
223    * Gets the default duration in milliseconds that a produced message
224    * should be retained by the provider.
225    * API method.
226    *
227    * @exception IllegalStateException If the producer is closed.
228    */

229   public synchronized long getTimeToLive() throws JMSException JavaDoc
230   {
231     if (closed)
232       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
233
234     return timeToLive;
235   }
236
237   /**
238    * API method.
239    *
240    * @exception IllegalStateException If the producer is closed.
241    */

242   public synchronized boolean getDisableMessageTimestamp() throws JMSException JavaDoc
243   {
244     if (closed)
245       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
246
247     return timestampDisabled;
248   }
249
250
251   /**
252    * Sends a message with the default delivery parameters.
253    *
254    * @exception UnsupportedOperationException If the dest is unidentified.
255    * @exception IllegalStateException If the producer is closed, or if the
256    * connection is broken.
257    * @exception JMSException If the request fails for any other reason.
258    */

259   public synchronized void send(javax.jms.Message JavaDoc message) throws JMSException JavaDoc
260   {
261     if (! identified)
262       throw new UnsupportedOperationException JavaDoc("Can't send message to"
263                                               + " an unidentified"
264                                               + " destination.");
265     // Actually producing it:
266
doSend(dest, message, deliveryMode, priority, timeToLive);
267   }
268
269   /**
270    * Sends a message with given delivery parameters.
271    *
272    * @exception UnsupportedOperationException If the dest is unidentified.
273    * @exception IllegalStateException If the producer is closed, or if the
274    * connection is broken.
275    * @exception JMSException If the request fails for any other reason.
276    */

277   public synchronized void send(javax.jms.Message JavaDoc message,
278                                 int deliveryMode,
279                                 int priority,
280                                 long timeToLive) throws JMSException JavaDoc
281   {
282     if (! identified)
283       throw new UnsupportedOperationException JavaDoc("Can't send message to"
284                                               + " an unidentified"
285                                               + " destination.");
286     // Actually producing it:
287
doSend(dest, message, deliveryMode, priority, timeToLive);
288   }
289
290   /**
291    * Sends a message with default delivery parameters for an unidentified
292    * message producer.
293    *
294    * @exception UnsupportedOperationException When the producer did not
295    * properly identify itself.
296    * @exception JMSSecurityException If the user if not a WRITER on the
297    * specified destination.
298    * @exception IllegalStateException If the producer is closed, or if the
299    * connection is broken.
300    * @exception JMSException If the request fails for any other reason.
301    */

302   public synchronized void send(javax.jms.Destination JavaDoc dest,
303                                 javax.jms.Message JavaDoc message) throws JMSException JavaDoc
304   {
305     if (identified)
306       throw new UnsupportedOperationException JavaDoc("An unidentified message"
307                                               + " producer can't use this"
308                                               + " identified message"
309                                               + " producer.");
310     if (dest == null)
311       throw new UnsupportedOperationException JavaDoc("Can't send message to"
312                                               + " an unidentified"
313                                               + " destination.");
314
315     doSend((Destination) dest, message, deliveryMode, priority, timeToLive);
316   }
317
318   /**
319    * Sends a message with given delivery parameters for an unidentified
320    * message producer.
321    *
322    * @exception UnsupportedOperationException When the producer did not
323    * properly identify itself.
324    * @exception JMSSecurityException If the user if not a WRITER on the
325    * specified destination.
326    * @exception IllegalStateException If the producer is closed, or if the
327    * connection is broken.
328    * @exception JMSException If the request fails for any other reason.
329    */

330   public synchronized void send(javax.jms.Destination JavaDoc dest,
331                                 javax.jms.Message JavaDoc message,
332                                 int deliveryMode,
333                                 int priority,
334                                 long timeToLive) throws JMSException JavaDoc
335   {
336     if (identified)
337       throw new UnsupportedOperationException JavaDoc("An unidentified message"
338                                               + " producer can't use this"
339                                               + " identified message"
340                                               + " producer.");
341     if (dest == null)
342       throw new UnsupportedOperationException JavaDoc("Can't send message to"
343                                               + " an unidentified"
344                                               + " destination.");
345
346     doSend((Destination) dest, message, deliveryMode, priority, timeToLive);
347   }
348
349   /**
350    * Closes the message producer.
351    * API method.
352    *
353    * @exception JMSException Actually never thrown.
354    */

355   public synchronized void close() throws JMSException JavaDoc
356   {
357     // Ignoring call if producer is already closed:
358
if (closed)
359       return;
360
361     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
362       JoramTracing.dbgClient.log(BasicLevel.DEBUG, "--- " + this
363                                  + ": closing...");
364
365     sess.closeProducer(this);
366     closed = true;
367
368     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
369       JoramTracing.dbgClient.log(BasicLevel.DEBUG, this + ": closed.");
370
371   }
372
373   /**
374    * Actually sends a message to a given destination.
375    *
376    * @exception MessageFormatException If the message to send is invalid.
377    * @exception InvalidDestinationException If the specified destination is
378    * invalid.
379    * @exception IllegalStateException If the connection is broken.
380    * @exception JMSException If the request fails for any other reason.
381    */

382   private void doSend(Destination dest,
383                       javax.jms.Message JavaDoc message,
384                       int deliveryMode,
385                       int priority,
386                       long timeToLive)
387     throws JMSException JavaDoc {
388     if (closed)
389       throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
390     
391     sess.send(dest, message, deliveryMode, priority,
392               timeToLive, timestampDisabled);
393   }
394 }
395
Popular Tags