閱讀148 返回首頁    go 小米 go 小米5


MQ 客戶端收發 MQTT 消息__Java 接入示例_MQTT 接入(物聯)_消息隊列 MQ-阿裏雲

MQ 除了提供標準的 MQTT 服務之外,還支持使用 MQ 客戶端來收發 MQTT 消息,實現 MQ 私有協議和 MQTT 協議之間的數據互通。這種方式一般用於 MQTT 移動端設備上傳的數據的後期處理,即在 ECS 上部署 MQ 的客戶端,分析處理 MQTT 上傳的數據。

MQ 消息和 MQTT 消息的對應關係如下圖所示:

mq2mqtt

使用 MQ 客戶端的相關說明,請參見 MQ TCP Java SDK 接入

1. MQ 客戶端發送 MQTT 消息

本小節介紹如何使用 MQ 的 SDK 向 MQTT 設備發送消息。使用 MQ 客戶端向 MQTT 設備發消息的方式和普通 MQ 客戶端發消息沒有區別,隻是要根據需求,將 MQTT 的二級 Topic 設置到 MQ 的消息屬性中。

  1. public class ONSSendMsg {
  2. public static void main(String[] args) throws InterruptedException {
  3. /**
  4. * 設置阿裏雲的AccessKey,用於鑒權
  5. */
  6. final String acessKey ="XXXXXX";
  7. /**
  8. * 設置阿裏雲的SecretKey,用於鑒權
  9. */
  10. final String secretKey ="XXXXXXX";
  11. /**
  12. * 發消息使用的一級Topic,需要先在MQ控製台裏申請
  13. */
  14. final String topic ="MQTTTestTopic";
  15. /**
  16. * ProducerID,需要先在MQ控製台裏申請
  17. */
  18. final String producerId ="PID_MQTTTestTopic";
  19. Properties properties =new Properties();
  20. properties.put(PropertyKeyConst.ProducerId, producerId);
  21. properties.put(PropertyKeyConst.AccessKey, acessKey);
  22. properties.put(PropertyKeyConst.SecretKey, secretKey);
  23. Producer producer = ONSFactory.createProducer(properties);
  24. producer.start();
  25. byte[] body=new byte[1024];
  26. final Message msg = new Message(
  27. topic,//MQ消息的Topic,需要事先申請
  28. "TagTest",//MQ Tag,可以進行消息過濾
  29. body);//消息體,和MQTT的body對應
  30. /**
  31. * 使用MQ客戶端給MQTT設備發送P2P消息時,需要在MQ消息中設置mqttSecondTopic屬性
  32. * 設置的值是“/p2p/”+目標ClientID
  33. */
  34. String targetClientID="GID_MQTTTestTopic@@@DeviceID_0001";
  35. msg.putUserProperties("mqttSecondTopic", "/p2p/"+targetClientID);
  36. //發送消息,隻要不拋異常就是成功。
  37. SendResult sendResult = producer.send(msg);
  38. System.out.println(sendResult);
  39. /**
  40. * 如果僅僅發送Pub/Sub消息,則隻需要設置實際MQTT訂閱的Topic即可,支持設置二級Topic
  41. */
  42. msg.putUserProperties("mqttSecondTopic", "/notice/");
  43. SendResult result =producer.send(msg);
  44. producer.shutdown();
  45. System.exit(0);
  46. }
  47. }

2. MQ 客戶端接收 MQTT 消息

本小節介紹如何使用 MQ 的 SDK 接收來自 MQTT 設備發送的消息。使用 MQ 客戶端接收 MQTT 設備的消息和普通 MQ 客戶端收消息沒有區別,MQ 客戶端隻需要訂閱 MQTT 的一級 Topic 即可。

  1. public class ONSRecvMsg {
  2. public static void main(String[] args) throws InterruptedException {
  3. /**
  4. * 設置阿裏雲的AccessKey,用於鑒權
  5. */
  6. final String acessKey ="XXXXXX";
  7. /**
  8. * 設置阿裏雲的SecretKey,用於鑒權
  9. */
  10. final String secretKey ="XXXXXXX";
  11. /**
  12. * 收消息使用的一級Topic,需要先在MQ控製台裏申請
  13. */
  14. final String topic ="MQTTTestTopic";
  15. /**
  16. * ConsumerID ,需要先在MQ控製台裏申請
  17. */
  18. final String consumerID ="GID_MQTTTestTopic";
  19. Properties properties =new Properties();
  20. properties.put(PropertyKeyConst.ConsumerId, consumerID);
  21. properties.put(PropertyKeyConst.AccessKey, acessKey);
  22. properties.put(PropertyKeyConst.SecretKey, secretKey);
  23. Consumer consumer =ONSFactory.createConsumer(properties);
  24. /**
  25. * 此處MQ客戶端隻需要訂閱MQTT的一級Topic即可
  26. */
  27. consumer.subscribe(topic, "*", new MessageListener() {
  28. public Action consume(Message message, ConsumeContext consumeContext) {
  29. System.out.println("recv msg:"+message);
  30. return Action.CommitMessage;
  31. }
  32. });
  33. consumer.start();
  34. System.out.println("[Case Normal Consumer Init] Ok");
  35. Thread.sleep(Integer.MAX_VALUE);
  36. consumer.shutdown();
  37. System.exit(0);
  38. }

最後更新:2016-11-23 16:04:20

  上一篇:go JavaScript 發消息示例__JavaScript 接入示例_MQTT 接入(物聯)_消息隊列 MQ-阿裏雲
  下一篇:go MQTT 客戶端收發 MQTT 消息__Java 接入示例_MQTT 接入(物聯)_消息隊列 MQ-阿裏雲