带有消息属性的主题推送示例

本文介绍基于Java SDK提供的携带消息属性的主题消息推送示例。

背景信息

轻量消息队列(原 MNS)在主题模型下,当订阅类型是邮件推送或短信服务时,允许在发送消息的时候指定消息属性,从而实现动态修改推送细节的能力。

前提条件

示例代码

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());
        // this client need only initialize once
        MNSClient client = account.getMNSClient();
    
        publishDysmsMessage(client, topicName, body);
        publishDmMessage(client, topicName, body);
    
        client.close();
    }
  
    /**
     * 发送消息到短信服务订阅端
     */
    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();
        // 动态修改短信服务订阅端接收消息的手机号码
        dysmsAttributes.setPhoneNumber("1350000****");
    
        MessageAttributes attributes = new MessageAttributes();
        attributes.setDysmsAttributes(dysmsAttributes);
    
        publishMessageWithAttributes(topic, message, attributes);
    }

    /**
     * 发送消息到邮件推送订阅端
     */
    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();
        // 动态修改邮件推送订阅端接收消息的邮箱地址
        dmAttributes.setMailAddress("test@example.com");
        // 动态修改邮件推送订阅端邮件主题
        dmAttributes.setSubject("test subject");
        // 动态修改邮件推送订阅端邮件正文格式为HTML,不指定默认为纯文本格式
        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 publish.");
            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("publish message error.");
        }
    }
}