Source code: org/activemq/message/MessageAck.java
1 /**
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18
19 package org.activemq.message;
20
21 import org.activemq.service.MessageIdentity;
22
23 import java.io.DataInput;
24 import java.io.IOException;
25 import java.io.DataOutput;
26
27 /**
28 * Denotes an object that can be serialized/deserailized using a PacketReader/PacketWriter
29 */
30
31 public class MessageAck extends AbstractPacket implements BodyPacket{
32
33 public static final int MESSAGE_READ_INDEX = 2;
34 public static final int XA_TRANS_INDEX = 3;
35 public static final int PERSISTENT_INDEX = 4;
36 public static final int EXPIRED_INDEX = 5;
37 public static final int TRANSACTION_ID_INDEX = 6;
38 public static final int EXTERNAL_MESSAGE_ID_INDEX = 7;
39 public static final int CACHED_VALUES_INDEX = 8;
40 public static final int LONG_SEQUENCE_INDEX = 9;
41
42 private String consumerId;
43 private String messageID;
44 private ActiveMQDestination destination;
45 private Object transactionId;
46 private boolean messageRead;
47 private boolean xaTransacted;
48 private boolean persistent;
49 private boolean expired;
50 private short sessionId;
51 private long sequenceNumber;
52 private String producerKey;
53 private boolean externalMessageId;
54 private transient MessageIdentity messageIdentity;
55
56
57 /**
58 * Return the type of Packet
59 *
60 * @return integer representation of the type of Packet
61 */
62
63 public int getPacketType() {
64 return ACTIVEMQ_MSG_ACK;
65 }
66
67 /**
68 * @return pretty print of this Packet
69 */
70 public String toString() {
71 return super.toString() + " MessageAck{ " +
72 "consumerId = '" + consumerId + "' " +
73 ", messageID = '" + messageID + "' " +
74 ", destination = " + destination +
75 ", transactionId = '" + transactionId + "' " +
76 ", messageRead = " + messageRead +
77 ", xaTransacted = " + xaTransacted +
78 ", persistent = " + persistent +
79 ", expired = " + expired +
80 ", messageIdentity = " + messageIdentity +
81 " }";
82 }
83
84
85 /**
86 * @return Returns the transactionId.
87 *
88 * @Transient
89 */
90 public Object getTransactionId() {
91 return this.transactionId;
92 }
93
94 /**
95 * @param newTransactionId The transactionId to set.
96 *
97 * @Transient
98 */
99 public void setTransactionId(Object newTransactionId) {
100 this.transactionId = newTransactionId;
101 this.xaTransacted = newTransactionId!=null && newTransactionId.getClass()==ActiveMQXid.class;
102 }
103
104
105 public void readBody(DataInput dataIn) throws IOException {
106 setTransactionIDString(dataIn.readUTF());
107 }
108
109 public void writeBody(DataOutput dataOut) throws IOException {
110 dataOut.writeUTF(getTransactionIDString());
111 }
112
113 /**
114 * @return Returns true if this message is part of a transaction
115 */
116
117 public boolean isPartOfTransaction() {
118 return this.transactionId != null;
119 }
120
121
122 /**
123 * @return the messageId
124 */
125
126 public String getMessageID() {
127 if (messageID == null && producerKey != null){
128 messageID = producerKey + sequenceNumber;
129 }
130 return messageID;
131 }
132
133 /**
134 * @param messageID The messageID to set.
135 */
136 public void setMessageID(String messageID) {
137 this.messageID = messageID;
138 this.messageIdentity=null;
139 }
140
141 /**
142 * @return Returns the messageRead.
143 */
144 public boolean isMessageRead() {
145 return messageRead;
146 }
147
148 /**
149 * @param messageRead The messageRead to set.
150 */
151 public void setMessageRead(boolean messageRead) {
152 this.messageRead = messageRead;
153 }
154
155 /**
156 * @return Returns the consumerId.
157 */
158 public String getConsumerId() {
159 return consumerId;
160 }
161
162 /**
163 * @param consumerId The consumerId to set.
164 */
165 public void setConsumerId(String consumerId) {
166 this.consumerId = consumerId;
167 }
168
169 /**
170 * @return Returns the xaTransacted.
171 */
172 public boolean isXaTransacted() {
173 return xaTransacted;
174 }
175
176 public MessageIdentity getMessageIdentity() {
177 if (messageIdentity == null) {
178 messageIdentity = new MessageIdentity(getMessageID());
179 }
180 return messageIdentity;
181 }
182 /**
183 * @return Returns the destination.
184 */
185 public ActiveMQDestination getDestination() {
186 return destination;
187 }
188 /**
189 * @param destination The destination to set.
190 */
191 public void setDestination(ActiveMQDestination destination) {
192 this.destination = destination;
193 }
194 /**
195 * @return Returns the persistent.
196 */
197 public boolean isPersistent() {
198 return persistent;
199 }
200 /**
201 * @param persistent The persistent to set.
202 */
203 public void setPersistent(boolean persistent) {
204 this.persistent = persistent;
205 }
206
207 /**
208 * @return true the delivered message was to a non-persistent destination
209 */
210 public boolean isTemporary(){
211 return persistent == false || (destination != null && destination.isTemporary());
212 }
213
214 /**
215 * @return Returns the expired.
216 */
217 public boolean isExpired() {
218 return expired;
219 }
220 /**
221 * @param expired The expired to set.
222 */
223 public void setExpired(boolean expired) {
224 this.expired = expired;
225 }
226
227 /**
228 * @return Returns the producerKey.
229 */
230 public String getProducerKey() {
231 return producerKey;
232 }
233 /**
234 * @param producerKey The producerKey to set.
235 */
236 public void setProducerKey(String producerKey) {
237 this.producerKey = producerKey;
238 }
239
240 /**
241 * @return Returns the messageSequence.
242 */
243 public long getSequenceNumber() {
244 return sequenceNumber;
245 }
246 /**
247 * @param messageSequence The messageSequence to set.
248 */
249 public void setSequenceNumber(long messageSequence) {
250 this.sequenceNumber = messageSequence;
251 }
252 /**
253 * @return Returns the sessionId.
254 */
255 public short getSessionId() {
256 return sessionId;
257 }
258 /**
259 * @param sessionId The sessionId to set.
260 */
261 public void setSessionId(short sessionId) {
262 this.sessionId = sessionId;
263 }
264 /**
265 * @return Returns the externalMessageId.
266 */
267 public boolean isExternalMessageId() {
268 return externalMessageId;
269 }
270 /**
271 * @param externalMessageId The externalMessageId to set.
272 */
273 public void setExternalMessageId(boolean externalMessageId) {
274 this.externalMessageId = externalMessageId;
275 }
276
277 /**
278 * A helper method for the OpenWire protocol
279 */
280 public String getTransactionIDString() throws IOException {
281 return ActiveMQXid.transactionIDToString(getTransactionId());
282 }
283
284 /**
285 * A helper method for the OpenWire protocol
286 */
287 public void setTransactionIDString(String text) throws IOException {
288 setTransactionId(ActiveMQXid.transactionIDFromString(text));
289 }
290 }