实例介绍
【实例简介】
【实例截图】
【核心代码】
/** * aliyun.com Inc. * Copyright (c) 2004-2017 All Rights Reserved. */ package com.aliyun.iot.demo.iothub; import java.net.InetAddress; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; import java.util.concurrent.TimeUnit; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.IMqttMessageListener; import org.eclipse.paho.client.mqttv3.MqttCallback; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import com.aliyun.iot.util.LogUtil; import com.aliyun.iot.util.SignUtil; /** * IoT套件JAVA版设备接入demo */ public class SimpleClient4IOT { /******这里是客户端需要的参数*******/ public static String deviceName = "huakuangxxxxx"; public static String productKey = "axxxxxxx"; public static String secret = "6sllz6mmMeuZpri9Jnnhwxxxx"; //用于测试的topic private static String subTopic = "/" productKey "/" deviceName "/get"; private static String pubTopic = "/" productKey "/" deviceName "/update"; public static void main(String... strings) throws Exception { //客户端设备自己的一个标记,建议是MAC或SN,不能为空,32字符内 String clientId = InetAddress.getLocalHost().getHostAddress(); //设备认证 Map<String, String> params = new HashMap<String, String>(); params.put("productKey", productKey); //这个是对应用户在控制台注册的 设备productkey params.put("deviceName", deviceName); //这个是对应用户在控制台注册的 设备name params.put("clientId", clientId); String t = System.currentTimeMillis() ""; params.put("timestamp", t); //MQTT服务器地址,TLS连接使用ssl开头 String targetServer = "ssl://" productKey ".iot-as-mqtt.cn-shanghai.aliyuncs.com:1883"; //客户端ID格式,两个||之间的内容为设备端自定义的标记,字符范围[0-9][a-z][A-Z] String mqttclientId = clientId "|securemode=2,signmethod=hmacsha1,timestamp=" t "|"; String mqttUsername = deviceName "&" productKey; //mqtt用户名格式 String mqttPassword = SignUtil.sign(params, secret, "hmacsha1"); //签名 System.err.println("mqttclientId=" mqttclientId "&mqttPassword=" mqttPassword); connectMqtt(targetServer, mqttclientId, mqttUsername, mqttPassword, deviceName); } public static void connectMqtt(String url, String clientId, String mqttUsername, String mqttPassword, final String deviceName) throws Exception { MemoryPersistence persistence = new MemoryPersistence(); SSLSocketFactory socketFactory = createSSLSocket(); final MqttClient sampleClient = new MqttClient(url, clientId, persistence); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setMqttVersion(4); // MQTT 3.1.1 connOpts.setSocketFactory(socketFactory); //设置是否自动重连 connOpts.setAutomaticReconnect(true); //如果是true,那么清理所有离线消息,即QoS1或者2的所有未接收内容 connOpts.setCleanSession(false); connOpts.setUserName(mqttUsername); connOpts.setPassword(mqttPassword.toCharArray()); connOpts.setKeepAliveInterval(65); LogUtil.print(clientId "进行连接, 目的地: " url); sampleClient.connect(connOpts); 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())); } }); LogUtil.print("连接成功:---"); //这里测试发送一条消息 String content = "{'content':'msg from :" clientId "," System.currentTimeMillis() "'}"; MqttMessage message = new MqttMessage(content.getBytes("utf-8")); message.setQos(0); //System.out.println(System.currentTimeMillis() "消息发布:---"); sampleClient.publish(pubTopic, message); //一次订阅永久生效 //这个是第一种订阅topic方式,回调到统一的callback sampleClient.subscribe(subTopic); //这个是第二种订阅方式, 订阅某个topic,有独立的callback //sampleClient.subscribe(subTopic, new IMqttMessageListener() { // @Override // public void messageArrived(String topic, MqttMessage message) throws Exception { // // LogUtil.print("收到消息:" message ",topic=" topic); // } //}); //回复RRPC响应 final ExecutorService executorService = new ThreadPoolExecutor(2, 4, 600, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100), new CallerRunsPolicy()); String reqTopic = "/sys/" productKey "/" deviceName "/rrpc/request/ "; sampleClient.subscribe(reqTopic, new IMqttMessageListener() { @Override public void messageArrived(String topic, MqttMessage message) throws Exception { LogUtil.print("收到请求:" message ", topic=" topic); String messageId = topic.substring(topic.lastIndexOf('/') 1); final String respTopic = "/sys/" productKey "/" deviceName "/rrpc/response/" messageId; String content = "hello world"; final MqttMessage response = new MqttMessage(content.getBytes()); response.setQos(0); //RRPC只支持QoS0 //不能在回调线程中调用publish,会阻塞线程,所以使用线程池 executorService.submit(new Runnable() { @Override public void run() { try { sampleClient.publish(respTopic, response); LogUtil.print("回复响应成功,topic=" respTopic); } catch (Exception e) { e.printStackTrace(); } } }); } }); } private static SSLSocketFactory createSSLSocket() throws Exception { SSLContext context = SSLContext.getInstance("TLSV1.2"); context.init(null, new TrustManager[] {new ALiyunIotX509TrustManager()}, null); SSLSocketFactory socketFactory = context.getSocketFactory(); return socketFactory; } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论