标签:计费 计算 中断 code package 通过 ram 开发 rtti
什么是MQTT
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分。该协议支持所有平台,几乎可以把所有联网物品和外部连接起来,是一种基于发布/订阅(publish/subscribe)模式的“轻量级”通讯协议,该协议构建于TCP/IP协议上,被用来当做传感器和制动器(比如通过Twitter让房屋联网)的通信协议。
MQTT特点
MQTT协议是为大量计算能力有限,且工作在低带宽、不可靠的网络的远程传感器和控制设备通讯而设计的协议,它具有以下主要的几项特性:
1、使用发布/订阅消息模式,提供一对多的消息发布,解除应用程序耦合;
2、对负载内容屏蔽的消息传输;
3、使用 TCP/IP 提供网络连接;
4、有三种消息发布服务质量:
package com.joysuccess.testmqtt; import com.alibaba.fastjson.JSON; import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * Created by ${zhangxufeng} on 2018/6/28. */ public class Server { //tcp://MQTT安装的服务器地址:MQTT定义的端口号 public static final String HOST = "tcp://localhost:12333"; //定义一个主题 public static final String TOPIC = "XXXXXXXXXXXXX"; //定义MQTT的ID,可以在MQTT服务配置中指定 private static final String clientid = "server11"; private MqttClient client; private MqttTopic topic11; private String userName = "admin"; private String passWord = "admin"; private MqttMessage message; /** * 构造函数 * @throws MqttException */ public Server() throws MqttException { // MemoryPersistence设置clientid的保存形式,默认为以内存保存 client = new MqttClient(HOST, clientid, new MemoryPersistence()); connect(); } /** * 用来连接服务器 */ private void connect() { MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(false); options.setUserName(userName); options.setPassword(passWord.toCharArray()); // 设置超时时间 options.setConnectionTimeout(10); // 设置会话心跳时间 options.setKeepAliveInterval(20); try { client.setCallback(new PushCallback()); client.connect(options); topic11 = client.getTopic(TOPIC); } catch (Exception e) { e.printStackTrace(); } } /** * * @param topic * @param message * @throws MqttPersistenceException * @throws MqttException */ public void publish(MqttTopic topic , MqttMessage message) throws MqttPersistenceException, MqttException { MqttDeliveryToken token = topic.publish(message); token.waitForCompletion(); System.out.println("message is published completely! " + token.isComplete()); } /** * 启动入口 * @param args * @throws MqttException */ public static void main(String[] args) throws MqttException { Server server = new Server(); server.message = new MqttMessage(); server.message.setQos(1); server.message.setRetained(true); Map<String,Object> map = new HashMap<>(); Map<String,Object> map1 = new HashMap<>();//灯的颜色选择,r:红,g:绿,b:蓝,0闭,1长亮,大于2闪烁,大于2时数值为闪烁频率(500,每500ms闪烁一次) map1.put("r",0); map1.put("g",500); map1.put("b",0); map.put("moit","04D"); map.put("sat","i7"); map.put("cl","lxd"); map.put("pas",map1); map.put("pe","exing"); map.put("tut",1000); map.put("pry",0); map.put("oor","admin"); Date date = new Date(); DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制; String format = dFormat.format(date); String[] split = format.split(" "); String startTime = split[0]+"T"+split[1]+"Z";//拼接时间格式 map.put("startTime",startTime); server.message.setPayload( JSON.toJSONString(map).getBytes());//转成JSON格式 server.publish(server.topic11 , server.message); if (server.client.isConnected()){ server.client.disconnect();//关闭连接 } } }
Subscribe如下:
package com.joysuccess.testmqtt; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttTopic; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import java.util.concurrent.ScheduledExecutorService; /** * Created by ${zhangxufeng} on 2018/6/28. */ public class PubMsg { public static final String HOST = "tcp://localhost:12333"; public static final String TOPIC = "xxxxxxxxx"; private static final String clientid = "client11"; private MqttClient client; private MqttConnectOptions options; private String userName = "admin"; private String passWord = "admin"; private ScheduledExecutorService scheduler; private void start() { try { // host为主机名,clientid即连接MQTT的客户端ID,一般以唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存 client = new MqttClient(HOST, clientid, new MemoryPersistence()); // MQTT的连接设置 options = new MqttConnectOptions(); // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接 options.setCleanSession(true); // 设置连接的用户名 options.setUserName(userName); // 设置连接的密码 options.setPassword(passWord.toCharArray()); // 设置超时时间 单位为秒 options.setConnectionTimeout(10); // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制 options.setKeepAliveInterval(20); // 设置回调 client.setCallback(new PushCallback()); MqttTopic topic = client.getTopic(TOPIC); //setWill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息 options.setWill(topic, "close".getBytes(), 2, true); client.connect(options); //订阅消息 int[] Qos = {1}; String[] topic1 = {TOPIC}; client.subscribe(topic1, Qos); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws MqttException { PubMsg client = new PubMsg(); client.start(); } }
PushCallBack如下:
package com.joysuccess.testmqtt; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttTopic; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import java.util.concurrent.ScheduledExecutorService; /** * Created by ${zhangxufeng} on 2018/6/28. */ public class PubMsg { public static final String HOST = "tcp://localhost:12333"; public static final String TOPIC = "xxxxxxxxx"; private static final String clientid = "client11"; private MqttClient client; private MqttConnectOptions options; private String userName = "admin"; private String passWord = "admin"; private ScheduledExecutorService scheduler; private void start() { try { // host为主机名,clientid即连接MQTT的客户端ID,一般以唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存 client = new MqttClient(HOST, clientid, new MemoryPersistence()); // MQTT的连接设置 options = new MqttConnectOptions(); // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接 options.setCleanSession(true); // 设置连接的用户名 options.setUserName(userName); // 设置连接的密码 options.setPassword(passWord.toCharArray()); // 设置超时时间 单位为秒 options.setConnectionTimeout(10); // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制 options.setKeepAliveInterval(20); // 设置回调 client.setCallback(new PushCallback()); MqttTopic topic = client.getTopic(TOPIC); //setWill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息 options.setWill(topic, "close".getBytes(), 2, true); client.connect(options); //订阅消息 int[] Qos = {1}; String[] topic1 = {TOPIC}; client.subscribe(topic1, Qos); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws MqttException { PubMsg client = new PubMsg(); client.start(); } }
目前这只是测试Demo,还需集成到spring框架中,有待下一步开发
标签:计费 计算 中断 code package 通过 ram 开发 rtti
原文地址:https://www.cnblogs.com/zxfjava/p/9255614.html