KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > SomniMessageProducer


1 package net.walend.somnifugi;
2
3 import javax.jms.MessageProducer JavaDoc;
4 import javax.jms.JMSException JavaDoc;
5 import javax.jms.DeliveryMode JavaDoc;
6 import javax.jms.Message JavaDoc;
7 import javax.jms.Destination JavaDoc;
8
9 /**
10 SomniMessageProducer is an abstarct superclass for things that take in Messages to deliver elsewhere. Each SomniMessageProducer has a specific SomniDestination.
11
12 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
13  */

14
15 public abstract class SomniMessageProducer
16     implements MessageProducer JavaDoc
17 {
18     private boolean useMessageIDs = true;
19     private boolean useTimestamps = true;
20     private int defaultPriority = 0;
21     private long timeToLive = 0;
22     private SomniDestination destination;
23     private String JavaDoc connectionClientID;
24     private String JavaDoc name;
25
26     private boolean closed = false;
27    
28     protected final Object JavaDoc guard = new Object JavaDoc();
29
30     protected SomniMessageProducer(SomniDestination destination,String JavaDoc name,String JavaDoc connectionClientID)
31     {
32         this.destination = destination;
33         this.name = name;
34         this.connectionClientID = connectionClientID;
35     }
36
37     public boolean isClosed()
38     {
39         synchronized(guard)
40             {
41                 return closed;
42             }
43     }
44
45     /**
46 Sets whether message IDs are disabled.
47  
48 <P>Since message IDs take some effort to create and increase a
49 message's size, some JMS providers may be able to optimize message
50 overhead if they are given a hint that the message ID is not used by
51 an application. By calling the <CODE>setDisableMessageID</CODE>
52 method on this message producer, a JMS client enables this potential
53 optimization for all messages sent by this message producer. If the JMS
54 provider accepts this hint,
55 these messages must have the message ID set to null; if the provider
56 ignores the hint, the message ID must be set to its normal unique value.
57       *
58 <P>Message IDs are enabled by default.
59       *
60 @param value indicates if message IDs are disabled
61  
62 @exception JMSException if the JMS provider fails to set message ID to
63                         disabled due to some internal error.
64       */

65     public void setDisableMessageID(boolean value)
66         throws JMSException JavaDoc
67     {
68         synchronized(guard)
69             {
70                 useMessageIDs = !value;
71             }
72     }
73
74     /** Gets an indication of whether message IDs are disabled.
75  
76 @return an indication of whether message IDs are disabled
77  
78 @exception JMSException if the JMS provider fails to determine if
79                         message IDs are disabled due to some internal
80                         error.
81       */

82     public boolean getDisableMessageID()
83         throws JMSException JavaDoc
84     {
85         synchronized(guard)
86             {
87                 return !useMessageIDs;
88             }
89     }
90
91     /** Sets whether message timestamps are disabled.
92  
93 <P>Since timestamps take some effort to create and increase a
94 message's size, some JMS providers may be able to optimize message
95 overhead if they are given a hint that the timestamp is not used by an
96 application. By calling the <CODE>setDisableMessageTimestamp</CODE>
97 method on this message producer, a JMS client enables this potential
98 optimization for all messages sent by this message producer. If the
99 JMS provider accepts this hint,
100 these messages must have the timestamp set to zero; if the provider
101 ignores the hint, the timestamp must be set to its normal value.
102  
103 <P>Message timestamps are enabled by default.
104       *
105 @param value indicates if message timestamps are disabled
106  
107 @exception JMSException if the JMS provider fails to set timestamps to
108                         disabled due to some internal error.
109       */

110     public void setDisableMessageTimestamp(boolean value)
111         throws JMSException JavaDoc
112     {
113         synchronized(guard)
114             {
115                 useTimestamps = !value;
116             }
117     }
118
119     /** Gets an indication of whether message timestamps are disabled.
120  
121 @return an indication of whether message timestamps are disabled
122  
123 @exception JMSException if the JMS provider fails to determine if
124                         timestamps are disabled due to some internal
125                         error.
126       */

127     public boolean getDisableMessageTimestamp()
128         throws JMSException JavaDoc
129     {
130         synchronized(guard)
131             {
132                 return !useTimestamps;
133             }
134     }
135
136     /** Sets the producer's default delivery mode.
137  
138 <P>Delivery mode is set to <CODE>PERSISTENT</CODE> by default.
139       *
140 @param deliveryMode the message delivery mode for this message
141 producer; legal values are <code>DeliveryMode.NON_PERSISTENT</code>
142 and <code>DeliveryMode.PERSISTENT</code>
143  
144 @exception JMSException if the JMS provider fails to set the delivery
145                         mode due to some internal error.
146       *
147 @see javax.jms.MessageProducer#getDeliveryMode
148 @see javax.jms.DeliveryMode#NON_PERSISTENT
149 @see javax.jms.DeliveryMode#PERSISTENT
150 @see javax.jms.Message#DEFAULT_DELIVERY_MODE
151       */

152     public void setDeliveryMode(int deliveryMode)
153         throws JMSException JavaDoc
154     {
155         synchronized(guard)
156             {
157                 if(deliveryMode!=DeliveryMode.NON_PERSISTENT)
158                     {
159                         throw new IllegalStateException JavaDoc("DeliveryMode can only be DeliveryMode.NON_PERSISTENT.");
160                     }
161             }
162     }
163
164     /** Gets the producer's default delivery mode.
165  
166 @return the message delivery mode for this message producer
167  
168 @exception JMSException if the JMS provider fails to get the delivery
169                         mode due to some internal error.
170       *
171 @see javax.jms.MessageProducer#setDeliveryMode
172       */

173     public int getDeliveryMode()
174         throws JMSException JavaDoc
175     {
176         synchronized(guard)
177             {
178                 return DeliveryMode.NON_PERSISTENT;
179             }
180     }
181
182     /** Sets the producer's default priority.
183  
184 <P>The JMS API defines ten levels of priority value, with 0 as the
185 lowest priority and 9 as the highest. Clients should consider priorities
186 0-4 as gradations of normal priority and priorities 5-9 as gradations
187 of expedited priority. Priority is set to 4 by default.
188       *
189 @param defaultPriority the message priority for this message producer;
190                        must be a value between 0 and 9
191
192  
193 @exception JMSException if the JMS provider fails to set the priority
194                         due to some internal error.
195       *
196 @see javax.jms.MessageProducer#getPriority
197 @see javax.jms.Message#DEFAULT_PRIORITY
198       */

199     public void setPriority(int defaultPriority)
200         throws JMSException JavaDoc
201     {
202         if((defaultPriority<0)||(defaultPriority>9))
203             {
204                 throw new IllegalStateException JavaDoc("defaultPriority must be between 0 and 9, not "+defaultPriority);
205             }
206         synchronized(guard)
207             {
208                 this.defaultPriority = defaultPriority;
209             }
210     }
211
212     /** Gets the producer's default priority.
213  
214 @return the message priority for this message producer
215  
216 @exception JMSException if the JMS provider fails to get the priority
217                         due to some internal error.
218       *
219 @see javax.jms.MessageProducer#setPriority
220       */

221     public int getPriority()
222         throws JMSException JavaDoc
223     {
224         synchronized(guard)
225             {
226                 return defaultPriority;
227             }
228     }
229
230     /** Sets the default length of time in milliseconds from its dispatch time
231 that a produced message should be retained by the message system.
232       *
233 <P>Time to live is set to zero by default.
234       *
235 @param timeToLive the message time to live in milliseconds; zero is
236 unlimited
237       *
238 @exception JMSException if the JMS provider fails to set the time to
239                         live due to some internal error.
240       *
241 @see javax.jms.MessageProducer#getTimeToLive
242 @see javax.jms.Message#DEFAULT_TIME_TO_LIVE
243       */

244     public void setTimeToLive(long timeToLive)
245         throws JMSException JavaDoc
246     {
247         if(timeToLive < 0)
248             {
249                 throw new IllegalStateException JavaDoc("timeToLive must be 0 or larger, not "+timeToLive);
250             }
251         synchronized(guard)
252             {
253                 this.timeToLive = timeToLive;
254             }
255     }
256
257     /** Gets the default length of time in milliseconds from its dispatch time
258 that a produced message should be retained by the message system.
259       *
260 @return the message time to live in milliseconds; zero is unlimited
261       *
262 @exception JMSException if the JMS provider fails to get the time to
263                         live due to some internal error.
264       *
265 @see javax.jms.MessageProducer#setTimeToLive
266       */

267     public long getTimeToLive()
268         throws JMSException JavaDoc
269     {
270         synchronized(guard)
271             {
272                 return timeToLive;
273             }
274     }
275
276     /** Gets the destination associated with this <CODE>MessageProducer</CODE>.
277       *
278       * @return this producer's <CODE>Destination/<CODE>
279       *
280       * @exception JMSException if the JMS provider fails to get the destination for
281       * this <CODE>MessageProducer</CODE>
282       * due to some internal error.
283       *@since 1.1
284       */

285     
286     public abstract Destination JavaDoc getDestination() throws JMSException JavaDoc;
287     
288     /** Closes the message producer.
289       *
290 <P>Since a provider may allocate some resources on behalf of a
291 <CODE>MessageProducer</CODE> outside the Java virtual machine, clients
292 should close them when they
293 are not needed. Relying on garbage collection to eventually reclaim
294 these resources may not be timely enough.
295  
296 @exception JMSException if the JMS provider fails to close the producer
297                         due to some internal error.
298       */

299     public void close()
300         throws JMSException JavaDoc
301     {
302         synchronized(guard)
303             {
304                 closed=true;
305                 SomniLogger.IT.finer(name+" closed.");
306             }
307     }
308
309     private static final String JavaDoc PREFIX = "ID:";
310     private int counter = 0;
311
312     private String JavaDoc createMessageID(int counterValue)
313     {
314         StringBuffer JavaDoc buffy = new StringBuffer JavaDoc();
315         buffy.append(PREFIX);
316         buffy.append(name);
317         buffy.append(":"+counterValue);
318
319         return buffy.toString();
320     }
321
322     protected void setMessageDetails(SomniMessage message,int deliveryMode,int priority,long timeToLive)
323         throws JMSException JavaDoc
324     {
325         if(useMessageIDs)
326             {
327                 message.setSomniProducerCount(counter);
328                 counter++;
329                 message.setJMSMessageID(createMessageID(counter));
330             }
331         else
332             {
333                 message.setJMSMessageID(null);
334             }
335         if(useTimestamps)
336             {
337                 message.setJMSTimestamp(System.currentTimeMillis());
338                 if(timeToLive>0)
339                     {
340                         message.setJMSExpiration(message.getJMSTimestamp()+timeToLive);
341                     }
342                 else
343                     {
344                         message.setJMSExpiration(0);
345                     }
346             }
347         else
348             {
349                 message.setJMSTimestamp(0);
350                 message.setJMSExpiration(0);
351             }
352         message.setJMSDeliveryMode(deliveryMode);
353         if(message.getJMSPriority() < priority)
354         {
355             message.setJMSPriority(priority);
356         }
357         message.setJMSDestination(destination);
358         message.setSomniProducerConnectionClientID(connectionClientID);
359         //reconciled 3.9 and 3.10 by letting 3.10 win.
360
message.setReadOnly();
361     }
362
363     protected void logSent(Message JavaDoc message,String JavaDoc sendOrPublish)
364     {
365         if(message!=null)
366             {
367                 //todo is logging an Object possible?
368
StringBuffer JavaDoc buffy = new StringBuffer JavaDoc();
369                 
370                 buffy.append(name);
371                 buffy.append(" ");
372                 buffy.append(sendOrPublish);
373                 buffy.append(" ");
374                 buffy.append(message.toString());
375                 
376                 SomniLogger.IT.finest(buffy.toString());
377             }
378     }
379
380     protected String JavaDoc getName()
381     {
382         return name;
383     }
384
385 }
386
387 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
388 All rights reserved.
389
390 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
391
392 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
393
394 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
395
396 Neither the name of the SomnifugiJMS Project, walend.net, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission from David Walend.
397
398 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
399
400 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
401 The net.walend.somnifugi.sql92 package is modified code from the openmq project, https://mq.dev.java.net/ , Copyright (c) of Sun, and carries the CDDL license, repeated here: You can obtain a copy of the license at https://glassfish.dev.java.net/public/CDDLv1.0.html. See the License for the specific language governing permissions and limitations under the License.
402
403 =================================================================================
404
405 For more information and the latest version of this software, please see http://somnifugi.sourceforge.net and http://walend.net or email <a HREF="mailto:david@walend.net">david@walend.net</a>.
406  */

407
Popular Tags