This article describes how to use MNS to transfer a message whose size is larger than 64 KB without slicing the message.
The maximum size of a message in an MNS queue is 64 KB. This limit can meet the needs of data exchanges in most cases when throttling is implemented. In this case, if a message is larger than 64 KB, it must be sliced.
MNS provides a solution that allows you to transmit a message whose size is larger than 64 KB without slicing the message.
- If a producer client sends a message whose size is larger than 64 KB to MNS, the client uploads the message to Object Storage Service (OSS).
- The producer client sends the OSS object information that contains the message to MNS.
- The consumer client reads the message from the MNS queue and determines whether the message content is the information of the OSS object.
- If the message content is the information of the OSS object, the consumer client downloads the OSS object and returns the message content to the upper-layer application.
The following figure shows the message transfer process.
MNS provides the Java sample code to transfer oversized messages. To download the sample code, click here. The BigMessageSizeQueue class encapsulates all required features.
BigMessageSizeQueue provides the following public methods:
// Specify a constructor. cq is a common mnsqueue object. The ossClient and ossBucketName include the information of the OSS region and bucket that are used to transfer oversized messages. public BigMessageSizeQueue(CloudQueue cq, OSSClient ossClient, String ossBucketName) // Send a message. public Message putMessage(Message message) // Receive the message. public Message popMessage(int waitSeconds) // Delete the message. public void deleteMessage(String receiptHandle) // Specify the maximum size of a message. If the size of a message exceeds the limit, the message is transferred by using OSS. Default value: 64 KB. public void setBigMessageSize(long bigMessageSize) // Specify whether to delete the OSS object that contains the message. Default value: yes. public void setNeedDeleteMessageObjectOnOSSFlag(boolean flag)
For more information about the sample code, see the Demo.java file.
- Transferring oversized messages consumes a large amount of network bandwidth. A bandwidth bottleneck may occur on the producer and consumer clients.
- Transferring oversized messages is time-consuming and may be affected by network jitter. We recommend that you perform retries at the upper layer if errors occur.