This topic describes the terms, scenarios, and solutions related to manage the visibility timeout of a message when it is consumed by customers.
Terms
Visible: A visible message can be read and received by numerous consumers. A consumer is a service or application that receives and processes messages.
Invisible: After a message is received by a single consumer, it becomes invisible and cannot be received by other consumers. This prevents other consumers from processing the same message.
Visibility timeout period: The period from when a message is received by a consumer (invisible to other consumers) to when it can be received by other consumers again (visible to other consumers).
After a message is received by a single consumer, it becomes invisible and cannot be received by other consumers. The invisible status depends on visibility timeout period that prevents other consumers from processing the same message.
For more information about visibility timeout period, see Visibility timeout period.
Scenario
Simple Message Queue (SMQ,formerly MNS) provides the VisibilityTimeout parameter for each message. After a consumer receives a message, SMQ starts to time for the visibility timeout. If a consumer does not finish processing a message within the specified timeout period, the message may be received and processed by other consumers. If you specify the VisibilityTimeout parameter for a message and a consumer fails to process a message, the message is not deleted and can be processed by other consumers.
You can set a large value for the VisibilityTimeout parameter to ensure that a consumer has enough time to process messages. Assume that the value of the VisibilityTimeout parameter for a queue is set to 6 hours. A consumer receives a message M1. After the consumer processes the message, the process fails or the host is restarted. In this case, the message M1 cannot be received or processed by another consumer until six hours later. You can write code to shorten the time, but this will make the process more complicated.
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 consumer fails, messages that have not been processed by the consumer can be received and processed by other consumers within five minutes.
When a consumer processes a message, the connection must be maintained. This is because the process may take more than five minutes.
Solution
Simple Message Queue (SMQ, formerly MNS)provides the C# sample code to resolve the issue described in the preceding scenario. The process is: When a consumer processes a message, SMQ regularly checks whether to call the ChangeVisibility operation. After the consumer processes the message, the deleteMessage operation is called to delete the message.
For more information about the how to download C# sample code, see SDK for C++. For technical support about the demo usage, submit a ticket.
When you use the sample code, take note of the following information:
Before you run the code, you must configure the
accessId,accessKey, andEndPoint.Variables:
Variables
Description
MessageMinimalLife
The minimum lifetime that is required when a message is registered. For example, if a message will time out 0.1 seconds after it is registered, SMQ does not have sufficient time to call the ChangeVisibility operation to extend the lifetime of the message. Therefore, MessageMinimalLife ensures that a message does not time out before SMQ can call the ChangeVisibility operation to extend the lifetime of the message. You can specify the MessageMinimalLife parameter based on your business needs.
TimerInterval
The interval that the internal timer of the manager uses for timing. You must make sure that the timer is started before the specified lifetime of a message ends. You can set a small value for the TimerInterval parameter so that messages are checked at short intervals.
QueueMessageVisibilityTimeout
The default timeout period of a message in a queue. When you use the sample code to call the ChangeVisibility operation, the value of the VisibilityTimeout parameter is the same as the one specified for the QueueMessageVisibilityTimeout parameter. Therefore, the value of the QueueMessageVisibilityTimeout parameter must be greater than the sum of the values of the TimerInterval and MessageMinmalLife parameters. Otherwise, the message may time out.
MessageTimeout
The timeout period of a message in the manager. For example, if a consumer fails, the manager no longer changes the visibility of the message. Then, the message times out.
Process:
After a message is received, a consumer registers and processes the message, and then calls the Manager.
deleteMessageoperation to delete the message.After a message is registered, the manager calls the ThreadPool.ChangeVisibilityTask operation to check whether the ChangeVisibility operation is required. Then, the manager adds the message to the internal message list.
The timer in the manager regularly calls the Parallel.ChangeVisibilityTask operation to check all messages in the message list.
The following figure describes the process that is used to manage the visibility timeout of messages by calling the Manager.ChangeMessageVisibility (ChangeVisibilityTask) operation.
The following figure describes the procedure of managing the visibility timeout of messages: