There are two ways 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.permit,Parameter topics= icbu_trade_OrderNotify
Official Environment Service address: ws://mc.api.taobao.com/
Pre Test GateWay ws://premc.api.taobao.com/
Official Environment Service address : https://api.taobao.com/router/rest
Pre Test GateWay http://pre-gw.api.taobao.com/top/router/rest
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) ;
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/");
Basic steps:
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=***********×tamp=2021-01-28+21%3A07%3A12&partner_id=top-sdk-java-20201024&format=json&sign_method=hmac-sha256
response:
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=***************×tamp=2021-01-28+21%3A16%3A15&partner_id=top-sdk-java-20201024&format=json&sign_method=hmac-sha256