KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > session > MessageProducerImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.session;
30
31 import com.caucho.jms.AbstractDestination;
32 import com.caucho.jms.message.MessageImpl;
33 import com.caucho.util.Alarm;
34 import com.caucho.util.L10N;
35
36 import javax.jms.DeliveryMode JavaDoc;
37 import javax.jms.Destination JavaDoc;
38 import javax.jms.JMSException JavaDoc;
39 import javax.jms.Message JavaDoc;
40 import javax.jms.MessageProducer JavaDoc;
41
42 /**
43  * A basic message producer.
44  */

45 public class MessageProducerImpl implements MessageProducer JavaDoc {
46   static final L10N L = new L10N(MessageProducer JavaDoc.class);
47
48   private int _deliveryMode = DeliveryMode.NON_PERSISTENT;
49   private boolean _disableMessageId = true;
50   private boolean _disableMessageTimestamp = true;
51   private int _priority = 4;
52   private long _timeToLive = 30 * 24 * 3600 * 1000L;
53
54   protected SessionImpl _session;
55   protected AbstractDestination _destination;
56
57   public MessageProducerImpl(SessionImpl session, Destination destination)
58   {
59     _session = session;
60     _destination = (AbstractDestination) destination;
61   }
62
63   /**
64    * Returns the producer's destination.
65    */

66   public Destination getDestination()
67   {
68     return _destination;
69   }
70
71   /**
72    * Returns the default delivery mode.
73    */

74   public int getDeliveryMode()
75   {
76     return _deliveryMode;
77   }
78
79   /**
80    * Sets the default delivery mode.
81    */

82   public void setDeliveryMode(int deliveryMode)
83   {
84     _deliveryMode = deliveryMode;
85   }
86
87   /**
88    * Returns true if message ids are disabled by default.
89    */

90   public boolean getDisableMessageID()
91   {
92     return _disableMessageId;
93   }
94
95   /**
96    * Sets true if message ids should be disabled by default.
97    */

98   public void setDisableMessageID(boolean disable)
99   {
100     _disableMessageId = disable;
101   }
102
103   /**
104    * Returns true if message timestamps are disabled by default.
105    */

106   public boolean getDisableMessageTimestamp()
107   {
108     return _disableMessageTimestamp;
109   }
110
111   /**
112    * Sets true if message timestamps should be disabled by default.
113    */

114   public void setDisableMessageTimestamp(boolean disable)
115   {
116     _disableMessageTimestamp = disable;
117   }
118
119   /**
120    * Returns the default priority
121    */

122   public int getPriority()
123   {
124     return _priority;
125   }
126
127   /**
128    * Sets the default priority.
129    */

130   public void setPriority(int priority)
131   {
132     _priority = priority;
133   }
134
135   /**
136    * Returns the default time to live
137    */

138   public long getTimeToLive()
139   {
140     return _timeToLive;
141   }
142
143   /**
144    * Sets the default time to live.
145    */

146   public void setTimeToLive(long timeToLive)
147   {
148     _timeToLive = timeToLive;
149   }
150
151   /**
152    * Sends a message to the destination
153    *
154    * @param message the message to send
155    */

156   public void send(Message JavaDoc message)
157     throws JMSException JavaDoc
158   {
159     send(_destination, message,
160      _deliveryMode, _priority, _timeToLive);
161   }
162   
163   /**
164    * Sends a message to the destination
165    *
166    * @param message the message to send
167    * @param deliveryMode the delivery mode
168    * @param priority the priority
169    * @param timeToLive how long the message should live
170    */

171   public void send(Message JavaDoc message,
172                    int deliveryMode,
173                    int priority,
174                    long timeToLive)
175     throws JMSException JavaDoc
176   {
177     send(_destination, message,
178      deliveryMode, priority, timeToLive);
179   }
180
181   /**
182    * Sends a message to the destination
183    *
184    * @param destination the destination the message should be send to
185    * @param message the message to send
186    */

187   public void send(Destination destination, Message JavaDoc message)
188     throws JMSException JavaDoc
189   {
190     send(destination, message,
191      _deliveryMode, _priority, _timeToLive);
192   }
193   
194   /**
195    * Sends a message to the destination
196    *
197    * @param destination the destination the message should be send to
198    * @param message the message to send
199    * @param deliveryMode the delivery mode
200    * @param priority the priority
201    * @param timeToLive how long the message should live
202    */

203   public void send(Destination destination,
204                    Message JavaDoc message,
205                    int deliveryMode,
206                    int priority,
207                    long timeToLive)
208     throws JMSException JavaDoc
209   {
210     _session.send((AbstractDestination) destination,
211           (MessageImpl) message,
212           deliveryMode, priority,
213           calculateExpiration(timeToLive));
214     // _session.checkThread();
215
}
216
217   /**
218    * Calculates the expires time.
219    */

220   protected long calculateExpiration(long timeToLive)
221   {
222     if (timeToLive <= 0)
223       return timeToLive;
224     else
225       return timeToLive + Alarm.getCurrentTime();
226   }
227
228   /**
229    * Closes the producer.
230    */

231   public void close()
232     throws JMSException JavaDoc
233   {
234   }
235 }
236
237
Popular Tags