Skip to content

Commit 233c258

Browse files
volkantoziyanli-amazon
authored andcommitted
JMSTimestamp could be set to SentTimestamp
1 parent 456a154 commit 233c258

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

src/main/java/com/amazon/sqs/javamessaging/SQSMessageConsumerPrefetch.java

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.Iterator;
2121
import java.util.List;
22+
import java.util.Map;
2223
import java.util.Set;
2324
import java.util.UUID;
2425

@@ -96,14 +97,14 @@ public class SQSMessageConsumerPrefetch implements Runnable, PrefetchManager {
9697
* Counter on how many messages are prefetched into internal messageQueue.
9798
*/
9899
protected int messagesPrefetched = 0;
99-
100+
100101
/**
101102
* Counter on how many messages have been explicitly requested.
102103
* TODO: Consider renaming this class and several other variables now that
103104
* this logic factors in message requests as well as prefetching.
104105
*/
105106
protected int messagesRequested = 0;
106-
107+
107108
/**
108109
* States of the prefetch thread
109110
*/
@@ -123,7 +124,7 @@ public class SQSMessageConsumerPrefetch implements Runnable, PrefetchManager {
123124
* 25ms delayInterval.
124125
*/
125126
protected ExponentialBackoffStrategy backoffStrategy = new ExponentialBackoffStrategy(25,25,2000);
126-
127+
127128
SQSMessageConsumerPrefetch(SQSSessionCallbackScheduler sqsSessionRunnable, Acknowledger acknowledger,
128129
NegativeAcknowledger negativeAcknowledger, SQSQueueDestination sqsDestination,
129130
AmazonSQSMessagingClientWrapper amazonSQSClient, int numberOfMessagesToPrefetch) {
@@ -141,16 +142,16 @@ public class SQSMessageConsumerPrefetch implements Runnable, PrefetchManager {
141142
MessageListener getMessageListener() {
142143
return messageListener;
143144
}
144-
145+
145146
void setMessageConsumer(SQSMessageConsumer messageConsumer) {
146147
this.messageConsumer = messageConsumer;
147148
}
148-
149+
149150
@Override
150151
public SQSMessageConsumer getMessageConsumer() {
151152
return messageConsumer;
152153
}
153-
154+
154155
/**
155156
* Sets the message listener.
156157
* <P>
@@ -170,7 +171,7 @@ protected void setMessageListener(MessageListener messageListener) {
170171
if (!running || isClosed()) {
171172
return;
172173
}
173-
174+
174175
List<MessageManager> allPrefetchedMessages = new ArrayList<MessageManager>(messageQueue);
175176
sqsSessionRunnable.scheduleCallBacks(messageListener, allPrefetchedMessages);
176177
messageQueue.clear();
@@ -181,7 +182,7 @@ protected void setMessageListener(MessageListener messageListener) {
181182
messageListenerReady();
182183
}
183184
}
184-
185+
185186
/**
186187
* Determine the number of messages we should attempt to fetch from SQS.
187188
* Returns the difference between the number of messages needed (either for
@@ -191,7 +192,7 @@ private int numberOfMessagesToFetch() {
191192
int numberOfMessagesNeeded = Math.max(numberOfMessagesToPrefetch, messagesRequested);
192193
return Math.max(numberOfMessagesNeeded - messagesPrefetched, 0);
193194
}
194-
195+
195196
/**
196197
* Runs until the message consumer is closed and in-progress SQS
197198
* <code>receiveMessage</code> call returns.
@@ -212,7 +213,7 @@ public void run() {
212213
if (isClosed()) {
213214
break;
214215
}
215-
216+
216217
synchronized (stateLock) {
217218
waitForStart();
218219
waitForPrefetch();
@@ -240,7 +241,7 @@ public void run() {
240241
}
241242
}
242243
}
243-
244+
244245
/**
245246
* Call <code>receiveMessage</code> with the given wait time.
246247
*/
@@ -263,7 +264,7 @@ protected List<Message> getMessages(int batchSize, int waitTimeSeconds) throws J
263264
ReceiveMessageResponse receivedMessageResult = amazonSQSClient.receiveMessage(receiveMessageRequestBuilder.build());
264265
return receivedMessageResult.messages();
265266
}
266-
267+
267268
/**
268269
* Converts the received message to JMS message, and pushes to messages to
269270
* either callback scheduler for asynchronous message delivery or to
@@ -282,14 +283,14 @@ protected void processReceivedMessages(List<Message> messages) {
282283
nackMessages.add(message.receiptHandle());
283284
}
284285
}
285-
286+
286287
synchronized (stateLock) {
287288
if (messageListener != null) {
288289
sqsSessionRunnable.scheduleCallBacks(messageListener, messageManagers);
289290
} else {
290291
messageQueue.addAll(messageManagers);
291292
}
292-
293+
293294
messagesPrefetched += messageManagers.size();
294295
notifyStateChange();
295296
}
@@ -363,11 +364,12 @@ protected javax.jms.Message convertToJMSMessage(Message message) throws JMSExcep
363364
throw new JMSException("Not a supported JMS message type");
364365
}
365366
}
366-
367+
367368
jmsMessage.setJMSDestination(sqsDestination);
368-
369+
369370
MessageAttributeValue replyToQueueNameAttribute = message.messageAttributes().get(
370371
SQSMessage.JMS_SQS_REPLY_TO_QUEUE_NAME);
372+
371373
MessageAttributeValue replyToQueueUrlAttribute = message.messageAttributes().get(
372374
SQSMessage.JMS_SQS_REPLY_TO_QUEUE_URL);
373375
if (replyToQueueNameAttribute != null && replyToQueueUrlAttribute != null) {
@@ -376,16 +378,27 @@ protected javax.jms.Message convertToJMSMessage(Message message) throws JMSExcep
376378
Destination replyToQueue = new SQSQueueDestination(replyToQueueName, replyToQueueUrl);
377379
jmsMessage.setJMSReplyTo(replyToQueue);
378380
}
379-
381+
380382
MessageAttributeValue correlationIdAttribute = message.messageAttributes().get(
381383
SQSMessage.JMS_SQS_CORRELATION_ID);
382384
if (correlationIdAttribute != null) {
383385
jmsMessage.setJMSCorrelationID(correlationIdAttribute.stringValue());
384386
}
385-
387+
388+
jmsMessage.setJMSTimestamp(getJMSTimestamp(message));
386389
return jmsMessage;
387390
}
388391

392+
private long getJMSTimestamp(Message message) {
393+
Map<String, String> systemAttributes = message.attributesAsStrings();
394+
String timestamp = systemAttributes.get(SQSMessagingClientConstants.SENT_TIMESTAMP);
395+
if (timestamp != null) {
396+
return Long.parseLong(timestamp);
397+
} else {
398+
return 0L;
399+
}
400+
}
401+
389402
protected void nackQueueMessages() {
390403
// Also nack messages already in the messageQueue
391404
synchronized (stateLock) {
@@ -411,7 +424,7 @@ protected void waitForStart() throws InterruptedException {
411424
}
412425
}
413426
}
414-
427+
415428
@Override
416429
public void messageDispatched() {
417430
synchronized (stateLock) {
@@ -433,7 +446,7 @@ public void messageListenerReady() {
433446
}
434447
}
435448
}
436-
449+
437450
void requestMessage() {
438451
synchronized (stateLock) {
439452
messagesRequested++;
@@ -447,7 +460,7 @@ private void unrequestMessage() {
447460
notifyStateChange();
448461
}
449462
}
450-
463+
451464
public static class MessageManager {
452465

453466
private final PrefetchManager prefetchManager;
@@ -471,7 +484,7 @@ public javax.jms.Message getMessage() {
471484
javax.jms.Message receive() throws JMSException {
472485
return receive(0);
473486
}
474-
487+
475488
javax.jms.Message receive(long timeout) throws JMSException {
476489
if (cannotDeliver()) {
477490
return null;
@@ -480,7 +493,7 @@ javax.jms.Message receive(long timeout) throws JMSException {
480493
if (timeout < 0) {
481494
timeout = 0;
482495
}
483-
496+
484497
MessageManager messageManager = null;
485498
synchronized (stateLock) {
486499
requestMessage();
@@ -490,7 +503,7 @@ javax.jms.Message receive(long timeout) throws JMSException {
490503
messageManager = messageQueue.pollFirst();
491504
} else {
492505
long startTime = System.currentTimeMillis();
493-
506+
494507
long waitTime = 0;
495508
while (messageQueue.isEmpty() && !isClosed() &&
496509
(timeout == 0 || (waitTime = getWaitTime(timeout, startTime)) > 0)) {
@@ -529,7 +542,7 @@ javax.jms.Message receiveNoWait() throws JMSException {
529542
if (cannotDeliver()) {
530543
return null;
531544
}
532-
545+
533546
MessageManager messageManager;
534547
synchronized (stateLock) {
535548
if (messageQueue.isEmpty() && numberOfMessagesToPrefetch == 0) {
@@ -576,22 +589,22 @@ void close() {
576589
messageListener = null;
577590
}
578591
}
579-
592+
580593
/**
581594
* Helper that notifies PrefetchThread that message is dispatched and AutoAcknowledge
582595
*/
583596
private javax.jms.Message messageHandler(MessageManager messageManager) throws JMSException {
584597
if (messageManager == null) {
585598
return null;
586-
}
599+
}
587600
javax.jms.Message message = messageManager.getMessage();
588-
601+
589602
// Notify PrefetchThread that message is dispatched
590603
this.messageDispatched();
591604
acknowledger.notifyMessageReceived((SQSMessage) message);
592605
return message;
593606
}
594-
607+
595608
private boolean cannotDeliver() throws JMSException {
596609
if (!running) {
597610
return true;
@@ -606,7 +619,7 @@ private boolean cannotDeliver() throws JMSException {
606619
}
607620
return false;
608621
}
609-
622+
610623
/**
611624
* Sleeps for the configured time.
612625
*/
@@ -617,7 +630,7 @@ protected void sleep(long sleepTimeMillis) throws InterruptedException {
617630
throw e;
618631
}
619632
}
620-
633+
621634
protected boolean isClosed() {
622635
return closed;
623636
}
@@ -645,7 +658,7 @@ List<SQSMessageIdentifier> purgePrefetchedMessagesWithGroups(Set<String> affecte
645658

646659
notifyStateChange();
647660
}
648-
661+
649662
return purgedMessages;
650663
}
651664
}

