public class SQSMessageConsumerPrefetch extends Object implements Runnable, PrefetchManager
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.
| Modifier and Type | Class and Description |
|---|---|
static class |
SQSMessageConsumerPrefetch.MessageManager |
| Modifier and Type | Field and 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 thread
|
protected 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 SQS
|
protected boolean |
running |
protected static int |
WAIT_TIME_SECONDS |
| Modifier and Type | Method and Description |
|---|---|
protected javax.jms.Message |
convertToJMSMessage(com.amazonaws.services.sqs.model.Message message)
Convert the return SQS message into JMS message
|
SQSMessageConsumer |
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)
Call
receiveMessage 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 SQS
receiveMessage 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() |
protected static final int WAIT_TIME_SECONDS
protected static final String ALL
protected final ArrayDeque<SQSMessageConsumerPrefetch.MessageManager> messageQueue
protected int messagesPrefetched
protected int messagesRequested
protected volatile boolean closed
protected volatile boolean running
protected int retriesAttempted
protected ExponentialBackoffStrategy backoffStrategy
public SQSMessageConsumer getMessageConsumer()
PrefetchManagergetMessageConsumer in interface PrefetchManagerprotected void setMessageListener(javax.jms.MessageListener messageListener)
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.
public void run()
receiveMessage 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.
protected List<com.amazonaws.services.sqs.model.Message> getMessages(int batchSize, int waitTimeSeconds) throws javax.jms.JMSException
receiveMessage with the given wait time.javax.jms.JMSExceptionprotected void processReceivedMessages(List<com.amazonaws.services.sqs.model.Message> messages)
protected List<com.amazonaws.services.sqs.model.Message> getMessagesWithBackoff(int batchSize) throws InterruptedException
InterruptedExceptionprotected void waitForPrefetch()
throws InterruptedException
InterruptedExceptionprotected javax.jms.Message convertToJMSMessage(com.amazonaws.services.sqs.model.Message message)
throws javax.jms.JMSException
message - SQS message to convertjavax.jms.JMSExceptionprotected void nackQueueMessages()
protected void waitForStart()
throws InterruptedException
InterruptedExceptionpublic void messageDispatched()
PrefetchManagermessageDispatched in interface PrefetchManagerpublic void messageListenerReady()
PrefetchManagermessageListenerReady in interface PrefetchManagerprotected void notifyStateChange()
protected void sleep(long sleepTimeMillis)
throws InterruptedException
InterruptedExceptionprotected boolean isClosed()
Copyright © 2022. All rights reserved.