MQTT订阅主题就会收到消息的问题--Retained

MQTT订阅主题就会收到消息的问题–Retained

之前的项目使用了mqtt消息队列作为第三方消息的接收和发送,本地作为mqtt的订阅者也作为发送者。
本地本地接受到mqtt订阅的消息,作处理后再重新发送到云服务端。这里遇到一个坑(其实是自己造成的),在订阅主题后就会收到mqtt服务器发送的消息,查找资料后发现是在publish某条数据时,设置了Retained(默认是ture,这里选择后为false)。
在这里插入图片描述
详细资料可以查看https://blog.csdn.net/libaineu2004/article/details/78667726
大意是设置了Retained,那每次有客户端订阅该主题,就会发送你之前设置了Retained的publish的数据。
解决的办法就是再publish一条无数据的带Retained(false)的消息即可。
在这里插入图片描述
之后再publish就不要带Retained![在这里插入图片描述](https://img-blog.csdnimg.cn/20190530100013360.png
或者在代码里设置MqttMessage的Retained为false。

发布了5 篇原创文章 · 获赞 1 · 访问量 6399
展开阅读全文

关于mqtt断线重连后消息停留在broker上,没有推送到客户端

03-21

最近在学习mqtt,拿到例程来实验发现离线后推送的信息不能在重连后接收到,这是为什么?我用wireshark照着手册看了一遍没毛病啊,而且断线重连后订阅的消息有些能收到,有些会停留在代理上,求解!!! ``` public class MQTTPublish implements MqttCallback { //public static final String HOST = "tcp://10.0.0.250:1884"; public static final String HOST = "tcp://192.168.67.130:61613"; public static final String TOPIC = "MQTTtest"; private static final String clientid ="publisher"; private static final String str = "ad钙奶"; private static MqttClient client; private MqttTopic topic; private String userName = "admin"; private String passWord = "password"; private MqttMessage message; public MQTTPublish() throws MqttException { 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(this); client.connect(options); topic = client.getTopic(TOPIC); } catch (Exception e) { e.printStackTrace(); } } public void publish(MqttMessage message) throws MqttPersistenceException, MqttException{ MqttDeliveryToken token = topic.publish(message); token.waitForCompletion(); System.out.println("Token is complete:" + token.isComplete()); } public static void main(String[] args) throws MqttException { MQTTPublish mqttpub = new MQTTPublish(); mqttpub.message = new MqttMessage(); mqttpub.message.setQos(2); mqttpub.message.setRetained(true); mqttpub.message.setPayload(str.getBytes()); mqttpub.publish(mqttpub.message); System.out.println("Ratained state:" + mqttpub.message.isRetained()); client.disconnect(); System.out.println("Disconnected"); System.exit(0); } @Override public void connectionLost(Throwable arg0) { // TODO Auto-generated method stub } public void deliveryComplete(IMqttDeliveryToken token) { try { System.out.println("deliveryComplete---------"+ token.isComplete()); } catch (Exception e) { e.printStackTrace(); } } @Override public void messageArrived(String arg0, MqttMessage arg1) throws Exception { // TODO Auto-generated method stub } } ``` ``` public class MQTTSubscribe implements MqttCallback { //public static final String HOST = "tcp://10.0.0.250:1884"; public static final String HOST = "tcp://192.168.67.130:61613"; public static final String TOPIC = "MQTTtest"; private static final String clientid = "subscriber"; private MqttClient client; private MqttConnectOptions options; private String userName = "admin"; private String passWord = "password"; private ScheduledExecutorService scheduler; public void startReconnect() { scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(new Runnable() { public void run() { if (!client.isConnected()) { try { client.connect(options); } catch (MqttSecurityException e) { e.printStackTrace(); } catch (MqttException e) { e.printStackTrace(); } } } }, 0 * 1000, 10 * 1000, TimeUnit.MILLISECONDS); } private void start() { try { client = new MqttClient(HOST, clientid, new MemoryPersistence()); options = new MqttConnectOptions(); options.setCleanSession(false); options.setUserName(userName); options.setPassword(passWord.toCharArray()); options.setConnectionTimeout(10); options.setKeepAliveInterval(20); client.setCallback(this); // MqttTopic topic = client.getTopic(TOPIC); // options.setWill(topic, "close".getBytes(), 2, true); client.connect(options); int[] Qos = {2}; String[] topic1 = {TOPIC}; client.subscribe(topic1, Qos); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws MqttException { MQTTSubscribe client = new MQTTSubscribe(); client.start(); } public void connectionLost(Throwable cause) { System.out.println("Connection lost, reconnect please!"); } @Override public void deliveryComplete(IMqttDeliveryToken token) { try { System.out.println("deliveryComplete---------"+ token.isComplete()); } catch (Exception e) { e.printStackTrace(); } } @Override public void messageArrived(String topic, MqttMessage message) throws Exception { System.out.println("Message arrived on topic:"+topic); System.out.println("Message arrived on QoS:"+message.getQos()); System.out.println("Message arrived on content:"+new String(message.getPayload())); } } ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览