This topic describes how to manage the visibility timeout of a message.
Background information
Simple Message Queue (formerly MNS) provides the VisibilityTimeout parameter for each message. After a worker process receives a message, SMQ starts to time for the visibility timeout. If a worker process does not finish processing a message within the specified timeout period, the message may be received and processed by other worker processes.
If you specify the VisibilityTimeout parameter for a message and a worker process fails to process a message, the message is not deleted and can be processed by other worker processes.
You can set a large value for the VisibilityTimeout parameter to ensure that a worker process has enough time to process messages.
Scenario
Assume that the value of the VisibilityTimeout parameter for a queue is set to 6 hours.
A worker process receives a message M1. After the worker process processes the message, the process fails or the host is restarted.
In this case, the message M1 cannot be received or processed by another worker process until six hours later. You can write code to shorten the time, but this will make the process more complicated.
Requirements
In specific scenarios, you need to process messages immediately after they are received. Assume that you have the following requirements:
The value of the VisibilityTimeout parameter is small, for example, 5 minutes. If a worker process fails, messages that have not been processed by the worker process can be received and processed by other worker processes within five minutes.
When a worker process processes a message, the connection must be maintained. This is because the process may take more than five minutes.
Solution
SMQ provides the C# sample code to resolve the issue described in the preceding scenario. Download the sample code. When a worker process processes a message, SMQ regularly checks whether to call the ChangeVisibility operation. After the worker process processes the message, the deleteMessage operation is called to delete the message.
For technical support about the demo usage,submit a ticket.
When you use the sample code, take note of the following information: