Source code: com/presumo/jms/client/JmsMessageProducer.java
1 /**
2 * This file is part of Presumo.
3 *
4 * Presumo is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * Presumo is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Presumo; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 *
19 * Copyright 2001 Dan Greff
20 */
21 package com.presumo.jms.client;
22
23 import com.presumo.jms.message.JmsMessage;
24 import com.presumo.jms.message.SystemMessageConstants;
25
26 import javax.jms.Destination;
27 import javax.jms.DeliveryMode;
28 import javax.jms.IllegalStateException;
29 import javax.jms.JMSException;
30 import javax.jms.Message;
31 import javax.jms.MessageFormatException;
32 import javax.jms.MessageProducer;
33
34
35 /**
36 * Implementation of the interface <code>javax.jms.MessageProducer</code>.
37 * <code>TopicPublisher</code>s and <code>QueueSender</code>s have pratically
38 * identical functionality in this implementation (actually the differences
39 * are just differred to the routing logic elsewhere). Consequently, almost
40 * all logic is in this base class.
41 *
42 * @author Dan Greff
43 */
44 public abstract class JmsMessageProducer implements MessageProducer
45 {
46 protected boolean disableMessageTimestamp;
47
48 // Make non-persistent messages the default
49 protected int deliveryMode = DeliveryMode.NON_PERSISTENT;
50 protected int priority = Message.DEFAULT_PRIORITY;
51 protected long timeToLive = Message.DEFAULT_TIME_TO_LIVE;
52
53 protected final JmsSession mySession;
54
55 ///////////////////////////////////////////////////////////////////////
56 // Constructors //
57 ///////////////////////////////////////////////////////////////////////
58
59 JmsMessageProducer(JmsSession session)
60 {
61 this.mySession = session;
62 session.addProducer(this);
63 }
64
65 /////////////////////////////////////////////////////////////////////////
66 // Public Methods //
67 /////////////////////////////////////////////////////////////////////////
68
69 /**
70 * This option is ignored because the internal routing mechanism is
71 * dependent on Message IDs.
72 */
73 public final void setDisableMessageID(boolean value) throws JMSException
74 {
75 }
76
77 /**
78 * Always returns false because message IDs may never be disabled due to
79 * the internal routing mechanism's dependance on this feature.
80 */
81 public final boolean getDisableMessageID() throws JMSException
82 {
83 return false;
84 }
85
86
87 public final void setDisableMessageTimestamp(boolean value) throws JMSException
88 {
89 this.disableMessageTimestamp = value;
90 }
91
92
93 public final boolean getDisableMessageTimestamp() throws JMSException
94 {
95 return disableMessageTimestamp;
96 }
97
98
99 public final void setDeliveryMode(int deliveryMode) throws JMSException
100 {
101 this.deliveryMode = deliveryMode;
102 }
103
104
105 public final int getDeliveryMode() throws JMSException
106 {
107 return deliveryMode;
108 }
109
110
111 public final void setPriority(int priority) throws JMSException
112 {
113 this.priority = priority;
114 }
115
116
117 public final int getPriority() throws JMSException
118 {
119 return priority;
120 }
121
122
123 public final void setTimeToLive(long timeToLive) throws JMSException
124 {
125 this.timeToLive = timeToLive;
126 }
127
128
129 public final long getTimeToLive() throws JMSException
130 {
131 return timeToLive;
132 }
133
134
135 public void close() throws JMSException
136 {
137 mySession.removeProducer(this);
138 }
139
140
141 /////////////////////////////////////////////////////////////////////////
142 // Protected Methods //
143 /////////////////////////////////////////////////////////////////////////
144
145 /**
146 * Sets all the necessary properties on the message before passing it off
147 * to this instance's session for routing.
148 */
149 protected final void prepareAndSend(Destination destination,
150 Message message,
151 int _deliveryMode,
152 int _priority,
153 long _timeToLive) throws JMSException
154 {
155 if (message == null)
156 throw new MessageFormatException("Attempted to send null message");
157
158 // TODO:: support any provieder's implementation of Message
159 if (! (message instanceof JmsMessage))
160 throw new MessageFormatException
161 ("Message was not generated by this JMS implementation: " + message.toString());
162
163 // Prepare the message
164 JmsMessage messageToSend = (JmsMessage) message;
165 messageToSend.setJMSDestination(destination);
166
167 // Set so that noLocal filtering works for TopicPublishers.
168 // Messages from remote clients will not have this value set.
169 messageToSend.setLocal(true);
170
171 //if (deliveryMode != Message.DEFAULT_DELIVERY_MODE)
172 messageToSend.setJMSDeliveryMode(_deliveryMode);
173 if (priority != Message.DEFAULT_PRIORITY)
174 messageToSend.setJMSPriority(_priority);
175
176 if (!this.disableMessageTimestamp) {
177 // Do timestamp work
178 // TODO::
179 //long timestamp = JmsMessage.createTimestamp();
180 //messageToSend.setJMSTimestamp(timestamp);
181
182 //if (timeToLive != Message.DEFAULT_TIME_TO_LIVE) {
183 // messageToSend.setJMSExpiration(timestamp + timeToLive);
184 //}
185 }
186
187 // Just in case somebody tries to set the system message property
188 // to something else always set it back to a USER_MSG.
189 messageToSend.setJMSSystemMsgType(SystemMessageConstants.USER_MSG);
190
191 mySession.send(messageToSend);
192 }
193
194 }
195
196
197
198