设备端代码(mqttClient):https://help.aliyun.com/document_detail/42648.html?spm=5176.doc30579.6.569.ZEgA1g
服务端代码(aliyun-java-sdk-iot):https://help.aliyun.com/document_detail/30579.html?spm=5176.doc42693.6.597.WhnZQa
消息代码(aliyun-sdk-mns-samples):https://help.aliyun.com/document_detail/27508.html?spm=5176.doc27509.6.648.9hGlrb
https://help.aliyun.com/document_detail/42693.html?spm=5176.doc50633.6.573.wlKHyi
https://help.aliyun.com/document_detail/27509.html?spm=5176.product27412.6.652.uIO9HW
一、设备端发送消息到服务端流程
1、终端发送消息到物联网套件
SimpleClient4IOT/connectMqtt:sampleClient.publish(pubTopic,message);
String content = "客户端发送消息到服务端";
MqttMessage message = new MqttMessage(content.getBytes("utf-8"));
message.setQos(0);//消息qos 0:最多一次,1:至少一次
sampleClient.publish(pubTopic, message);//发送数据到某个topic
System.out.println("发送消息成功");
2、物联网套件发送消息到消息服务
通过上一篇 阿里云学习之IOT物联网套件(配置篇),讲解物联网套件和消息服务的具体配置流程和走向
规则引擎——>主题——>订阅——>队列
3、消息服务发送消息到服务端,服务端接收消息
ComsumerDemo/main:queue.popMessage();
MNSClient client = account.getMNSClient(); //this client need only initialize once
CloudQueue queue = client.getQueueRef("aliyun-iot-UGlr70uPYVM");// replace with your queue name
System.out.println("开始调试...");
for (int i = 0; i < 20; i++)
{
Message popMsg = queue.popMessage();
System.out.println("消息为空...");
if (popMsg != null){
System.out.println("message handle: " + popMsg.getReceiptHandle());
System.out.println("message body: " + popMsg.getMessageBodyAsString());
System.out.println("message id: " + popMsg.getMessageId());
System.out.println("message dequeue count:" + popMsg.getDequeueCount());
//<<to add your special logic.>>
//remember to delete message when consume message successfully.
queue.deleteMessage(popMsg.getReceiptHandle());
System.out.println("delete message successfully.\n");
}
}
二、服务端发送消息到设备端流程
1、服务端发送消息到物联网套件
String accessKey = "账号key";
String accessSecret = "账号Secret";
try {
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
} catch (ClientException e) {
e.printStackTrace();
}
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客户端
PubRequest request = new PubRequest();
request.setProductKey("产品key");
request.setMessageContent(Base64.encodeBase64String("消息服务".getBytes()));
request.setTopicFullName("topic");// /UGlr70uPYVM/bulb/get
request.setQos(0); //目前支持QoS0和QoS1 PubResponse response; try { response = client.getAcsResponse(request); System.out.println(response.getSuccess()); System.out.println(response.getErrorMessage()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); }
2、设备端接收消息
sampleClient.setCallback(new MqttCallback() {
@Override
/*public void connectionLost(Throwable cause) {
LogUtil.print("连接失败,原因:" + cause);
cause.printStackTrace();
}*/
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
LogUtil.print("接收到消息,来至Topic [" + topic + "] , 内容是:["
+ new String(message.getPayload(), "UTF-8") + "], ");
}
@Override
/*public void deliveryComplete(IMqttDeliveryToken token) {
//如果是QoS0的消息,token.resp是没有回复的
LogUtil.print("消息发送成功! " + ((token == null || token.getResponse() == null) ? "null"
: token.getResponse().getKey()));
}*/
});
来源:oschina
链接:https://my.oschina.net/u/4314317/blog/4220610