Java 如何从 Mosquitto 清除所有保留的 mqtt 消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36729300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear ALL retained mqtt messages from Mosquitto?
提问by JohnL
I've seen the mosquitto_pub -h [server] -r -n -t [XYZ]
syntax for clearing out one off messages. My problem is the device developers have posted a lot of garbage messages.
我已经看到了mosquitto_pub -h [server] -r -n -t [XYZ]
清除一次性消息的语法。我的问题是设备开发人员发布了很多垃圾信息。
I have a Java/Paho code base that I'd like to modify to do this automatically as needed, but I can't seem to publish a zero byte message. I tried
我有一个 Java/Paho 代码库,我想根据需要修改它以自动执行此操作,但我似乎无法发布零字节消息。我试过
client.publish(topic,null);
...but that didn't seem to work.
……但这似乎不起作用。
Any suggestions on how to delete everything, en mass?
关于如何批量删除所有内容的任何建议?
采纳答案by hardillb
There are 2 options for this using the paho client code depending on which of the 2 publish
methods you use.
根据publish
您使用的两种方法中的哪一种,使用 paho 客户端代码有两种选择。
MqttMessage msg = new MqttMessage(new byte[0]);
msg.setRetained(true);
client.publish(topic, msg);
or
或者
client.publish(topic, new byte[0],0,true);
The other option would be to stop mosquitto and delete the persistence file and restart
另一种选择是停止 mosquitto 并删除持久性文件并重新启动
回答by kartben
This should work:
这应该有效:
client.publish(topic, new byte[]{}, 0, true);
Also, you may be interested in this scriptfrom Eclipse Paho Python, to clear a given topic hierarchy. You may want to implement a similar behavior in Java, but it looks like you may be looking for a one-off solution, so maybe just use the Python script :)
此外,您可能对Eclipse Paho Python中的此脚本感兴趣,以清除给定的主题层次结构。您可能想在 Java 中实现类似的行为,但看起来您可能正在寻找一次性解决方案,所以也许只需使用 Python 脚本 :)
回答by Gussoh
Here is how to do it properly with a shell script.
以下是如何使用 shell 脚本正确执行此操作。
#!/bin/sh
echo "cleaning " " :: usage: cleanmqtt <host>"
mosquitto_sub -h -t "#" -v | while read line; do mosquitto_pub -h -t "${line% *}" -r -n; done
Just put it in a file called somthing like
把它放在一个叫做类似的文件中
finally_a_working_way_to_remove_all_those_annoying_messages.sh
finally_a_working_way_to_remove_all_those_annoying_messages.sh
Then run
然后运行
sh finally_a_working_way_to_remove_all_those_annoying_messages.sh localhost
You'll have to to ctrl+c after a while when it has received all the messages. This solution is quite crude. You cant specify what to delete or anything.
当它收到所有消息后,您必须在一段时间后按 ctrl+c。这个解决方案非常粗糙。您无法指定要删除的内容或任何内容。
回答by Manav Akela
If you are using the Mosquitto MQTT Broker, disable "Retained messages" using the official method, provided by Mosquitto
如果您使用的是 Mosquitto MQTT Broker,请使用 Mosquitto 提供的官方方法禁用“保留消息”
First find mosquitto.conf file
首先找到mosquitto.conf文件
(In my Ubuntu/EC2 instance it is stored in /etc/mosquitto directory, I assume your mosquitto.conf file path is /etc/mosquitto/mosquitto.conf)
(在我的 Ubuntu/EC2 实例中,它存储在 /etc/mosquitto 目录中,我假设您的 mosquitto.conf 文件路径是 /etc/mosquitto/mosquitto.conf)
Edit with your favorite text editor, mine is nano.
使用您最喜欢的文本编辑器进行编辑,我的是 nano。
sudo nano /etc/mosquitto/mosquitto.conf
and in that file replace "persistence false" in place of "persistence true"
并在该文件中将“persistence false”替换为“persistence true”
persistence false
Now save file (if using nano press ctrl+o and then enter to save, ctrl+x to exit)
现在保存文件(如果使用nano按ctrl+o然后回车保存,ctrl+x退出)
now restart mosquitto using below commands
现在使用以下命令重新启动 mosquitto
sudo service mosquitto stop
sudo service mosquitto start
Note:If this config path does not exist in your case, find config file using this command-
注意:如果此配置路径在您的情况下不存在,请使用此命令查找配置文件-
sudo find / -name mosquitto.conf
回答by user3084651
Since I don't have enough points to comment, running
由于我没有足够的积分来评论,运行
#!/bin/sh
echo "cleaning " " :: usage: cleanmqtt <host>"
mosquitto_sub -h -t "#" -v | while read line; do mosquitto_pub -h -t "${line% *}" -r -n; done
could cause an infinite loop due to pub/sub delays.
Adding --retained-only
to mosquitto_sub
seems to remove the infinite loop.
由于发布/订阅延迟,可能会导致无限循环。添加--retained-only
到mosquitto_sub
似乎消除了无限循环。
回答by DrSlow
for powershell users
对于 PowerShell 用户
had this issue on windows, so here with powershell. Mosquitto needs to be installed on the command host.
在 Windows 上有这个问题,所以在这里使用 powershell。Mosquitto 需要安装在命令主机上。
get retained messages
获取保留消息
I did not want to clear all retained messages. Example, only the ones containing "octo" in it's topic. Let's see what's there:
我不想清除所有保留的消息。例如,只有主题中包含“octo”的那些。让我们看看那里有什么:
mosquitto_sub.exe -h <mqtt host> -v -u <mqtt user> -P <mqtt password> -t '#' --retained-only|
Select-String octo
(replace mqtt host, user, password as needed)
(根据需要替换 mqtt 主机、用户、密码)
delete retained messages
删除保留的消息
use the same search string here ( in this example "octo"):
在此处使用相同的搜索字符串(在此示例中为“octo”):
mosquitto_sub.exe -h <mqtt host> -v -u <mqtt user> -P <mqtt password> -t '#' --retained-only|
Select-String octo |
Out-String -Stream|ForEach-Object -Process {$_.Split(" ")[0]}|
%{mosquitto_pub.exe -h <mqtt host> -u <mqtt user> -P <mqtt password> -t "$_" -r -n}
Again, replace all occurences of mqtt host, user, password.
再次,替换所有出现的 mqtt 主机、用户、密码。
Simply check again with step 1 if anything is left :)
如果有任何剩余,只需使用步骤 1 再次检查:)