src/main/java/com/amazon/sqs/javamessaging/SQSMessagingClientConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public class SQSMessagingClientConstants {
7070

7171
public static final String APPROXIMATE_RECEIVE_COUNT = "ApproximateReceiveCount";
7272

73+
public static final String SENT_TIMESTAMP = "SentTimestamp";
74+
7375
public static final String MESSAGE_DEDUPLICATION_ID = "MessageDeduplicationId";
7476

7577
public static final String MESSAGE_GROUP_ID = "MessageGroupId";

src/test/java/com/amazon/sqs/javamessaging/SQSMessageConsumerPrefetchTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import javax.jms.MessageListener;
5656
import javax.jms.ObjectMessage;
5757

58+
import org.joda.time.DateTime;
5859
import org.junit.Assert;
5960
import org.junit.Before;
6061
import org.junit.Test;
@@ -1069,8 +1070,16 @@ public void testConvertToJMSMessageTextTypeAttribute() throws JMSException, IOEx
10691070
.build();
10701071
mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);
10711072

1073+
<<<<<<< HEAD
10721074
Map<MessageSystemAttributeName, String> mapAttributes = new HashMap<>();
10731075
mapAttributes.put(MessageSystemAttributeName.fromValue(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT), "1");
1076+
=======
1077+
Map<String, String> mapAttributes = new HashMap<String, String>();
1078+
mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");
1079+
Long now = DateTime.now().getMillis();
1080+
mapAttributes.put(SQSMessagingClientConstants.SENT_TIMESTAMP, now.toString());
1081+
1082+
>>>>>>> 84f6b36 (JMSTimestamp could be set to SentTimestamp)
10741083

10751084
// Return message attributes with message type 'TEXT'
10761085
Message message = Message.builder()
@@ -1088,7 +1097,12 @@ public void testConvertToJMSMessageTextTypeAttribute() throws JMSException, IOEx
10881097
* Verify results
10891098
*/
10901099
assertTrue(jsmMessage instanceof SQSTextMessage);
1100+
<<<<<<< HEAD
10911101
assertEquals(message.body(), "MessageBody");
1102+
=======
1103+
assertEquals(message.getBody(), "MessageBody");
1104+
assertEquals(jsmMessage.getJMSTimestamp(), now.longValue());
1105+
>>>>>>> 84f6b36 (JMSTimestamp could be set to SentTimestamp)
10921106
}
10931107

10941108
/**

0 commit comments

Comments
 (0)