Class SQSMessageConsumer
- java.lang.Object
-
- com.amazon.sqs.javamessaging.SQSMessageConsumer
-
- All Implemented Interfaces:
javax.jms.MessageConsumer
,javax.jms.QueueReceiver
public class SQSMessageConsumer extends Object implements javax.jms.MessageConsumer, javax.jms.QueueReceiver
A client uses a MessageConsumer object to receive messages from a destination. A MessageConsumer object is created by passing a Destination object to a message-consumer creation method supplied by a session.This message consumer does not support message selectors
A client may either synchronously receive a message consumer's messages or have the consumer asynchronously deliver them as they arrive via registering a MessageListener object.
The message consumer creates a background thread to prefetch the messages to improve the
receive
turn-around times.
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
closed
static int
PREFETCH_EXECUTOR_GRACEFUL_SHUTDOWN_TIME
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the message consumer.javax.jms.MessageListener
getMessageListener()
Gets the message consumer's MessageListener.String
getMessageSelector()
This method is not supported.javax.jms.Queue
getQueue()
Gets the queue destination associated with this queue receiver, where the messages are delivered from.javax.jms.Message
receive()
This call blocks indefinitely until a message is produced or until this message consumer is closed.javax.jms.Message
receive(long timeout)
This call blocks until a message arrives, the timeout expires, or this message consumer is closed.javax.jms.Message
receiveNoWait()
Receives the next message if one is immediately available.void
setMessageListener(javax.jms.MessageListener listener)
Sets the message consumer's MessageListener.protected void
startPrefetch()
This starts the prefetchingprotected void
stopPrefetch()
This stops the prefetching
-
-
-
Field Detail
-
PREFETCH_EXECUTOR_GRACEFUL_SHUTDOWN_TIME
public static final int PREFETCH_EXECUTOR_GRACEFUL_SHUTDOWN_TIME
- See Also:
- Constant Field Values
-
closed
protected volatile boolean closed
-
-
Method Detail
-
getQueue
public javax.jms.Queue getQueue() throws javax.jms.JMSException
Gets the queue destination associated with this queue receiver, where the messages are delivered from.- Specified by:
getQueue
in interfacejavax.jms.QueueReceiver
- Returns:
- a queue destination
- Throws:
javax.jms.JMSException
-
getMessageListener
public javax.jms.MessageListener getMessageListener() throws javax.jms.JMSException
Gets the message consumer's MessageListener.- Specified by:
getMessageListener
in interfacejavax.jms.MessageConsumer
- Returns:
- a message listener
- Throws:
javax.jms.JMSException
-
setMessageListener
public void setMessageListener(javax.jms.MessageListener listener) throws javax.jms.JMSException
Sets the message consumer's MessageListener.- Specified by:
setMessageListener
in interfacejavax.jms.MessageConsumer
- Parameters:
listener
- a message listener to use for asynchronous message delivery- Throws:
javax.jms.JMSException
- If the message consumer is closed
-
receive
public javax.jms.Message receive() throws javax.jms.JMSException
This call blocks indefinitely until a message is produced or until this message consumer is closed. When ConnectionState is stopped receive is paused.- Specified by:
receive
in interfacejavax.jms.MessageConsumer
- Returns:
- the next message produced for this message consumer, or null if this message consumer is closed during the receive call
- Throws:
javax.jms.JMSException
- On internal error
-
receive
public javax.jms.Message receive(long timeout) throws javax.jms.JMSException
This call blocks until a message arrives, the timeout expires, or this message consumer is closed. A timeout of zero never expires, and the call blocks indefinitely.- Specified by:
receive
in interfacejavax.jms.MessageConsumer
- Parameters:
timeout
- the timeout value (in milliseconds)- Returns:
- the next message produced for this message consumer, or null if the timeout expires or this message consumer is closed during the receive call
- Throws:
javax.jms.JMSException
- On internal error
-
receiveNoWait
public javax.jms.Message receiveNoWait() throws javax.jms.JMSException
Receives the next message if one is immediately available.- Specified by:
receiveNoWait
in interfacejavax.jms.MessageConsumer
- Returns:
- the next message produced for this message consumer, or null if no message is available
- Throws:
javax.jms.JMSException
- On internal error
-
close
public void close() throws javax.jms.JMSException
Closes the message consumer.This will not return until receives and/or message listeners in progress have completed. A blocked message consumer receive call returns null when this consumer is closed.
Since consumer prefetch threads use SQS long-poll feature with 20 seconds timeout, closing each consumer prefetch thread can take up to 20 seconds, which in-turn will impact the time on consumer close.
This method may be called from a message listener's onMessage method on its own consumer. After this method returns the onMessage method will be allowed to complete normally, and the callback scheduler thread will be closing the message consumer.
- Specified by:
close
in interfacejavax.jms.MessageConsumer
- Throws:
javax.jms.JMSException
- On internal error.
-
getMessageSelector
public String getMessageSelector() throws javax.jms.JMSException
This method is not supported.- Specified by:
getMessageSelector
in interfacejavax.jms.MessageConsumer
- Throws:
javax.jms.JMSException
-
stopPrefetch
protected void stopPrefetch()
This stops the prefetching
-
startPrefetch
protected void startPrefetch()
This starts the prefetching
-
-