This topic describes the best practice of the long polling mechanism provided by MNS.

Background information

  • MNS provides a long polling mechanism for the ReceiveMessage operation. Valid values of the WaitSecond parameter are between 1 and 30. An MNS server holds long polling requests until messages are available on the server. The long polling mechanism allows you to receive messages in real time without the need to send a large number of invalid requests. We recommend that you use the long polling mechanism provided by MNS.

    For more information about how to specify the parameters for long polling, see ReceiveMessage.

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


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

In this case, the MNS server may return the error message "The message does not exist".

If you use a while loop to send long polling requests without resolving the error, a large number of requests may be sent to the server.


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 for long polling based on your needs. When the specified threads detect a message in the queue, they can wake up other threads to receive the message.

MNS provides the MessageReceiver class to retrieve messages. For more information about the sample code, see the LimitedLongPolling package. An object named MessageReceiver is created for each thread and the object calls the receiver.receiveMessage operation to retrieve messages.

MessageReceiver provides an exclusive long polling mechanism. When an MNS server uses one thread to hold long polling requests, other threads are in the Waiting state.