文档中心 > Developing Documents

There are two ways to consume messages:

  1. Establish a long connection and listen to messages. It only works on java sdk and C# SDK.
  2. You can call this operation to pull messages in any language.

1. Enable the permission to consume messages

Contact alibaba open staff to help you subscribe to messages.


Then Before you listen to the message topic ( like icbu_trade_OrderNotify), you need to authorize messages for each user first, and you need to call taobao.tmc.user.permitParameter topics= icbu_trade_OrderNotify

2. Formal environment

  1. For SDK(java sdk and C# SDK):

Official Environment Service address: ws://mc.api.taobao.com/

Pre Test GateWay ws://premc.api.taobao.com/


  1. For API pull message:

Official Environment Service address : https://api.taobao.com/router/rest

Pre Test GateWay http://pre-gw.api.taobao.com/top/router/rest

3. Consumption in long connection mode

java SDK demo

TmcClient client = new TmcClient("app_key", "app_secret", "default"); // 关于default参考消息分组说明
client.setMessageHandler(new MessageHandler() {
    public void onMessage(Message message, MessageStatus status) {
        try {
            System.out.println(message.getContent());
            System.out.println(message.getTopic());
        } catch (Exception e) {
            e.printStackTrace();
            status.fail(); // 消息处理失败回滚,服务端需要重发
          // 重试注意:不是所有的异常都需要系统重试。 
          // 对于字段不全、主键冲突问题,导致写DB异常,不可重试,否则消息会一直重发
          // 对于,由于网络问题,权限问题导致的失败,可重试。
          // 重试时间 5分钟不等,不要滥用,否则会引起雪崩
        }
    }
});
client.connect("ws://mc.api.taobao.com"); // 消息环境地址:ws://mc.api.tbsandbox.com/

note: Thread. Sleep (1000) ;

C# use Sample code

TmcClient client = new TmcClient("appkey", "appsecret", "default"); // 关于default参考消息分组说明
client.OnMessage += (s, e) =>
{
    try
    {
        Console.WriteLine(e.Message.Topic);
        Console.WriteLine(e.Message.Content);
        // 默认不抛出异常则认为消息处理成功
    }
    catch (Exception exp)
    {
        Console.WriteLine(exp.StackTrace);
        e.Fail(); // 消息处理失败回滚,服务端需要重发
        // 重试注意:不是所有的异常都需要系统重试。 
        //对于字段不全、主键冲突问题,导致写DB异常,不可重试,否则消息会一直重发
        // 对于,由于网络问题,权限问题导致的失败,可重试。
        // 重试时间 5分钟不等,不要滥用,否则会引起雪崩
    }
};
client.Connect("ws://mc.api.taobao.com/"); 


4. API pull messages

Basic steps:

  1. First consume message: API name: taobao.tmc.messages.consume After the message is consumed, the pointer automatically moves backward. The next call automatically obtains the message that has not been consumed, but the message after consumption confirmation cannot be obtained again.
  2. Then confirm the message: API name: taobao.tmc.messages.confirm After the message is obtained, if it is not confirmed, the message service selects the time to resend the message. The number of resend times is controlled by the message service. If the message is not confirmed within one day, it will be deleted.

HTTP Request DEMO:

  1. invoke consume api taobao.tmc.messages.consume :

https://api.taobao.com/router/rest?group_name=default&quantity=100&app_key=YOUR_APP_KEY&method=taobao.tmc.messages.consume&v=2.0&sign=***********&timestamp=2021-01-28+21%3A07%3A12&partner_id=top-sdk-java-20201024&format=json&sign_method=hmac-sha256

image.png


response:

image.png


  1. next invoke confirm Api taobao.tmc.messages.confirm :


https://api.taobao.com/router/rest?group_name=default&s_message_ids=41106722229471922&app_key=YOUR_APP_KEY&method=taobao.tmc.messages.confirm&v=2.0&sign=***************&timestamp=2021-01-28+21%3A16%3A15&partner_id=top-sdk-java-20201024&format=json&sign_method=hmac-sha256


image.png

FAQ

关于此文档暂时还没有FAQ
返回
顶部