001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jms.connection;
031:
032: import com.caucho.jms.message.MessageImpl;
033: import com.caucho.jms.queue.*;
034: import com.caucho.util.Alarm;
035: import com.caucho.util.L10N;
036:
037: import javax.jms.DeliveryMode;
038: import javax.jms.Destination;
039: import javax.jms.JMSException;
040: import javax.jms.Message;
041: import javax.jms.MessageProducer;
042:
043: /**
044: * A basic message producer.
045: */
046: public class MessageProducerImpl implements MessageProducer {
047: static final L10N L = new L10N(MessageProducer.class);
048:
049: private int _deliveryMode = DeliveryMode.PERSISTENT;
050: private boolean _disableMessageId = true;
051: private boolean _disableMessageTimestamp = true;
052: private int _priority = 4;
053: private long _timeToLive = 30 * 24 * 3600 * 1000L;
054:
055: protected JmsSession _session;
056: protected AbstractDestination _queue;
057:
058: public MessageProducerImpl(JmsSession session,
059: AbstractDestination queue) {
060: _session = session;
061: _queue = queue;
062: }
063:
064: /**
065: * Returns the producer's destination.
066: */
067: public Destination getDestination() throws JMSException {
068: if (_session == null || _session.isClosed())
069: throw new javax.jms.IllegalStateException(L
070: .l("getDestination(): message producer is closed."));
071:
072: return _queue;
073: }
074:
075: /**
076: * Returns the default delivery mode.
077: */
078: public int getDeliveryMode() throws JMSException {
079: if (_session == null || _session.isClosed())
080: throw new javax.jms.IllegalStateException(
081: L
082: .l("getDeliveryMode(): message producer is closed."));
083:
084: return _deliveryMode;
085: }
086:
087: /**
088: * Sets the default delivery mode.
089: */
090: public void setDeliveryMode(int deliveryMode) throws JMSException {
091: if (_session == null || _session.isClosed())
092: throw new javax.jms.IllegalStateException(
093: L
094: .l("setDeliveryMode(): message producer is closed."));
095:
096: _deliveryMode = deliveryMode;
097: }
098:
099: /**
100: * Returns true if message ids are disabled by default.
101: */
102: public boolean getDisableMessageID() throws JMSException {
103: if (_session == null || _session.isClosed())
104: throw new javax.jms.IllegalStateException(
105: L
106: .l("getDisableMessageID(): message producer is closed."));
107:
108: return _disableMessageId;
109: }
110:
111: /**
112: * Sets true if message ids should be disabled by default.
113: */
114: public void setDisableMessageID(boolean disable)
115: throws JMSException {
116: if (_session == null || _session.isClosed())
117: throw new javax.jms.IllegalStateException(
118: L
119: .l("setDisableMessageID(): message producer is closed."));
120:
121: _disableMessageId = disable;
122: }
123:
124: /**
125: * Returns true if message timestamps are disabled by default.
126: */
127: public boolean getDisableMessageTimestamp() throws JMSException {
128: if (_session == null || _session.isClosed())
129: throw new javax.jms.IllegalStateException(
130: L
131: .l("getDisableMessageTimestam(): message producer is closed."));
132:
133: return _disableMessageTimestamp;
134: }
135:
136: /**
137: * Sets true if message timestamps should be disabled by default.
138: */
139: public void setDisableMessageTimestamp(boolean disable)
140: throws JMSException {
141: if (_session == null || _session.isClosed())
142: throw new javax.jms.IllegalStateException(
143: L
144: .l("setDeliveryMode(): message producer is closed."));
145:
146: _disableMessageTimestamp = disable;
147: }
148:
149: /**
150: * Returns the default priority
151: */
152: public int getPriority() throws JMSException {
153: if (_session == null || _session.isClosed())
154: throw new javax.jms.IllegalStateException(L
155: .l("getPriority(): message producer is closed."));
156:
157: return _priority;
158: }
159:
160: /**
161: * Sets the default priority.
162: */
163: public void setPriority(int priority) throws JMSException {
164: if (_session == null || _session.isClosed())
165: throw new javax.jms.IllegalStateException(
166: L
167: .l("setDeliveryMode(): message producer is closed."));
168:
169: _priority = priority;
170: }
171:
172: /**
173: * Returns the default time to live
174: */
175: public long getTimeToLive() throws JMSException {
176: if (_session == null || _session.isClosed())
177: throw new javax.jms.IllegalStateException(L
178: .l("getTimeToLive(): message producer is closed."));
179:
180: return _timeToLive;
181: }
182:
183: /**
184: * Sets the default time to live.
185: */
186: public void setTimeToLive(long timeToLive) throws JMSException {
187: if (_session == null || _session.isClosed())
188: throw new javax.jms.IllegalStateException(L
189: .l("setTimeToLive(): message producer is closed."));
190:
191: _timeToLive = timeToLive;
192: }
193:
194: /**
195: * Sends a message to the destination
196: *
197: * @param message the message to send
198: */
199: public void send(Message message) throws JMSException {
200: if (message == null)
201: throw new NullPointerException(L
202: .l("jms message cannot be null for send()"));
203:
204: send(_queue, message, _deliveryMode, _priority, _timeToLive);
205: }
206:
207: /**
208: * Sends a message to the destination
209: *
210: * @param message the message to send
211: * @param deliveryMode the delivery mode
212: * @param priority the priority
213: * @param timeToLive how long the message should live
214: */
215: public void send(Message message, int deliveryMode, int priority,
216: long timeToLive) throws JMSException {
217: if (message == null)
218: throw new NullPointerException(L
219: .l("jms message cannot be null for send()"));
220:
221: send(_queue, message, deliveryMode, priority, timeToLive);
222: }
223:
224: /**
225: * Sends a message to the destination
226: *
227: * @param destination the destination the message should be send to
228: * @param message the message to send
229: */
230: public void send(Destination destination, Message message)
231: throws JMSException {
232: if (message == null)
233: throw new NullPointerException(L
234: .l("jms message cannot be null for send()"));
235: if (destination == null)
236: throw new NullPointerException(L
237: .l("jms destination cannot be null for send()"));
238:
239: send(destination, message, _deliveryMode, _priority,
240: _timeToLive);
241: }
242:
243: /**
244: * Sends a message to the destination
245: *
246: * @param destination the destination the message should be send to
247: * @param message the message to send
248: * @param deliveryMode the delivery mode
249: * @param priority the priority
250: * @param timeToLive how long the message should live
251: */
252: public void send(Destination destination, Message message,
253: int deliveryMode, int priority, long timeToLive)
254: throws JMSException {
255: if (destination == null)
256: destination = _queue;
257: else if (_queue != null && destination != _queue)
258: throw new UnsupportedOperationException(
259: L
260: .l(
261: "MessageProducer: '{0}' does not match the queue '{1}'",
262: destination, _queue));
263:
264: if (destination == null)
265: throw new UnsupportedOperationException(
266: L
267: .l("MessageProducer: null destination is not supported."));
268:
269: if (_session == null || _session.isClosed())
270: throw new javax.jms.IllegalStateException(
271: L
272: .l("getDeliveryMode(): message producer is closed."));
273:
274: _session.send((AbstractDestination) destination, message,
275: deliveryMode, priority, timeToLive);
276: // _session.checkThread();
277: }
278:
279: /**
280: * Calculates the expires time.
281: */
282: protected long calculateExpiration(long timeToLive) {
283: if (timeToLive <= 0)
284: return timeToLive;
285: else
286: return timeToLive + Alarm.getCurrentTime();
287: }
288:
289: /**
290: * Closes the producer.
291: */
292: public void close() throws JMSException {
293: _session = null;
294: }
295:
296: public String toString() {
297: return getClass().getName() + "[" + _queue + "]";
298: }
299: }
|