This topic describes a best practice of the long polling mechanism of Message Service (MNS).

Background information

MNS provides a long polling mechanism for the ReceiveMessage operation. To use the mechanism, set the WaitSecond parameter to a value between 1 and 30 when you call the ReceiveMessage operation. An MNS server holds long polling requests until messages are available on the server. The long polling mechanism allows you to receive messages in a timely manner and eliminates your needs to send a large number of invalid requests. We recommend that you use the long polling mechanism. For more information about how to specify the parameters when you receive messages, see ReceiveMessage.

HTTP long polling connections remain effective on MNS servers within a specified period and consume only a few resources. To avoid malicious attacks, MNS limits the number of connections to each queue for a single user. For more information about the limit, see Limits.


You may use hundreds of threads at a time on a single host to access an MNS queue to retrieve messages. When no messages exist in the queue, hundreds of long polling requests are held on the host. If you use a large number of hosts, thousands of long polling requests are sent at a time.

In this case, an error "The message does not exist" is prone to occur and the server will not hold the requests.

You need to use the long polling mechanism in an effective manner to retrieve expected effects. If you use a While hoop to send long polling requests without specifying exception-handling logic, you may find that a large number of requests are sent after a certain period of time.


If you use hundreds of threads to access an MNS queue at a time and no message exists in the queue, you do not need to hold all the threads on the server. You can specify whether to hold one or more threads based on your needs. When the specified threads find a message in the queue, they can wake other threads to receive the message.

MNS provides the MessageReceiver class that is used to retrieve messages. For more information about the sample code, see the LimitedLongPolling package. To download the package, click here. A MessageReceiver object is created for each thread and the object uses the receiver.receiveMessage operation to retrieve messages.

MessageReceiver provides an exclusive long polling mechanism. An MNS server only needs to hold one thread, which will wake other threads in the waiting state.