KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > MessageProducer


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 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): Nicolas Tachker (ScalAgent)
23  */

24 package com.scalagent.kjoram;
25
26 import com.scalagent.kjoram.jms.*;
27 import com.scalagent.kjoram.excepts.IllegalStateException;
28 import com.scalagent.kjoram.excepts.*;
29
30 public class MessageProducer
31 {
32   /** Default delivery mode. */
33   private int deliveryMode = DeliveryMode.PERSISTENT;
34   /** Default priority. */
35   private int priority = 4;
36   /** Default time to live. */
37   private long timeToLive = 0;
38   /**
39    * <code>true</code> if the client requests not to use the message
40    * identifiers; however it is not taken into account, as our MOM needs
41    * message identifiers for managing acknowledgements.
42    */

43   private boolean messageIDDisabled = false;
44   /** <code>true</code> if the time stamp is disabled. */
45   private boolean timestampDisabled = false;
46   /** <code>true</code> if the producer's destination is identified. */
47   private boolean identified = true;
48
49   /** <code>true</code> if the producer is closed. */
50   protected boolean closed = false;
51   /** The session the producer belongs to. */
52   protected Session sess;
53   /** The destination the producer sends messages to. */
54   protected Destination dest = null;
55   
56
57   /**
58    * Constructs a producer.
59    *
60    * @param sess The session the producer belongs to.
61    * @param dest The destination the producer sends messages to.
62    *
63    * @exception IllegalStateException If the connection is broken.
64    * @exception JMSException If the creation fails for any other reason.
65    */

66   MessageProducer(Session sess, Destination dest) throws JMSException
67     {
68       this.sess = sess;
69       this.dest = dest;
70
71       if (dest == null)
72         identified = false;
73
74       sess.producers.addElement(this);
75
76       if (JoramTracing.dbgClient)
77         JoramTracing.log(JoramTracing.DEBUG, this + ": created.");
78     }
79
80   /**
81    * API method; not taken into account.
82    *
83    * @exception IllegalStateException If the producer is closed.
84    */

85   public void setDisableMessageID(boolean value) throws JMSException
86     {
87       if (closed)
88         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
89     }
90
91   /**
92    * API method.
93    *
94    * @exception IllegalStateException If the producer is closed.
95    * @exception JMSException When setting an invalid delivery mode.
96    */

97   public void setDeliveryMode(int deliveryMode) throws JMSException
98     {
99       if (closed)
100         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
101
102       if (deliveryMode != DeliveryMode.PERSISTENT
103           && deliveryMode != DeliveryMode.NON_PERSISTENT)
104         throw new JMSException("Can't set invalid delivery mode.");
105
106       this.deliveryMode = deliveryMode;
107     }
108
109   /**
110    * API method.
111    *
112    * @exception IllegalStateException If the producer is closed.
113    * @exception JMSException When setting an invalid priority.
114    */

115   public void setPriority(int priority) throws JMSException
116     {
117       if (closed)
118         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
119
120       if (priority < 0 || priority > 9)
121         throw new JMSException("Can't set invalid priority value.");
122
123       this.priority = priority;
124     }
125
126   /**
127    * API method.
128    *
129    * @exception IllegalStateException If the producer is closed.
130    */

131   public void setTimeToLive(long timeToLive) throws JMSException
132     {
133       if (closed)
134         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
135
136       this.timeToLive = timeToLive;
137     }
138
139   /**
140    * API method.
141    *
142    * @exception IllegalStateException If the producer is closed.
143    */

144   public void setDisableMessageTimestamp(boolean value) throws JMSException
145     {
146       if (closed)
147         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
148
149       this.timestampDisabled = value;
150     }
151
152   /**
153    * API method.
154    *
155    * @exception IllegalStateException If the producer is closed.
156    */

157   public Destination getDestination() throws JMSException
158     {
159       if (closed)
160         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
161
162       return dest;
163     }
164
165   /**
166    * API method.
167    *
168    * @exception IllegalStateException If the producer is closed.
169    */

170   public boolean getDisableMessageID() throws JMSException
171     {
172       if (closed)
173         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
174
175       return messageIDDisabled;
176     }
177
178   /**
179    * API method.
180    *
181    * @exception IllegalStateException If the producer is closed.
182    */

183   public int getDeliveryMode() throws JMSException
184     {
185       if (closed)
186         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
187
188       return deliveryMode;
189     }
190
191   /**
192    * API method.
193    *
194    * @exception IllegalStateException If the producer is closed.
195    */

196   public int getPriority() throws JMSException
197     {
198       if (closed)
199         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
200
201       return priority;
202     }
203
204   /**
205    * API method.
206    *
207    * @exception IllegalStateException If the producer is closed.
208    */

209   public long getTimeToLive() throws JMSException
210     {
211       if (closed)
212         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
213
214       return timeToLive;
215     }
216
217   /**
218    * API method.
219    *
220    * @exception IllegalStateException If the producer is closed.
221    */

222   public boolean getDisableMessageTimestamp() throws JMSException
223     {
224       if (closed)
225         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
226
227       return timestampDisabled;
228     }
229
230
231   /**
232    * Sends a message with the default delivery parameters.
233    *
234    * @exception UnsupportedOperationException If the dest is unidentified.
235    * @exception IllegalStateException If the producer is closed, or if the
236    * connection is broken.
237    * @exception JMSException If the request fails for any other reason.
238    */

239   public void send(Message message) throws JMSException
240     {
241       if (! identified)
242         throw new RuntimeException JavaDoc("Can't send message to"
243                                    + " an unidentified"
244                                    + " destination.");
245       // Actually producing it:
246
doSend(dest, message, deliveryMode, priority, timeToLive);
247     }
248
249   /**
250    * Sends a message with given delivery parameters.
251    *
252    * @exception UnsupportedOperationException If the dest is unidentified.
253    * @exception IllegalStateException If the producer is closed, or if the
254    * connection is broken.
255    * @exception JMSException If the request fails for any other reason.
256    */

257   public void send(Message message, int deliveryMode,
258                    int priority, long timeToLive) throws JMSException
259     {
260       if (! identified)
261         throw new RuntimeException JavaDoc("Can't send message to"
262                                    + " an unidentified"
263                                    + " destination.");
264       // Actually producing it:
265
doSend(dest, message, deliveryMode, priority, timeToLive);
266     }
267
268   /**
269    * Sends a message with default delivery parameters for an unidentified
270    * message producer.
271    *
272    * @exception UnsupportedOperationException When the producer did not
273    * properly identify itself.
274    * @exception JMSSecurityException If the user if not a WRITER on the
275    * specified destination.
276    * @exception IllegalStateException If the producer is closed, or if the
277    * connection is broken.
278    * @exception JMSException If the request fails for any other reason.
279    */

280   public void send(Destination dest,
281                    Message message) throws JMSException
282     {
283       if (identified)
284         throw new RuntimeException JavaDoc("An unidentified message"
285                                    + " producer can't use this"
286                                    + " identified message"
287                                    + " producer.");
288       if (dest == null)
289         throw new RuntimeException JavaDoc("Can't send message to"
290                                    + " an unidentified"
291                                    + " destination.");
292       
293       doSend((Destination) dest, message, deliveryMode, priority, timeToLive);
294     }
295
296   /**
297    * Sends a message with given delivery parameters for an unidentified
298    * message producer.
299    *
300    * @exception UnsupportedOperationException When the producer did not
301    * properly identify itself.
302    * @exception JMSSecurityException If the user if not a WRITER on the
303    * specified destination.
304    * @exception IllegalStateException If the producer is closed, or if the
305    * connection is broken.
306    * @exception JMSException If the request fails for any other reason.
307    */

308   public void send(Destination dest, Message message,
309                    int deliveryMode, int priority,
310                    long timeToLive) throws JMSException
311     {
312       if (identified)
313         throw new RuntimeException JavaDoc("An unidentified message"
314                                    + " producer can't use this"
315                                    + " identified message"
316                                    + " producer.");
317       if (dest == null)
318         throw new RuntimeException JavaDoc("Can't send message to"
319                                    + " an unidentified"
320                                    + " destination.");
321
322       doSend((Destination) dest, message, deliveryMode, priority, timeToLive);
323     }
324
325   /**
326    * API method.
327    *
328    * @exception JMSException Actually never thrown.
329    */

330   public void close() throws JMSException
331     {
332       // Ignoring call if producer is already closed:
333
if (closed)
334         return;
335
336       if (JoramTracing.dbgClient)
337         JoramTracing.log(JoramTracing.DEBUG, "--- " + this
338                          + ": closing...");
339
340       sess.producers.removeElement(this);
341       closed = true;
342
343       if (JoramTracing.dbgClient)
344         JoramTracing.log(JoramTracing.DEBUG, this + ": closed.");
345
346     }
347
348   /**
349    * Actually sends a message to a given destination.
350    *
351    * @exception MessageFormatException If the message to send is invalid.
352    * @exception InvalidDestinationException If the specified destination is
353    * invalid.
354    * @exception IllegalStateException If the connection is broken.
355    * @exception JMSException If the request fails for any other reason.
356    */

357   private void doSend(Destination dest, Message message,
358                       int deliveryMode, int priority,
359                       long timeToLive) throws JMSException
360     {
361       if (closed)
362         throw new IllegalStateException JavaDoc("Forbidden call on a closed producer.");
363
364       if (JoramTracing.dbgClient)
365         JoramTracing.log(JoramTracing.DEBUG, "--- " + this
366                          + ": producing...");
367
368       // Updating the message property fields:
369
String JavaDoc msgID = sess.cnx.nextMessageId();
370       message.setJMSMessageID(msgID);
371       message.setJMSDeliveryMode(deliveryMode);
372       message.setJMSDestination(dest);
373       if (timeToLive == 0)
374         message.setJMSExpiration(0);
375       else
376         message.setJMSExpiration(System.currentTimeMillis() + timeToLive);
377       message.setJMSPriority(priority);
378       if (! timestampDisabled)
379         message.setJMSTimestamp(System.currentTimeMillis());
380
381       com.scalagent.kjoram.messages.Message momMsg = null;
382       // If the message to send is a proprietary one, getting the MOM message
383
// it wraps:
384
if (message instanceof Message)
385         momMsg = ((Message) message).getMomMessage();
386
387       // If the message to send is a non proprietary JMS message, building
388
// a proprietary message and then getting the MOM message it wraps:
389
else if (message instanceof Message) {
390         try {
391           Message joramMessage = Message.convertJMSMessage(message);
392           momMsg = joramMessage.getMomMessage();
393         }
394         catch (JMSException jE) {
395           MessageFormatException mE = new MessageFormatException("Message to"
396                                                                  + " send is"
397                                                                  + " invalid.");
398           mE.setLinkedException(jE);
399           throw mE;
400         }
401       }
402       else {
403         MessageFormatException mE = new MessageFormatException("Message to"
404                                                                + " send is"
405                                                                + " invalid.");
406         throw mE;
407       }
408
409       // If the session is transacted, keeping the request for later delivery:
410
if (sess.transacted) {
411         if (JoramTracing.dbgClient)
412           JoramTracing.log(JoramTracing.DEBUG, "Buffering the message.");
413
414         sess.prepareSend(dest,
415                          (com.scalagent.kjoram.messages.Message) momMsg.clone());
416       }
417       // If not, building a new request and sending it:
418
else {
419       ProducerMessages pM = new ProducerMessages(dest.getName(), momMsg);
420
421       if (JoramTracing.dbgClient)
422         JoramTracing.log(JoramTracing.DEBUG, "Sending " + momMsg);
423       
424         sess.cnx.syncRequest(pM);
425       }
426     }
427 }
428
Popular Tags