Class SQSMessageConsumerPrefetch
- All Implemented Interfaces:
PrefetchManager,Runnable
receive 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 -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Stringprotected ExponentialBackoffStrategyAWS SQS SDK with default backup strategy already re-tries 3 times exponentially.protected booleanStates of the prefetch threadprotected final ArrayDeque<SQSMessageConsumerPrefetch.MessageManager>Internal buffer of Messages.protected intCounter on how many messages are prefetched into internal messageQueue.protected intCounter on how many messages have been explicitly requested.protected intControls the number of retry attempts to the SQSprotected booleanprotected static int -
Method Summary
Modifier and TypeMethodDescriptionprotected jakarta.jms.MessageconvertToJMSMessage(com.amazonaws.services.sqs.model.Message message) Convert the return SQS message into JMS messageThis 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) CallreceiveMessagewith the given wait time.protected List<com.amazonaws.services.sqs.model.Message>getMessagesWithBackoff(int batchSize) protected booleanisClosed()voidNotify the prefetchThread that the message is dispatched from messageQueue when user calls for receive or message listener onMessage is called.voidNotify the prefetchThread that the message listener has finished with any previous message and is ready to accept another.protected voidprotected voidprotected voidprocessReceivedMessages(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.voidrun()Runs until the message consumer is closed and in-progress SQSreceiveMessagecall returns.protected voidsetMessageListener(jakarta.jms.MessageListener messageListener) Sets the message listener.protected voidsleep(long sleepTimeMillis) Sleeps for the configured time.protected voidprotected void
-
Field Details
-
WAIT_TIME_SECONDS
protected static int WAIT_TIME_SECONDS -
ALL
- See Also:
-
messageQueue
Internal buffer of Messages. The size of queue is MIN_BATCH by default, and it can be changed by user. -
messagesPrefetched
protected int messagesPrefetchedCounter on how many messages are prefetched into internal messageQueue. -
messagesRequested
protected int messagesRequestedCounter 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 closedStates of the prefetch thread -
running
protected volatile boolean running -
retriesAttempted
protected int retriesAttemptedControls the number of retry attempts to the SQS -
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 Details
-
getMessageConsumer
Description copied from interface:PrefetchManagerThis is used to determine the state of the consumer, when the message listener scheduler is processing the messages.- Specified by:
getMessageConsumerin interfacePrefetchManager- Returns:
- The message consumer, which owns the prefetchThread
-
setMessageListener
protected void setMessageListener(jakarta.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 SQSreceiveMessagecall 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 jakarta.jms.JMSException CallreceiveMessagewith the given wait time.- Throws:
jakarta.jms.JMSException
-
processReceivedMessages
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
- Throws:
InterruptedException
-
convertToJMSMessage
protected jakarta.jms.Message convertToJMSMessage(com.amazonaws.services.sqs.model.Message message) throws jakarta.jms.JMSException Convert the return SQS message into JMS message- Parameters:
message- SQS message to convert- Returns:
- Converted JMS message
- Throws:
jakarta.jms.JMSException- jmsException
-
nackQueueMessages
protected void nackQueueMessages() -
waitForStart
- Throws:
InterruptedException
-
messageDispatched
public void messageDispatched()Description copied from interface:PrefetchManagerNotify the prefetchThread that the message is dispatched from messageQueue when user calls for receive or message listener onMessage is called.- Specified by:
messageDispatchedin interfacePrefetchManager
-
messageListenerReady
public void messageListenerReady()Description copied from interface:PrefetchManagerNotify the prefetchThread that the message listener has finished with any previous message and is ready to accept another.- Specified by:
messageListenerReadyin interfacePrefetchManager
-
notifyStateChange
protected void notifyStateChange() -
sleep
Sleeps for the configured time.- Throws:
InterruptedException
-
isClosed
protected boolean isClosed()
-