All Products
Search
Document Center

Simple Message Queue (formerly MNS):Example of pushing a topic message with message attributes

Last Updated:Sep 23, 2025

This topic provides an example of how to push a topic message with message attributes using the Java SDK.

Background information

Simple Message Queue (formerly MNS) uses a topic-based model. If a subscription is for Direct Mail or Short Message Service, you can specify message attributes when you send a message. This lets you dynamically change the push details.

Prerequisites

Example code

import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudTopic;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.common.utils.ServiceSettings;
import com.aliyun.mns.model.DmAttributes;
import com.aliyun.mns.model.DysmsAttributes;
import com.aliyun.mns.model.MessageAttributes;
import com.aliyun.mns.model.RawTopicMessage;
import com.aliyun.mns.model.TopicMessage;

public class PublishMessageWithAttributesDemo {

    public static void main(String[] args) {
        String topicName = "TestTopic";
        String body = "hello world!";
        CloudAccount account = new CloudAccount(ServiceSettings.getMNSAccountEndpoint());
        // Initialize this client only once.
        MNSClient client = account.getMNSClient();
    
        publishDysmsMessage(client, topicName, body);
        publishDmMessage(client, topicName, body);
    
        client.close();
    }
  
    /**
     * Sends a message to a Short Message Service subscriber.
     */
    private static void publishDysmsMessage(MNSClient client, String topicName, String body) {
        CloudTopic topic = client.getTopicRef(topicName);
        RawTopicMessage message = new RawTopicMessage();
        message.setMessageBody(body);
    
        DysmsAttributes dysmsAttributes = new DysmsAttributes();
        // Dynamically change the recipient's mobile number for the Short Message Service subscription.
        dysmsAttributes.setPhoneNumber("1350000****");
    
        MessageAttributes attributes = new MessageAttributes();
        attributes.setDysmsAttributes(dysmsAttributes);
    
        publishMessageWithAttributes(topic, message, attributes);
    }

    /**
     * Sends a message to a Direct Mail subscriber.
     */
    private static void publishDmMessage(MNSClient client, String topicName, String body) {
        CloudTopic topic = client.getTopicRef(topicName);
        RawTopicMessage message = new RawTopicMessage();
        message.setMessageBody(body);
    
        DmAttributes dmAttributes = new DmAttributes();
        // Dynamically change the recipient's email address for the Direct Mail subscription.
        dmAttributes.setMailAddress("test@example.com");
        // Dynamically change the email subject for the Direct Mail subscription.
        dmAttributes.setSubject("test subject");
        // Dynamically change the email body format to HTML for the Direct Mail subscription. The default format is plain text.
        dmAttributes.setIsHtml(true);
    
        MessageAttributes attributes = new MessageAttributes();
        attributes.setDmAttributes(dmAttributes);
    
        publishMessageWithAttributes(topic, message, attributes);
    }

    private static void publishMessageWithAttributes(CloudTopic topic, RawTopicMessage message, MessageAttributes attributes) {
        try {
            TopicMessage publishResultMsg = topic.publishMessage(message, attributes);
            System.out.println("Message published.");
            System.out.println("reqId: " + publishResultMsg.getRequestId());
            System.out.println("msgId: " + publishResultMsg.getMessageId());
            System.out.println("md5: " + publishResultMsg.getMessageBodyMD5());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Failed to publish the message.");
        }
    }
}