-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathSQSMessage.java
More file actions
1195 lines (1084 loc) · 41.4 KB
/
SQSMessage.java
File metadata and controls
1195 lines (1084 loc) · 41.4 KB
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
/*
* Copyright 2010-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 com.amazon.sqs.javamessaging.message;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageFormatException;
import javax.jms.MessageNotWriteableException;
import com.amazon.sqs.javamessaging.SQSMessageConsumerPrefetch;
import com.amazon.sqs.javamessaging.acknowledge.Acknowledger;
import com.amazonaws.services.sqs.model.MessageAttributeValue;
import static com.amazon.sqs.javamessaging.SQSMessagingClientConstants.*;
/**
* The SQSMessage is the root class of all SQS JMS messages and implements JMS
* Message interface.
* <P>
* Not all message headers are supported at this time:
* <ul>
* <li><code>JMSMessageID</code> is always assigned as SQS provided message id.</li>
* <li><code>JMSRedelivered</code> is set to true if SQS delivers the message
* more than once. This not necessarily mean that the user received message more
* than once, but rather SQS attempted to deliver it more than once. Due to
* prefetching used in {@link SQSMessageConsumerPrefetch}, this can be set to
* true although user never received the message. This is set based on SQS
* ApproximateReceiveCount attribute</li>
* <li><code>JMSDestination</code></li> is the destination object which message
* is sent to and received from.
* </ul>
* </P>
* <P>
* JMSXDeliveryCount reserved property is supported and set based on the
* approximate receive count observed on the SQS side.
*/
public class SQSMessage implements Message {
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
// Define constant message types.
public static final String BYTE_MESSAGE_TYPE = "byte";
public static final String OBJECT_MESSAGE_TYPE = "object";
public static final String TEXT_MESSAGE_TYPE = "text";
public static final String JMS_SQS_MESSAGE_TYPE = "JMS_SQSMessageType";
// Default JMS Message properties
private int deliveryMode = Message.DEFAULT_DELIVERY_MODE;
private int priority = Message.DEFAULT_PRIORITY;
private long timestamp;
private boolean redelivered;
private String correlationID;
private long expiration = Message.DEFAULT_TIME_TO_LIVE;
private String messageID;
private String type;
private Destination replyTo;
private Destination destination;
private final Map<String, JMSMessagePropertyValue> properties = new HashMap<String, JMSMessagePropertyValue>();
private boolean writePermissionsForProperties;
private boolean writePermissionsForBody;
/**
* Function for acknowledging message.
*/
private Acknowledger acknowledger;
/**
* Original SQS Message ID.
*/
private String sqsMessageID;
/**
* QueueUrl the message came from.
*/
private String queueUrl;
/**
* Original SQS Message receipt handle.
*/
private String receiptHandle;
/**
* This is called at the receiver side to create a
* JMS message from the SQS message received.
*/
SQSMessage(Acknowledger acknowledger, String queueUrl, com.amazonaws.services.sqs.model.Message sqsMessage) throws JMSException{
this.acknowledger = acknowledger;
this.queueUrl = queueUrl;
receiptHandle = sqsMessage.getReceiptHandle();
sqsMessageID = sqsMessage.getMessageId();
this.messageID = String.format(MESSAGE_ID_FORMAT,sqsMessageID);
Map<String,String> systemAttributes = sqsMessage.getAttributes();
String receiveCountAttrib = systemAttributes.get(APPROXIMATE_RECEIVE_COUNT);
int receiveCount = receiveCountAttrib == null ? 0 : Integer.parseInt(receiveCountAttrib);
/*
* JMSXDeliveryCount is set based on SQS ApproximateReceiveCount
* attribute.
*/
properties.put(JMSX_DELIVERY_COUNT, new JMSMessagePropertyValue(
receiveCount, INT));
if (receiveCount > 1) {
setJMSRedelivered(true);
}
if (sqsMessage.getMessageAttributes() != null) {
addMessageAttributes(sqsMessage);
}
writePermissionsForBody = false;
writePermissionsForProperties = false;
}
/**
* Create new empty Message to send. SQSMessage cannot be sent without any
* payload. One of SQSTextMessage, SQSObjectMessage, or SQSBytesMessage
* should be used to add payload.
*/
SQSMessage() {
writePermissionsForBody = true;
writePermissionsForProperties = true;
}
private void addMessageAttributes(com.amazonaws.services.sqs.model.Message sqsMessage) throws JMSException {
for (Entry<String, MessageAttributeValue> entry : sqsMessage.getMessageAttributes().entrySet()) {
properties.put(entry.getKey(), new JMSMessagePropertyValue(
entry.getValue().getStringValue(), entry.getValue().getDataType()));
}
}
protected void checkPropertyWritePermissions() throws JMSException {
if (!writePermissionsForProperties) {
throw new MessageNotWriteableException("Message properties are not writable");
}
}
protected void checkBodyWritePermissions() throws JMSException {
if (!writePermissionsForBody) {
throw new MessageNotWriteableException("Message body is not writable");
}
}
protected static JMSException convertExceptionToJMSException(Exception e) {
JMSException ex = new JMSException(e.getMessage());
ex.initCause(e);
return ex;
}
protected static MessageFormatException convertExceptionToMessageFormatException(Exception e) {
MessageFormatException ex = new MessageFormatException(e.getMessage());
ex.initCause(e);
return ex;
}
protected void setBodyWritePermissions(boolean enable) {
writePermissionsForBody = enable;
}
/**
* Get SQS Message Id.
*
* @return SQS Message Id.
*/
public String getSQSMessageId() {
return sqsMessageID;
}
/**
* Set SQS Message Id, used on send.
*
* @param sqsMessageID
* messageId assigned by SQS during send.
*/
public void setSQSMessageId(String sqsMessageID) throws JMSException {
this.sqsMessageID = sqsMessageID;
}
/**
* Get SQS Message receiptHandle.
*
* @return SQS Message receiptHandle.
*/
public String getReceiptHandle() {
return receiptHandle;
}
/**
* Get queueUrl the message came from.
*
* @return queueUrl.
*/
public String getQueueUrl() {
return queueUrl;
}
/**
* Gets the message ID.
* <P>
* The JMSMessageID header field contains a value that uniquely identifies
* each message sent by a provider. It is set to SQS messageId with the
* prefix 'ID:'.
*
* @return the ID of the message.
*/
@Override
public String getJMSMessageID() throws JMSException {
return messageID;
}
/**
* Sets the message ID. It should have prefix 'ID:'.
* <P>
* Set when a message is sent. This method can be used to change the value
* for a message that has been received.
*
* @param id
* The ID of the message.
*/
@Override
public void setJMSMessageID(String id) throws JMSException {
messageID = id;
}
@Override
public long getJMSTimestamp() throws JMSException {
return timestamp;
}
@Override
public void setJMSTimestamp(long timestamp) throws JMSException {
this.timestamp = timestamp;
}
@Override
public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
return correlationID != null ? correlationID.getBytes(DEFAULT_CHARSET) : null;
}
@Override
public void setJMSCorrelationIDAsBytes(byte[] correlationID) throws JMSException {
try {
this.correlationID = correlationID != null ? new String(correlationID, "UTF-8") : null;
} catch (UnsupportedEncodingException e) {
throw new JMSException(e.getMessage());
}
}
@Override
public void setJMSCorrelationID(String correlationID) throws JMSException {
this.correlationID = correlationID;
}
@Override
public String getJMSCorrelationID() throws JMSException {
return correlationID;
}
@Override
public Destination getJMSReplyTo() throws JMSException {
return replyTo;
}
@Override
public void setJMSReplyTo(Destination replyTo) throws JMSException {
this.replyTo = replyTo;
}
/**
* Gets the Destination object for this message.
* <P>
* The JMSDestination header field contains the destination to which the
* message is being sent.
* <P>
* When a message is sent, this field is ignored. After completion of the
* send or publish method, the field holds the destination specified by the
* method.
* <P>
* When a message is received, its JMSDestination value must be equivalent
* to the value assigned when it was sent.
*
* @return The destination of this message.
*/
@Override
public Destination getJMSDestination() throws JMSException {
return destination;
}
/**
* Sets the Destination object for this message.
* <P>
* Set when a message is sent. This method can be used to change the value
* for a message that has been received.
*
* @param destination
* The destination for this message.
*/
@Override
public void setJMSDestination(Destination destination) throws JMSException {
this.destination = destination;
}
@Override
public int getJMSDeliveryMode() throws JMSException {
return deliveryMode;
}
@Override
public void setJMSDeliveryMode(int deliveryMode) throws JMSException {
this.deliveryMode = deliveryMode;
}
@Override
public boolean getJMSRedelivered() throws JMSException {
return redelivered;
}
@Override
public void setJMSRedelivered(boolean redelivered) throws JMSException {
this.redelivered = redelivered;
}
@Override
public String getJMSType() throws JMSException {
return type;
}
@Override
public void setJMSType(String type) throws JMSException {
this.type = type;
}
@Override
public long getJMSExpiration() throws JMSException {
return expiration;
}
@Override
public void setJMSExpiration(long expiration) throws JMSException {
this.expiration = expiration;
}
@Override
public int getJMSPriority() throws JMSException {
return priority;
}
@Override
public void setJMSPriority(int priority) throws JMSException {
this.priority = priority;
}
/**
* Clears a message's properties and set the write permissions for
* properties. The message's header fields and body are not cleared.
*/
@Override
public void clearProperties() throws JMSException {
properties.clear();
writePermissionsForProperties = true;
}
/**
* Indicates whether a property value exists for the given property name.
*
* @param name
* The name of the property.
* @return true if the property exists.
*/
@Override
public boolean propertyExists(String name) throws JMSException {
return properties.containsKey(name);
}
/**
* Get the value for a property that represents a java primitive(e.g. int or
* long).
*
* @param property
* The name of the property to get.
* @param type
* The type of the property.
* @return the converted value for the property.
* @throws JMSException
* On internal error.
* @throws MessageFormatException
* If the property cannot be converted to the specified type.
* @throws NullPointerException
* and NumberFormatException when property name or value is
* null. Method throws same exception as primitives
* corresponding valueOf(String) method.
*/
<T> T getPrimitiveProperty(String property, Class<T> type) throws JMSException {
if (property == null) {
throw new NullPointerException("Property name is null");
}
Object value = getObjectProperty(property);
if (value == null) {
return handleNullPropertyValue(property, type);
}
T convertedValue = TypeConversionSupport.convert(value, type);
if (convertedValue == null) {
throw new MessageFormatException("Property " + property + " was " + value.getClass().getName() +
" and cannot be read as " + type.getName());
}
return convertedValue;
}
@SuppressWarnings("unchecked")
private <T> T handleNullPropertyValue(String name, Class<T> clazz) {
if (clazz == String.class) {
return null;
} else if (clazz == Boolean.class) {
return (T) Boolean.FALSE;
} else if (clazz == Double.class || clazz == Float.class) {
throw new NullPointerException("Value of property with name " + name + " is null.");
} else {
throw new NumberFormatException("Value of property with name " + name + " is null.");
}
}
/**
* Returns the value of the <code>boolean</code> property with the specified
* name.
*
* @param name
* The name of the property to get.
* @return the <code>boolean</code> property value for the specified name.
* @throws JMSException
* On internal error.
* @throws MessageFormatException
* If the property cannot be converted to the specified type.
* @throws NullPointerException
* When property name is null.
*/
@Override
public boolean getBooleanProperty(String name) throws JMSException {
return getPrimitiveProperty(name, Boolean.class);
}
/**
* Returns the value of the <code>byte</code> property with the specified
* name.
*
* @param name
* The name of the property to get.
* @return the <code>byte</code> property value for the specified name.
* @throws JMSException
* On internal error.
* @throws MessageFormatException
* If the property cannot be converted to the specified type.
* @throws NullPointerException
* When property name is null.
* @throws NumberFormatException
* When property value is null.
*/
@Override
public byte getByteProperty(String name) throws JMSException {
return getPrimitiveProperty(name, Byte.class);
}
/**
* Returns the value of the <code>short</code> property with the specified
* name.
*
* @param name
* The name of the property to get.
* @return the <code>short</code> property value for the specified name.
* @throws JMSException
* On internal error.
* @throws MessageFormatException
* If the property cannot be converted to the specified type.
* @throws NullPointerException
* When property name is null.
* @throws NumberFormatException
* When property value is null.
*/
@Override
public short getShortProperty(String name) throws JMSException {
return getPrimitiveProperty(name, Short.class);
}
/**
* Returns the value of the <code>int</code> property with the specified
* name.
*
* @param name
* The name of the property to get.
* @return the <code>int</code> property value for the specified name.
* @throws JMSException
* On internal error.
* @throws MessageFormatException
* If the property cannot be converted to the specified type.
* @throws NullPointerException
* When property name is null.
* @throws NumberFormatException
* When property value is null.
*/
@Override
public int getIntProperty(String name) throws JMSException {
return getPrimitiveProperty(name, Integer.class);
}
/**
* Returns the value of the <code>long</code> property with the specified
* name.
*
* @param name
* The name of the property to get.
* @return the <code>long</code> property value for the specified name.
* @throws JMSException
* On internal error.
* @throws MessageFormatException
* If the property cannot be converted to the specified type.
* @throws NullPointerException
* When property name is null.
* @throws NumberFormatException
* When property value is null.
*/
@Override
public long getLongProperty(String name) throws JMSException {
return getPrimitiveProperty(name, Long.class);
}
/**
* Returns the value of the <code>float</code> property with the specified
* name.
*
* @param name
* The name of the property to get.
* @return the <code>float</code> property value for the specified name.
* @throws JMSException
* Wn internal error.
* @throws MessageFormatException
* If the property cannot be converted to the specified type.
* @throws NullPointerException
* When property name or value is null.
*/
@Override
public float getFloatProperty(String name) throws JMSException {
return getPrimitiveProperty(name, Float.class);
}
/**
* Returns the value of the <code>double</code> property with the specified
* name.
*
* @param name
* The name of the property to get.
* @return the <code>double</code> property value for the specified name.
* @throws JMSException
* On internal error.
* @throws MessageFormatException
* If the property cannot be converted to the specified type.
* @throws NullPointerException
* When property name or value is null.
*/
@Override
public double getDoubleProperty(String name) throws JMSException {
return getPrimitiveProperty(name, Double.class);
}
/**
* Returns the value of the <code>String</code> property with the specified
* name.
*
* @param name
* The name of the property to get.
* @return the <code>String</code> property value for the specified name.
* @throws JMSException
* On internal error.
* @throws MessageFormatException
* If the property cannot be converted to the specified type.
* @throws NullPointerException
* When property name is null.
*/
@Override
public String getStringProperty(String name) throws JMSException {
return getPrimitiveProperty(name, String.class);
}
/**
* Returns the value of the Java object property with the specified name.
* <P>
* This method can be used to return, in boxed format, an object that has
* been stored as a property in the message with the equivalent
* <code>setObjectProperty</code> method call, or its equivalent primitive
* setter method.
*
* @param name
* The name of the property to get.
* @return the Java object property value with the specified name, in boxed
* format (for example, if the property was set as an
* <code>int</code>, an <code>Integer</code> is returned); if there
* is no property by this name, a null value is returned.
* @throws JMSException
* On internal error.
*/
@Override
public Object getObjectProperty(String name) throws JMSException {
JMSMessagePropertyValue propertyValue = getJMSMessagePropertyValue(name);
if (propertyValue != null) {
return propertyValue.getValue();
}
return null;
}
/**
* Returns the property value with message attribute to object property
* conversions took place.
* <P>
*
* @param name
* The name of the property to get.
* @return <code>JMSMessagePropertyValue</code> with object value and
* corresponding SQS message attribute type and message attribute
* string value.
* @throws JMSException
* On internal error.
*/
public JMSMessagePropertyValue getJMSMessagePropertyValue(String name) throws JMSException {
return properties.get(name);
}
private static class PropertyEnum implements Enumeration<String> {
private final Iterator<String> propertyItr;
public PropertyEnum(Iterator<String> propertyItr) {
this.propertyItr = propertyItr;
}
@Override
public boolean hasMoreElements() {
return propertyItr.hasNext();
}
@Override
public String nextElement() {
return propertyItr.next();
}
}
/**
* Returns an <code>Enumeration</code> of all the property names.
* <P>
* Note that JMS standard header fields are not considered properties and
* are not returned in this enumeration.
*
* @return an enumeration of all the names of property values.
* @throws JMSException
* On internal error.
*/
@Override
public Enumeration<String> getPropertyNames() throws JMSException {
return new PropertyEnum(properties.keySet().iterator());
}
/**
* Sets a <code>boolean</code> property value with the specified name into
* the message.
*
* @param name
* The name of the property to set.
* @param value
* The <code>boolean</code> value of the property to set.
* @throws JMSException
* On internal error.
* @throws IllegalArgumentException
* If the name or value is null or empty string.
* @throws MessageNotWriteableException
* If properties are read-only.
*/
@Override
public void setBooleanProperty(String name, boolean value) throws JMSException {
setObjectProperty(name, value);
}
/**
* Sets a <code>byte</code> property value with the specified name into
* the message.
*
* @param name
* The name of the property to set.
* @param value
* The <code>byte</code> value of the property to set.
* @throws JMSException
* On internal error.
* @throws IllegalArgumentException
* If the name or value is null or empty string.
* @throws MessageNotWriteableException
* If properties are read-only.
*/
@Override
public void setByteProperty(String name, byte value) throws JMSException {
setObjectProperty(name, value);
}
/**
* Sets a <code>short</code> property value with the specified name into
* the message.
*
* @param name
* The name of the property to set.
* @param value
* The <code>short</code> value of the property to set.
* @throws JMSException
* On internal error.
* @throws IllegalArgumentException
* If the name or value is null or empty string.
* @throws MessageNotWriteableException
* If properties are read-only.
*/
@Override
public void setShortProperty(String name, short value) throws JMSException {
setObjectProperty(name, value);
}
/**
* Sets a <code>int</code> property value with the specified name into
* the message.
*
* @param name
* The name of the property to set.
* @param value
* The <code>int</code> value of the property to set.
* @throws JMSException
* On internal error.
* @throws IllegalArgumentException
* If the name or value is null or empty string.
* @throws MessageNotWriteableException
* If properties are read-only.
*/
@Override
public void setIntProperty(String name, int value) throws JMSException {
setObjectProperty(name, value);
}
/**
* Sets a <code>long</code> property value with the specified name into
* the message.
*
* @param name
* The name of the property to set.
* @param value
* The <code>long</code> value of the property to set.
* @throws JMSException
* On internal error.
* @throws IllegalArgumentException
* If the name or value is null or empty string.
* @throws MessageNotWriteableException
* If properties are read-only.
*/
@Override
public void setLongProperty(String name, long value) throws JMSException {
setObjectProperty(name, value);
}
/**
* Sets a <code>float</code> property value with the specified name into
* the message.
*
* @param name
* The name of the property to set.
* @param value
* The <code>float</code> value of the property to set.
* @throws JMSException
* On internal error.
* @throws IllegalArgumentException
* If the name or value is null or empty string.
* @throws MessageNotWriteableException
* If properties are read-only.
*/
@Override
public void setFloatProperty(String name, float value) throws JMSException {
setObjectProperty(name, value);
}
/**
* Sets a <code>double</code> property value with the specified name into
* the message.
*
* @param name
* The name of the property to set.
* @param value
* The <code>double</code> value of the property to set.
* @throws JMSException
* On internal error.
* @throws IllegalArgumentException
* If the name or value is null or empty string.
* @throws MessageNotWriteableException
* If properties are read-only.
*/
@Override
public void setDoubleProperty(String name, double value) throws JMSException {
setObjectProperty(name, value);
}
/**
* Sets a <code>String</code> property value with the specified name into
* the message.
*
* @param name
* The name of the property to set.
* @param value
* The <code>String</code> value of the property to set.
* @throws JMSException
* On internal error.
* @throws IllegalArgumentException
* If the name or value is null or empty string.
* @throws MessageNotWriteableException
* If properties are read-only.
*/
@Override
public void setStringProperty(String name, String value) throws JMSException {
setObjectProperty(name, value);
}
/**
* Sets a Java object property value with the specified name into the
* message.
* <P>
* Note that this method works only for the boxed primitive object types
* (Integer, Double, Long ...) and String objects.
*
* @param name
* The name of the property to set.
* @param value
* The object value of the property to set.
* @throws JMSException
* On internal error.
* @throws IllegalArgumentException
* If the name or value is null or empty string.
* @throws MessageFormatException
* If the object is invalid type.
* @throws MessageNotWriteableException
* If properties are read-only.
*/
@Override
public void setObjectProperty(String name, Object value) throws JMSException {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Property name can not be null or empty.");
}
if (value == null || "".equals(value)) {
throw new IllegalArgumentException("Property value can not be null or empty.");
}
if(!isValidPropertyValueType(value)) {
throw new MessageFormatException("Value of property with name " + name + " has incorrect type " + value.getClass().getName() + ".");
}
checkPropertyWritePermissions();
properties.put(name, new JMSMessagePropertyValue(value));
}
/**
* <P>
* Acknowledges message(s).
* <P>
* A client may individually acknowledge each message as it is consumed, or
* it may choose to acknowledge multiple messages based on acknowledge mode,
* which in turn might might acknowledge all messages consumed by the
* session.
* <P>
* Messages that have been received but not acknowledged may be redelivered.
* <P>
* If the session is closed, messages cannot be acknowledged.
* <P>
* If only the consumer is closed, messages can still be acknowledged.
*
* @see com.amazon.sqs.javamessaging.acknowledge.AcknowledgeMode
* @throws JMSException
* On Internal error
* @throws IllegalStateException
* If this method is called on a closed session.
*/
@Override
public void acknowledge() throws JMSException {
if (acknowledger != null) {
acknowledger.acknowledge(this);
}
}
/**
* <P>
* Clears out the message body. Clearing a message's body does not clear its
* header values or property entries.
* <P>
* This method cannot be called directly instead the implementation on the
* subclasses should be used.
*
* @throws JMSException
* If directly called
*/
@Override
public void clearBody() throws JMSException {
throw new JMSException("SQSMessage does not have any body");
}
private boolean isValidPropertyValueType(Object value) {
return value instanceof Boolean || value instanceof Byte || value instanceof Short ||
value instanceof Integer || value instanceof Long || value instanceof Float ||
value instanceof Double || value instanceof String;
}
/**
* Copied from org.apache.activemq.util.TypeConversionSupport to provide the
* same property support provided by activemq without creating a dependency
* on activemq.
*/
public static class TypeConversionSupport {
static class ConversionKey {
final Class<?> from;
final Class<?> to;
public ConversionKey(Class<?> from, Class<?> to) {
this.from = from;
this.to = to;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((from == null) ? 0 : from.hashCode());
result = prime * result + ((to == null) ? 0 : to.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ConversionKey other = (ConversionKey) obj;
if (from == null) {
if (other.from != null)
return false;
} else if (!from.equals(other.from))
return false;
if (to == null) {
if (other.to != null)
return false;
} else if (!to.equals(other.to))
return false;
return true;
}
}
interface Converter {
Object convert(Object value);
}
static final private Map<ConversionKey, Converter> CONVERSION_MAP = new HashMap<ConversionKey, Converter>();
static {
Converter toStringConverter = new Converter() {
public Object convert(Object value) {
return value.toString();
}
};
CONVERSION_MAP.put(new ConversionKey(Boolean.class, String.class), toStringConverter);
CONVERSION_MAP.put(new ConversionKey(Byte.class, String.class), toStringConverter);
CONVERSION_MAP.put(new ConversionKey(Short.class, String.class), toStringConverter);
CONVERSION_MAP.put(new ConversionKey(Integer.class, String.class), toStringConverter);
CONVERSION_MAP.put(new ConversionKey(Long.class, String.class), toStringConverter);
CONVERSION_MAP.put(new ConversionKey(Float.class, String.class), toStringConverter);
CONVERSION_MAP.put(new ConversionKey(Double.class, String.class), toStringConverter);
CONVERSION_MAP.put(new ConversionKey(String.class, Boolean.class), new Converter() {
public Object convert(Object value) {
String stringValue = (String) value;
if (Boolean.valueOf(stringValue) || INT_TRUE.equals((String) value)) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}
});