/
AMQPMessage.java
1573 lines (1340 loc) · 56.4 KB
/
AMQPMessage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.protocol.amqp.broker;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.ActiveMQPropertyConversionException;
import org.apache.activemq.artemis.api.core.ICoreMessage;
import org.apache.activemq.artemis.api.core.RefCountMessage;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.message.impl.CoreMessageObjectPools;
import org.apache.activemq.artemis.core.persistence.Persister;
import org.apache.activemq.artemis.protocol.amqp.converter.AMQPMessageIdHelper;
import org.apache.activemq.artemis.protocol.amqp.converter.AMQPMessageSupport;
import org.apache.activemq.artemis.protocol.amqp.converter.AmqpCoreConverter;
import org.apache.activemq.artemis.protocol.amqp.util.NettyReadable;
import org.apache.activemq.artemis.protocol.amqp.util.NettyWritable;
import org.apache.activemq.artemis.protocol.amqp.util.TLSEncode;
import org.apache.activemq.artemis.reader.MessageUtil;
import org.apache.activemq.artemis.utils.DataConstants;
import org.apache.activemq.artemis.utils.collections.TypedProperties;
import org.apache.qpid.proton.amqp.Binary;
import org.apache.qpid.proton.amqp.Symbol;
import org.apache.qpid.proton.amqp.UnsignedByte;
import org.apache.qpid.proton.amqp.UnsignedInteger;
import org.apache.qpid.proton.amqp.UnsignedLong;
import org.apache.qpid.proton.amqp.UnsignedShort;
import org.apache.qpid.proton.amqp.messaging.AmqpSequence;
import org.apache.qpid.proton.amqp.messaging.AmqpValue;
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
import org.apache.qpid.proton.amqp.messaging.Data;
import org.apache.qpid.proton.amqp.messaging.DeliveryAnnotations;
import org.apache.qpid.proton.amqp.messaging.Footer;
import org.apache.qpid.proton.amqp.messaging.Header;
import org.apache.qpid.proton.amqp.messaging.MessageAnnotations;
import org.apache.qpid.proton.amqp.messaging.Properties;
import org.apache.qpid.proton.amqp.messaging.Section;
import org.apache.qpid.proton.codec.DecoderImpl;
import org.apache.qpid.proton.codec.DroppingWritableBuffer;
import org.apache.qpid.proton.codec.EncoderImpl;
import org.apache.qpid.proton.codec.ReadableBuffer;
import org.apache.qpid.proton.codec.TypeConstructor;
import org.apache.qpid.proton.codec.WritableBuffer;
import org.apache.qpid.proton.message.Message;
import org.apache.qpid.proton.message.impl.MessageImpl;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;
import org.jboss.logging.Logger;
// see https://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#section-message-format
public class AMQPMessage extends RefCountMessage {
private static final Logger logger = Logger.getLogger(AMQPMessage.class);
public static final SimpleString ADDRESS_PROPERTY = SimpleString.toSimpleString("_AMQ_AD");
public static final int DEFAULT_MESSAGE_FORMAT = 0;
public static final int DEFAULT_MESSAGE_PRIORITY = 4;
public static final int MAX_MESSAGE_PRIORITY = 9;
private static final int VALUE_NOT_PRESENT = -1;
// Buffer and state for the data backing this message.
private ReadableBuffer data;
private boolean messageDataScanned;
// Marks the message as needed to be re-encoded to update the backing buffer
private boolean modified;
// Track locations of the message sections for later use and track the size
// of the header and delivery annotations if present so we can easily exclude
// the delivery annotations later and perform efficient encodes or copies.
private int headerPosition = VALUE_NOT_PRESENT;
private int encodedHeaderSize;
private int deliveryAnnotationsPosition = VALUE_NOT_PRESENT;
private int encodedDeliveryAnnotationsSize;
private int messageAnnotationsPosition = VALUE_NOT_PRESENT;
private int propertiesPosition = VALUE_NOT_PRESENT;
private int applicationPropertiesPosition = VALUE_NOT_PRESENT;
private int remainingBodyPosition = VALUE_NOT_PRESENT;
// Message level meta data
private final long messageFormat;
private long messageID;
private SimpleString address;
private volatile int memoryEstimate = -1;
private long expiration;
private long scheduledTime = -1;
// The Proton based AMQP message section that are retained in memory, these are the
// mutable portions of the Message as the broker sees it, although AMQP defines that
// the Properties and ApplicationProperties are immutable so care should be taken
// here when making changes to those Sections.
private Header header;
private MessageAnnotations messageAnnotations;
private Properties properties;
private ApplicationProperties applicationProperties;
private String connectionID;
private final CoreMessageObjectPools coreMessageObjectPools;
private Set<Object> rejectedConsumers;
private DeliveryAnnotations deliveryAnnotationsForSendBuffer;
// These are properties set at the broker level and carried only internally by broker storage.
private volatile TypedProperties extraProperties;
/**
* Creates a new {@link AMQPMessage} instance from binary encoded message data.
*
* @param messageFormat
* The Message format tag given the in Transfer that carried this message
* @param data
* The encoded AMQP message
* @param extraProperties
* Broker specific extra properties that should be carried with this message
*/
public AMQPMessage(long messageFormat, byte[] data, TypedProperties extraProperties) {
this(messageFormat, data, extraProperties, null);
}
/**
* Creates a new {@link AMQPMessage} instance from binary encoded message data.
*
* @param messageFormat
* The Message format tag given the in Transfer that carried this message
* @param data
* The encoded AMQP message
* @param extraProperties
* Broker specific extra properties that should be carried with this message
* @param coreMessageObjectPools
* Object pool used to accelerate some String operations.
*/
public AMQPMessage(long messageFormat, byte[] data, TypedProperties extraProperties, CoreMessageObjectPools coreMessageObjectPools) {
this(messageFormat, ReadableBuffer.ByteBufferReader.wrap(data), extraProperties, coreMessageObjectPools);
}
/**
* Creates a new {@link AMQPMessage} instance from binary encoded message data.
*
* @param messageFormat
* The Message format tag given the in Transfer that carried this message
* @param data
* The encoded AMQP message in an {@link ReadableBuffer} wrapper.
* @param extraProperties
* Broker specific extra properties that should be carried with this message
* @param coreMessageObjectPools
* Object pool used to accelerate some String operations.
*/
public AMQPMessage(long messageFormat, ReadableBuffer data, TypedProperties extraProperties, CoreMessageObjectPools coreMessageObjectPools) {
this.data = data;
this.messageFormat = messageFormat;
this.coreMessageObjectPools = coreMessageObjectPools;
this.extraProperties = extraProperties == null ? null : new TypedProperties(extraProperties);
ensureMessageDataScanned();
}
/**
* Internal constructor used for persistence reload of the message.
* <p>
* The message will not be usable until the persistence mechanism populates the message
* data and triggers a parse of the message contents to fill in the message state.
*
* @param messageFormat
* The Message format tag given the in Transfer that carried this message
*/
AMQPMessage(long messageFormat) {
this.messageFormat = messageFormat;
this.modified = true; // No buffer yet so this indicates invalid state.
this.coreMessageObjectPools = null;
}
// Access to the AMQP message data using safe copies freshly decoded from the current
// AMQP message data stored in this message wrapper. Changes to these values cannot
// be used to influence the underlying AMQP message data, the standard AMQPMessage API
// must be used to make changes to mutable portions of the message.
/**
* Creates and returns a Proton-J MessageImpl wrapper around the message data. Changes to
* the returned Message are not reflected in this message.
*
* @return a MessageImpl that wraps the AMQP message data in this {@link AMQPMessage}
*/
public MessageImpl getProtonMessage() {
ensureMessageDataScanned();
ensureDataIsValid();
MessageImpl protonMessage = null;
if (data != null) {
protonMessage = (MessageImpl) Message.Factory.create();
data.rewind();
protonMessage.decode(data.duplicate());
}
return protonMessage;
}
/**
* Returns a copy of the message Header if one is present, changes to the returned
* Header instance do not affect the original Message.
*
* @return a copy of the Message Header if one exists or null if none present.
*/
public Header getHeader() {
ensureMessageDataScanned();
ensureDataIsValid();
return scanForMessageSection(headerPosition, Header.class);
}
/**
* Returns a copy of the MessageAnnotations in the message if present or null. Changes to the
* returned DeliveryAnnotations instance do not affect the original Message.
*
* @return a copy of the {@link DeliveryAnnotations} present in the message or null if non present.
*/
public DeliveryAnnotations getDeliveryAnnotations() {
ensureMessageDataScanned();
ensureDataIsValid();
return scanForMessageSection(deliveryAnnotationsPosition, DeliveryAnnotations.class);
}
/**
* Sets the delivery annotations to be included when encoding the message for sending it on the wire.
*
* The broker can add additional message annotations as long as the annotations being added follow the
* rules from the spec. If the user adds something that the remote doesn't understand and it is not
* prefixed with "x-opt" the remote can just kill the link. See:
*
* http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-annotations
*
* @param deliveryAnnotations delivery annotations used in the sendBuffer() method
*/
public void setDeliveryAnnotationsForSendBuffer(DeliveryAnnotations deliveryAnnotations) {
this.deliveryAnnotationsForSendBuffer = deliveryAnnotations;
}
/**
* Returns a copy of the DeliveryAnnotations in the message if present or null. Changes to the
* returned MessageAnnotations instance do not affect the original Message.
*
* @return a copy of the {@link MessageAnnotations} present in the message or null if non present.
*/
public MessageAnnotations getMessageAnnotations() {
ensureMessageDataScanned();
ensureDataIsValid();
return scanForMessageSection(messageAnnotationsPosition, MessageAnnotations.class);
}
/**
* Returns a copy of the message Properties if one is present, changes to the returned
* Properties instance do not affect the original Message.
*
* @return a copy of the Message Properties if one exists or null if none present.
*/
public Properties getProperties() {
ensureMessageDataScanned();
ensureDataIsValid();
return scanForMessageSection(propertiesPosition, Properties.class);
}
/**
* Returns a copy of the {@link ApplicationProperties} present in the message if present or null.
* Changes to the returned MessageAnnotations instance do not affect the original Message.
*
* @return a copy of the {@link ApplicationProperties} present in the message or null if non present.
*/
public ApplicationProperties getApplicationProperties() {
ensureMessageDataScanned();
ensureDataIsValid();
return scanForMessageSection(applicationPropertiesPosition, ApplicationProperties.class);
}
/**
* Retrieves the AMQP Section that composes the body of this message by decoding a
* fresh copy from the encoded message data. Changes to the returned value are not
* reflected in the value encoded in the original message.
*
* @return the Section that makes up the body of this message.
*/
public Section getBody() {
ensureMessageDataScanned();
ensureDataIsValid();
// We only handle Sections of AmqpSequence, AmqpValue and Data types so we filter on those.
// There could also be a Footer and no body so this will prevent a faulty return type in case
// of no body or message type we don't handle.
return scanForMessageSection(Math.max(0, remainingBodyPosition), AmqpSequence.class, AmqpValue.class, Data.class);
}
/**
* Retrieves the AMQP Footer encoded in the data of this message by decoding a
* fresh copy from the encoded message data. Changes to the returned value are not
* reflected in the value encoded in the original message.
*
* @return the Footer that was encoded into this AMQP Message.
*/
public Footer getFooter() {
ensureMessageDataScanned();
ensureDataIsValid();
return scanForMessageSection(Math.max(0, remainingBodyPosition), Footer.class);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private <T> T scanForMessageSection(int scanStartPosition, Class...targetTypes) {
ensureMessageDataScanned();
// In cases where we parsed out enough to know the value is not encoded in the message
// we can exit without doing any reads or buffer hopping.
if (scanStartPosition == VALUE_NOT_PRESENT) {
return null;
}
ReadableBuffer buffer = data.duplicate().position(0);
final DecoderImpl decoder = TLSEncode.getDecoder();
buffer.position(scanStartPosition);
T section = null;
decoder.setBuffer(buffer);
try {
while (buffer.hasRemaining()) {
TypeConstructor<?> constructor = decoder.readConstructor();
for (Class<?> type : targetTypes) {
if (type.equals(constructor.getTypeClass())) {
section = (T) constructor.readValue();
return section;
}
}
constructor.skipValue();
}
} finally {
decoder.setBuffer(null);
}
return section;
}
private ApplicationProperties lazyDecodeApplicationProperties() {
if (applicationProperties == null && applicationPropertiesPosition != VALUE_NOT_PRESENT) {
applicationProperties = scanForMessageSection(applicationPropertiesPosition, ApplicationProperties.class);
}
return applicationProperties;
}
@SuppressWarnings("unchecked")
private Map<String, Object> getApplicationPropertiesMap(boolean createIfAbsent) {
ApplicationProperties appMap = lazyDecodeApplicationProperties();
Map<String, Object> map = null;
if (appMap != null) {
map = appMap.getValue();
}
if (map == null) {
if (createIfAbsent) {
map = new HashMap<>();
this.applicationProperties = new ApplicationProperties(map);
} else {
map = Collections.EMPTY_MAP;
}
}
return map;
}
@SuppressWarnings("unchecked")
private Map<Symbol, Object> getMessageAnnotationsMap(boolean createIfAbsent) {
Map<Symbol, Object> map = null;
if (messageAnnotations != null) {
map = messageAnnotations.getValue();
}
if (map == null) {
if (createIfAbsent) {
map = new HashMap<>();
this.messageAnnotations = new MessageAnnotations(map);
} else {
map = Collections.EMPTY_MAP;
}
}
return map;
}
private Object getMessageAnnotation(String annotation) {
return getMessageAnnotation(Symbol.getSymbol(annotation));
}
private Object getMessageAnnotation(Symbol annotation) {
return getMessageAnnotationsMap(false).get(annotation);
}
private Object removeMessageAnnotation(Symbol annotation) {
return getMessageAnnotationsMap(false).remove(annotation);
}
private void setMessageAnnotation(String annotation, Object value) {
setMessageAnnotation(Symbol.getSymbol(annotation), value);
}
private void setMessageAnnotation(Symbol annotation, Object value) {
getMessageAnnotationsMap(true).put(annotation, value);
}
// Message decoding and copying methods. Care must be taken here to ensure the buffer and the
// state tracking information is kept up to data. When the message is manually changed a forced
// re-encode should be done to update the backing data with the in memory elements.
private synchronized void ensureMessageDataScanned() {
if (!messageDataScanned) {
scanMessageData();
messageDataScanned = true;
}
}
private synchronized void scanMessageData() {
DecoderImpl decoder = TLSEncode.getDecoder();
decoder.setBuffer(data.rewind());
header = null;
messageAnnotations = null;
properties = null;
applicationProperties = null;
expiration = 0;
encodedHeaderSize = 0;
memoryEstimate = -1;
scheduledTime = -1;
encodedDeliveryAnnotationsSize = 0;
headerPosition = VALUE_NOT_PRESENT;
deliveryAnnotationsPosition = VALUE_NOT_PRESENT;
propertiesPosition = VALUE_NOT_PRESENT;
applicationPropertiesPosition = VALUE_NOT_PRESENT;
remainingBodyPosition = VALUE_NOT_PRESENT;
try {
while (data.hasRemaining()) {
int constructorPos = data.position();
TypeConstructor<?> constructor = decoder.readConstructor();
if (Header.class.equals(constructor.getTypeClass())) {
header = (Header) constructor.readValue();
headerPosition = constructorPos;
encodedHeaderSize = data.position();
if (header.getTtl() != null) {
expiration = System.currentTimeMillis() + header.getTtl().intValue();
}
} else if (DeliveryAnnotations.class.equals(constructor.getTypeClass())) {
// Don't decode these as they are not used by the broker at all and are
// discarded on send, mark for lazy decode if ever needed.
constructor.skipValue();
deliveryAnnotationsPosition = constructorPos;
encodedDeliveryAnnotationsSize = data.position() - constructorPos;
} else if (MessageAnnotations.class.equals(constructor.getTypeClass())) {
messageAnnotationsPosition = constructorPos;
messageAnnotations = (MessageAnnotations) constructor.readValue();
} else if (Properties.class.equals(constructor.getTypeClass())) {
propertiesPosition = constructorPos;
properties = (Properties) constructor.readValue();
if (properties.getAbsoluteExpiryTime() != null && properties.getAbsoluteExpiryTime().getTime() > 0) {
expiration = properties.getAbsoluteExpiryTime().getTime();
}
} else if (ApplicationProperties.class.equals(constructor.getTypeClass())) {
// Lazy decoding will start at the TypeConstructor of these ApplicationProperties
// but we scan past it to grab the location of the possible body and footer section.
applicationPropertiesPosition = constructorPos;
constructor.skipValue();
remainingBodyPosition = data.hasRemaining() ? data.position() : VALUE_NOT_PRESENT;
break;
} else {
// This will be either the body or a Footer section which will be treated as an immutable
// and be copied as is when re-encoding the message.
remainingBodyPosition = constructorPos;
break;
}
}
} finally {
decoder.setByteBuffer(null);
data.rewind();
}
}
@Override
public org.apache.activemq.artemis.api.core.Message copy() {
ensureDataIsValid();
ReadableBuffer view = data.duplicate().rewind();
byte[] newData = new byte[view.remaining()];
// Copy the full message contents with delivery annotations as they will
// be trimmed on send and may become useful on the broker at a later time.
view.get(newData);
AMQPMessage newEncode = new AMQPMessage(this.messageFormat, newData, extraProperties, coreMessageObjectPools);
newEncode.setMessageID(this.getMessageID());
return newEncode;
}
@Override
public org.apache.activemq.artemis.api.core.Message copy(long newID) {
return copy().setMessageID(newID);
}
// Core Message APIs for persisting and encoding of message data along with
// utilities for checking memory usage and encoded size characteristics.
/**
* Would be called by the Artemis Core components to encode the message into
* the provided send buffer. Because of how Proton message data handling works
* this method is not currently used by the AMQP protocol head and will not be
* called for out-bound sends.
*
* @see #getSendBuffer(int) for the actual method used for message sends.
*/
@Override
public void sendBuffer(ByteBuf buffer, int deliveryCount) {
ensureDataIsValid();
NettyWritable writable = new NettyWritable(buffer);
writable.put(getSendBuffer(deliveryCount));
}
/**
* Gets a ByteBuf from the Message that contains the encoded bytes to be sent on the wire.
* <p>
* When possible this method will present the bytes to the caller without copying them into
* a new buffer copy. If copying is needed a new Netty buffer is created and returned. The
* caller should ensure that the reference count on the returned buffer is always decremented
* to avoid a leak in the case of a copied buffer being returned.
*
* @param deliveryCount
* The new delivery count for this message.
*
* @return a Netty ByteBuf containing the encoded bytes of this Message instance.
*/
public ReadableBuffer getSendBuffer(int deliveryCount) {
ensureDataIsValid();
if (deliveryCount > 1) {
return createCopyWithNewDeliveryCount(deliveryCount);
} else if (deliveryAnnotationsPosition != VALUE_NOT_PRESENT
|| (deliveryAnnotationsForSendBuffer != null && !deliveryAnnotationsForSendBuffer.getValue().isEmpty())) {
return createCopyWithSkippedOrExplicitlySetDeliveryAnnotations();
} else {
// Common case message has no delivery annotations, no delivery annotations for the send buffer were set
// and this is the first delivery so no re-encoding or section skipping needed.
return data.duplicate();
}
}
private ReadableBuffer createCopyWithSkippedOrExplicitlySetDeliveryAnnotations() {
// The original message had delivery annotations, or delivery annotations for the send buffer are set.
// That means we must copy into a new buffer skipping the original delivery annotations section
// (not meant to survive beyond this hop) and including the delivery annotations for the send buffer if set.
ReadableBuffer duplicate = data.duplicate();
final ByteBuf result = PooledByteBufAllocator.DEFAULT.heapBuffer(getEncodeSize());
result.writeBytes(duplicate.limit(encodedHeaderSize).byteBuffer());
writeDeliveryAnnotationsForSendBuffer(result);
duplicate.clear();
// skip existing delivery annotations of the original message
duplicate.position(encodedHeaderSize + encodedDeliveryAnnotationsSize);
result.writeBytes(duplicate.byteBuffer());
return new NettyReadable(result);
}
private ReadableBuffer createCopyWithNewDeliveryCount(int deliveryCount) {
assert deliveryCount > 1;
final int amqpDeliveryCount = deliveryCount - 1;
final ByteBuf result = PooledByteBufAllocator.DEFAULT.heapBuffer(getEncodeSize());
// If this is re-delivering the message then the header must be re-encoded
// otherwise we want to write the original header if present. When a
// Header is present we need to copy it as we are updating the re-delivered
// message and not the stored version which we don't want to invalidate here.
Header header = this.header;
if (header == null) {
header = new Header();
} else {
header = new Header(header);
}
header.setDeliveryCount(UnsignedInteger.valueOf(amqpDeliveryCount));
TLSEncode.getEncoder().setByteBuffer(new NettyWritable(result));
TLSEncode.getEncoder().writeObject(header);
TLSEncode.getEncoder().setByteBuffer((WritableBuffer) null);
writeDeliveryAnnotationsForSendBuffer(result);
// skip existing delivery annotations of the original message
data.position(encodedHeaderSize + encodedDeliveryAnnotationsSize);
result.writeBytes(data.byteBuffer());
data.position(0);
return new NettyReadable(result);
}
private void writeDeliveryAnnotationsForSendBuffer(ByteBuf result) {
if (deliveryAnnotationsForSendBuffer != null && !deliveryAnnotationsForSendBuffer.getValue().isEmpty()) {
TLSEncode.getEncoder().setByteBuffer(new NettyWritable(result));
TLSEncode.getEncoder().writeObject(deliveryAnnotationsForSendBuffer);
TLSEncode.getEncoder().setByteBuffer((WritableBuffer) null);
}
}
private int getDeliveryAnnotationsForSendBufferSize() {
if (deliveryAnnotationsForSendBuffer == null || deliveryAnnotationsForSendBuffer.getValue().isEmpty()) {
return 0;
}
DroppingWritableBuffer droppingWritableBuffer = new DroppingWritableBuffer();
TLSEncode.getEncoder().setByteBuffer(droppingWritableBuffer);
TLSEncode.getEncoder().writeObject(deliveryAnnotationsForSendBuffer);
TLSEncode.getEncoder().setByteBuffer((WritableBuffer) null);
return droppingWritableBuffer.position() + 1;
}
@Override
public void messageChanged() {
modified = true;
}
@Override
public ByteBuf getBuffer() {
if (data == null) {
return null;
} else {
if (data instanceof NettyReadable) {
return ((NettyReadable) data).getByteBuf();
} else {
return Unpooled.wrappedBuffer(data.byteBuffer());
}
}
}
@Override
public AMQPMessage setBuffer(ByteBuf buffer) {
// If this is ever called we would be in a highly unfortunate state
this.data = null;
return this;
}
@Override
public int getEncodeSize() {
ensureDataIsValid();
// The encoded size will exclude any delivery annotations that are present as we will clip them.
return data.remaining() - encodedDeliveryAnnotationsSize + getDeliveryAnnotationsForSendBufferSize();
}
@Override
public void receiveBuffer(ByteBuf buffer) {
// Not used for AMQP messages.
}
@Override
public int getMemoryEstimate() {
if (memoryEstimate == -1) {
memoryEstimate = memoryOffset + (data != null ? data.capacity() : 0);
}
return memoryEstimate;
}
@Override
public ICoreMessage toCore(CoreMessageObjectPools coreMessageObjectPools) {
try {
return AmqpCoreConverter.toCore(
this, coreMessageObjectPools, header, messageAnnotations, properties, lazyDecodeApplicationProperties(), getBody(), getFooter());
} catch (Exception e) {
logger.warn(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
@Override
public ICoreMessage toCore() {
return toCore(coreMessageObjectPools);
}
@Override
public void persist(ActiveMQBuffer targetRecord) {
ensureDataIsValid();
targetRecord.writeInt(internalPersistSize());
if (data.hasArray()) {
targetRecord.writeBytes(data.array(), data.arrayOffset(), data.remaining());
} else {
targetRecord.writeBytes(data.byteBuffer());
}
}
@Override
public int getPersistSize() {
ensureDataIsValid();
return DataConstants.SIZE_INT + internalPersistSize();
}
private int internalPersistSize() {
return data.remaining();
}
@Override
public void reloadPersistence(ActiveMQBuffer record) {
int size = record.readInt();
byte[] recordArray = new byte[size];
record.readBytes(recordArray);
data = ReadableBuffer.ByteBufferReader.wrap(ByteBuffer.wrap(recordArray));
// Message state is now that the underlying buffer is loaded but the contents
// not yet scanned, once done the message is fully populated and ready for dispatch.
// Force a scan now and tidy the state variables to reflect where we are following
// this reload from the store.
scanMessageData();
messageDataScanned = true;
modified = false;
// Message state should reflect that is came from persistent storage which
// can happen when moved to a durable location. We must re-encode here to
// avoid a subsequent redelivery from suddenly appearing with a durable header
// tag when the initial delivery did not.
if (!isDurable()) {
setDurable(true);
reencode();
}
}
@Override
public long getPersistentSize() throws ActiveMQException {
return getEncodeSize();
}
@Override
public Persister<org.apache.activemq.artemis.api.core.Message> getPersister() {
return AMQPMessagePersisterV2.getInstance();
}
@Override
public void reencode() {
ensureMessageDataScanned();
// The address was updated on a message with Properties so we update them
// for cases where there are no properties we aren't adding a properties
// section which seems wrong but this preserves previous behavior.
if (properties != null && address != null) {
properties.setTo(address.toString());
}
encodeMessage();
scanMessageData();
messageDataScanned = true;
modified = false;
}
private synchronized void ensureDataIsValid() {
assert data != null;
if (modified) {
encodeMessage();
modified = false;
}
}
private synchronized void encodeMessage() {
int estimated = Math.max(1500, data != null ? data.capacity() + 1000 : 0);
ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(estimated);
EncoderImpl encoder = TLSEncode.getEncoder();
try {
NettyWritable writable = new NettyWritable(buffer);
encoder.setByteBuffer(writable);
if (header != null) {
encoder.writeObject(header);
}
// We currently do not encode any delivery annotations but it is conceivable
// that at some point they may need to be preserved, this is where that needs
// to happen.
if (messageAnnotations != null) {
encoder.writeObject(messageAnnotations);
}
if (properties != null) {
encoder.writeObject(properties);
}
// Whenever possible avoid encoding sections we don't need to by
// checking if application properties where loaded or added and
// encoding only in that case.
if (applicationProperties != null) {
encoder.writeObject(applicationProperties);
// Now raw write the remainder body and footer if present.
if (data != null && remainingBodyPosition != VALUE_NOT_PRESENT) {
writable.put(data.position(remainingBodyPosition));
}
} else if (data != null && applicationPropertiesPosition != VALUE_NOT_PRESENT) {
// Writes out ApplicationProperties, Body and Footer in one go if present.
writable.put(data.position(applicationPropertiesPosition));
} else if (data != null && remainingBodyPosition != VALUE_NOT_PRESENT) {
// No Application properties at all so raw write Body and Footer sections
writable.put(data.position(remainingBodyPosition));
}
byte[] bytes = new byte[buffer.writerIndex()];
buffer.readBytes(bytes);
data = ReadableBuffer.ByteBufferReader.wrap(ByteBuffer.wrap(bytes));
} finally {
encoder.setByteBuffer((WritableBuffer) null);
buffer.release();
}
}
// These methods interact with the Extra Properties that can accompany the message but
// because these are not sent on the wire, update to these does not force a re-encode on
// send of the message.
public TypedProperties createExtraProperties() {
if (extraProperties == null) {
extraProperties = new TypedProperties();
}
return extraProperties;
}
public TypedProperties getExtraProperties() {
return extraProperties;
}
public AMQPMessage setExtraProperties(TypedProperties extraProperties) {
this.extraProperties = extraProperties;
return this;
}
@Override
public org.apache.activemq.artemis.api.core.Message putExtraBytesProperty(SimpleString key, byte[] value) {
createExtraProperties().putBytesProperty(key, value);
return this;
}
@Override
public byte[] getExtraBytesProperty(SimpleString key) throws ActiveMQPropertyConversionException {
if (extraProperties == null) {
return null;
} else {
return extraProperties.getBytesProperty(key);
}
}
@Override
public byte[] removeExtraBytesProperty(SimpleString key) throws ActiveMQPropertyConversionException {
if (extraProperties == null) {
return null;
} else {
return (byte[])extraProperties.removeProperty(key);
}
}
// Message meta data access for Core and AMQP usage.
@Override
public org.apache.activemq.artemis.api.core.Message setConnectionID(String connectionID) {
this.connectionID = connectionID;
return this;
}
@Override
public String getConnectionID() {
return connectionID;
}
public long getMessageFormat() {
return messageFormat;
}
@Override
public long getMessageID() {
return messageID;
}
@Override
public org.apache.activemq.artemis.api.core.Message setMessageID(long id) {
this.messageID = id;
return this;
}
@Override
public long getExpiration() {
return expiration;
}
@Override
public AMQPMessage setExpiration(long expiration) {
if (properties != null) {
if (expiration <= 0) {
properties.setAbsoluteExpiryTime(null);
} else {
properties.setAbsoluteExpiryTime(new Date(expiration));
}
} else if (expiration > 0) {
properties = new Properties();
properties.setAbsoluteExpiryTime(new Date(expiration));
}
// We are overriding expiration with an Absolute expiration time so any
// previous Header based TTL also needs to be removed.
if (header != null) {
header.setTtl(null);
}
this.expiration = Math.max(0, expiration);
return this;
}
@Override
public Object getUserID() {
// User ID in Artemis API means Message ID
if (properties != null && properties.getMessageId() != null) {
return properties.getMessageId();
} else {
return null;
}
}
/**
* Before we added AMQP into Artemis the name getUserID was already taken by JMSMessageID.
* We cannot simply change the names now as it would break the API for existing clients.
*
* This is to return and read the proper AMQP userID.
*
* @return the UserID value in the AMQP Properties if one is present.
*/
public Object getAMQPUserID() {
if (properties != null && properties.getUserId() != null) {
Binary binary = properties.getUserId();
return new String(binary.getArray(), binary.getArrayOffset(), binary.getLength(), StandardCharsets.UTF_8);
} else {
return null;
}
}
@Override
public org.apache.activemq.artemis.api.core.Message setUserID(Object userID) {
return this;
}
@Override
public Object getDuplicateProperty() {
return getObjectProperty(org.apache.activemq.artemis.api.core.Message.HDR_DUPLICATE_DETECTION_ID);
}
@Override
public boolean isDurable() {
if (header != null && header.getDurable() != null) {
return header.getDurable();
} else {
return false;
}
}
@Override
public org.apache.activemq.artemis.api.core.Message setDurable(boolean durable) {
if (header == null) {
header = new Header();
}
header.setDurable(durable); // Message needs to be re-encoded following this action.
return this;
}