Class SQSMessageConsumerPrefetch
- java.lang.Object
-
- com.amazon.sqs.javamessaging.SQSMessageConsumerPrefetch
-
- All Implemented Interfaces:
PrefetchManager
,Runnable
public class SQSMessageConsumerPrefetch extends Object implements Runnable, PrefetchManager
Used internally to prefetch messages to internal buffer on a background thread for betterreceive
turn around times.Each message consumer creates one prefetch thread.
This runs until the message consumer is closed and in-progress SQS
receiveMessage
call returns.Uses SQS
receiveMessage
with long-poll wait time of 20 seconds.Add re-tries on top of
AmazonSQSClient
re-tries on SQS calls.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SQSMessageConsumerPrefetch.MessageManager
-
Field Summary
Fields Modifier and Type Field Description protected static String
ALL
protected ExponentialBackoffStrategy
backoffStrategy
AWS SQS SDK with default backup strategy already re-tries 3 times exponentially.protected boolean
closed
States of the prefetch threadprotected ArrayDeque<SQSMessageConsumerPrefetch.MessageManager>
messageQueue
Internal buffer of Messages.protected int
messagesPrefetched
Counter on how many messages are prefetched into internal messageQueue.protected int
messagesRequested
Counter on how many messages have been explicitly requested.protected int
retriesAttempted
Controls the number of retry attempts to the SQSprotected boolean
running
protected static int
WAIT_TIME_SECONDS
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected javax.jms.Message
convertToJMSMessage(com.amazonaws.services.sqs.model.Message message)
Convert the return SQS message into JMS messageSQSMessageConsumer
getMessageConsumer()
This is used to determine the state of the consumer, when the message listener scheduler is processing the messages.protected List<com.amazonaws.services.sqs.model.Message>
getMessages(int batchSize, int waitTimeSeconds)
CallreceiveMessage
with the given wait time.protected List<com.amazonaws.services.sqs.model.Message>
getMessagesWithBackoff(int batchSize)
protected boolean
isClosed()
void
messageDispatched()
Notify the prefetchThread that the message is dispatched from messageQueue when user calls for receive or message listener onMessage is called.void
messageListenerReady()
Notify the prefetchThread that the message listener has finished with any previous message and is ready to accept another.protected void
nackQueueMessages()
protected void
notifyStateChange()
protected void
processReceivedMessages(List<com.amazonaws.services.sqs.model.Message> messages)
Converts the received message to JMS message, and pushes to messages to either callback scheduler for asynchronous message delivery or to internal buffers for synchronous message delivery.void
run()
Runs until the message consumer is closed and in-progress SQSreceiveMessage
call returns.protected void
setMessageListener(javax.jms.MessageListener messageListener)
Sets the message listener.protected void
sleep(long sleepTimeMillis)
Sleeps for the configured time.protected void
waitForPrefetch()
protected void
waitForStart()
-
-
-
Field Detail
-
WAIT_TIME_SECONDS
protected static int WAIT_TIME_SECONDS
-
ALL
protected static final String ALL
- See Also:
- Constant Field Values
-
messageQueue
protected final ArrayDeque<SQSMessageConsumerPrefetch.MessageManager> messageQueue
Internal buffer of Messages. The size of queue is MIN_BATCH by default and it can be changed by user.
-
messagesPrefetched
protected int messagesPrefetched
Counter on how many messages are prefetched into internal messageQueue.
-
messagesRequested
protected int messagesRequested
Counter on how many messages have been explicitly requested. TODO: Consider renaming this class and several other variables now that this logic factors in message requests as well as prefetching.
-
closed
protected volatile boolean closed
States of the prefetch thread
-
running
protected volatile boolean running
-
retriesAttempted
protected int retriesAttempted
Controls the number of retry attempts to the SQS
-
backoffStrategy
protected ExponentialBackoffStrategy backoffStrategy
AWS SQS SDK with default backup strategy already re-tries 3 times exponentially. This backoff is on top of that to let the prefetch thread backoff after SDK completes re-tries with a max delay of 2 seconds and 25ms delayInterval.
-
-
Method Detail
-
getMessageConsumer
public SQSMessageConsumer getMessageConsumer()
Description copied from interface:PrefetchManager
This is used to determine the state of the consumer, when the message listener scheduler is processing the messages.- Specified by:
getMessageConsumer
in interfacePrefetchManager
- Returns:
- The message consumer, which owns the prefetchThread
-
setMessageListener
protected void setMessageListener(javax.jms.MessageListener messageListener)
Sets the message listener.If message listener is set, the existing messages on the internal buffer will be pushed to session callback scheduler.
If message lister is set to null, then the messages on the internal buffer of session callback scheduler will be negative acknowledged, which will be handled by the session callback scheduler thread itself.
-
run
public void run()
Runs until the message consumer is closed and in-progress SQSreceiveMessage
call returns.This blocks if configured number of prefetched messages are already received or connection has not started yet.
After consumer is closed, all the messages inside internal buffer will be negatively acknowledged.
-
getMessages
protected List<com.amazonaws.services.sqs.model.Message> getMessages(int batchSize, int waitTimeSeconds) throws javax.jms.JMSException
CallreceiveMessage
with the given wait time.- Throws:
javax.jms.JMSException
-
processReceivedMessages
protected void processReceivedMessages(List<com.amazonaws.services.sqs.model.Message> messages)
Converts the received message to JMS message, and pushes to messages to either callback scheduler for asynchronous message delivery or to internal buffers for synchronous message delivery. Messages that was not converted to JMS message will be immediately negative acknowledged.
-
getMessagesWithBackoff
protected List<com.amazonaws.services.sqs.model.Message> getMessagesWithBackoff(int batchSize) throws InterruptedException
- Throws:
InterruptedException
-
waitForPrefetch
protected void waitForPrefetch() throws InterruptedException
- Throws:
InterruptedException
-
convertToJMSMessage
protected javax.jms.Message convertToJMSMessage(com.amazonaws.services.sqs.model.Message message) throws javax.jms.JMSException
Convert the return SQS message into JMS message- Parameters:
message
- SQS message to convert- Returns:
- Converted JMS message
- Throws:
javax.jms.JMSException
-
nackQueueMessages
protected void nackQueueMessages()
-
waitForStart
protected void waitForStart() throws InterruptedException
- Throws:
InterruptedException
-
messageDispatched
public void messageDispatched()
Description copied from interface:PrefetchManager
Notify the prefetchThread that the message is dispatched from messageQueue when user calls for receive or message listener onMessage is called.- Specified by:
messageDispatched
in interfacePrefetchManager
-
messageListenerReady
public void messageListenerReady()
Description copied from interface:PrefetchManager
Notify the prefetchThread that the message listener has finished with any previous message and is ready to accept another.- Specified by:
messageListenerReady
in interfacePrefetchManager
-
notifyStateChange
protected void notifyStateChange()
-
sleep
protected void sleep(long sleepTimeMillis) throws InterruptedException
Sleeps for the configured time.- Throws:
InterruptedException
-
isClosed
protected boolean isClosed()
-
-