Setting Visibility Timeout in |SQS|
When a message is received in |SQS|, it remains on the queue until it's deleted in order to ensure receipt. A message that was received, but not deleted, will be available in subsequent requests after a given visibility timeout to help prevent the message from being received more than once before it can be processed and deleted.
Note
When using :sqs-dg:`standard queues <standard-queues>`, visibility timeout isn't a guarantee against receiving a message twice. If you are using a standard queue, be sure that your code can handle the case where the same message has been delivered more than once.
When you have received a message, you can modify its visibility timeout by passing its receipt handle in a :aws-java-class:`ChangeMessageVisibilityRequest <services/sqs/model/ChangeMessageVisibilityRequest>` that you pass to the |sqsclient| class' :methodname:`changeMessageVisibility` method.
Imports
.. literalinclude:: example_code/sqs/src/main/java/aws/example/sqs/VisibilityTimeout.java :lines: 16-17 :language: java
Code
.. literalinclude:: example_code/sqs/src/main/java/aws/example/sqs/VisibilityTimeout.java :lines: 31-39 :dedent: 8 :language: java
See the :sdk-examples-java-sqs:`complete example <VisibilityTimeout.java>` on GitHub.
To set the message visibility timeout for multiple messages at once, create a list of :aws-java-class:`ChangeMessageVisibilityBatchRequestEntry <services/sqs/model/ChangeMessageVisibilityBatchRequestEntry>` objects, each containing a unique ID string and a receipt handle. Then, pass the list to the |sqs| client class' :methodname:`changeMessageVisibilityBatch` method.
Imports
.. literalinclude:: example_code/sqs/src/main/java/aws/example/sqs/VisibilityTimeout.java :lines: 16-18, 21-22 :language: java
Code
.. literalinclude:: example_code/sqs/src/main/java/aws/example/sqs/VisibilityTimeout.java :lines: 46-67 :dedent: 8 :language: java
See the :sdk-examples-java-sqs:`complete example <VisibilityTimeout.java>` on GitHub.
- :sqs-dg:`Visibility Timeout <sqs-visibility-timeout>` in the |sqs-dg|
- :sqs-api:`SetQueueAttributes` in the |sqs-api|
- :sqs-api:`GetQueueAttributes` in the |sqs-api|
- :sqs-api:`ReceiveMessage` in the |sqs-api|
- :sqs-api:`ChangeMessageVisibility` in the |sqs-api|
- :sqs-api:`ChangeMessageVisibilityBatch` in the |sqs-api|