Class SQSMessageConsumer

java.lang.Object
com.amazon.sqs.javamessaging.SQSMessageConsumer
All Implemented Interfaces:
jakarta.jms.MessageConsumer, jakarta.jms.QueueReceiver, AutoCloseable

public class SQSMessageConsumer extends Object implements jakarta.jms.MessageConsumer, jakarta.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
     
    static final int
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the message consumer.
    jakarta.jms.MessageListener
    Gets the message consumer's MessageListener.
    This method is not supported.
    jakarta.jms.Queue
    Gets the queue destination associated with this queue receiver, where the messages are delivered from.
    jakarta.jms.Message
    This call blocks indefinitely until a message is produced or until this message consumer is closed.
    jakarta.jms.Message
    receive(long timeout)
    This call blocks until a message arrives, the timeout expires, or this message consumer is closed.
    jakarta.jms.Message
    Receives the next message if one is immediately available.
    void
    setMessageListener(jakarta.jms.MessageListener listener)
    Sets the message consumer's MessageListener.
    protected void
    This starts the prefetching
    protected void
    This stops the prefetching

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • PREFETCH_EXECUTOR_GRACEFUL_SHUTDOWN_TIME

      public static final int PREFETCH_EXECUTOR_GRACEFUL_SHUTDOWN_TIME
      See Also:
    • closed

      protected volatile boolean closed
  • Method Details

    • getQueue

      public jakarta.jms.Queue getQueue() throws jakarta.jms.JMSException
      Gets the queue destination associated with this queue receiver, where the messages are delivered from.
      Specified by:
      getQueue in interface jakarta.jms.QueueReceiver
      Returns:
      a queue destination
      Throws:
      jakarta.jms.JMSException
    • getMessageListener

      public jakarta.jms.MessageListener getMessageListener() throws jakarta.jms.JMSException
      Gets the message consumer's MessageListener.
      Specified by:
      getMessageListener in interface jakarta.jms.MessageConsumer
      Returns:
      a message listener
      Throws:
      jakarta.jms.JMSException
    • setMessageListener

      public void setMessageListener(jakarta.jms.MessageListener listener) throws jakarta.jms.JMSException
      Sets the message consumer's MessageListener.
      Specified by:
      setMessageListener in interface jakarta.jms.MessageConsumer
      Parameters:
      listener - a message listener to use for asynchronous message delivery
      Throws:
      jakarta.jms.JMSException - If the message consumer is closed
    • receive

      public jakarta.jms.Message receive() throws jakarta.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 interface jakarta.jms.MessageConsumer
      Returns:
      the next message produced for this message consumer, or null if this message consumer is closed during the receive call
      Throws:
      jakarta.jms.JMSException - On internal error
    • receive

      public jakarta.jms.Message receive(long timeout) throws jakarta.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 interface jakarta.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:
      jakarta.jms.JMSException - On internal error
    • receiveNoWait

      public jakarta.jms.Message receiveNoWait() throws jakarta.jms.JMSException
      Receives the next message if one is immediately available.
      Specified by:
      receiveNoWait in interface jakarta.jms.MessageConsumer
      Returns:
      the next message produced for this message consumer, or null if no message is available
      Throws:
      jakarta.jms.JMSException - On internal error
    • close

      public void close() throws jakarta.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 interface AutoCloseable
      Specified by:
      close in interface jakarta.jms.MessageConsumer
      Throws:
      jakarta.jms.JMSException - On internal error.
    • getMessageSelector

      public String getMessageSelector() throws jakarta.jms.JMSException
      This method is not supported.
      Specified by:
      getMessageSelector in interface jakarta.jms.MessageConsumer
      Throws:
      jakarta.jms.JMSException
    • stopPrefetch

      protected void stopPrefetch()
      This stops the prefetching
    • startPrefetch

      protected void startPrefetch()
      This starts the prefetching