This topic describes how to use Message Service (MNS) to transmit a message that has a size larger than 64 KB without slicing the message.
The maximum size of a message in an MNS queue is 64 KB. This limit can typically meet the needs of data communications under flow control. If a message is larger than 64 KB, it is oversized and must sliced.
MNS provides a solution that allows you to transmit a message that has a size larger than 64 KB without slicing the message.
- If a producer client sends a message that has a size larger than 64 KB, the client uploads the message to Object Storage Service (OSS).
- The producer client sends the information about the OSS object 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 about the OSS object.
- If the message content is the information about the OSS object, the consumer client downloads the OSS object and returns the message content to the upper-layer program.
- If a message has a size of smaller than 64 KB, the message is sent to MNS.
The following figure shows the procedure.
MNS provides the Java sample code to solve the problem. To download the sample code, click here. The BigMessageSizeQueue class encapsulates all required features.
BigMessageSizeQueue provides the following public methods:
public BigMessageSizeQueue(CloudQueue cq, OSSClient ossClient, String ossBucketName) //Specify a constructor. cq is a common mnsqueue object. ossClient and ossBucketName include the information about the OSS region and bucket that are used to transfer oversized messages. public Message putMessage(Message message) //Send a message. public Message popMessage(int waitSeconds) //Receive the message. public void deleteMessage(String receiptHandle) //Delete the message. public void setBigMessageSize(long bigMessageSize) //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 setNeedDeleteMessageObjectOnOSSFlag(boolean flag) //Specify whether to delete the OSS object that contains the message. Default value: yes.
For more information about the sample code, see the Demo.java file.
- Transmitting messages of a large size consumes a large amount of network bandwidth. A bottleneck may occur on the network bandwidth of producer and consumer clients.
- Transmitting messages of a large size is time-consuming and can cause network jitter. We recommend that you retry transmitting messages at the upper layer if errors occur.