如何接收特定主题发布的 MQTT 消息?我可以在 MQTT 代理上发布消息,但我不知道如何接收消息?
这是我的组件 xml:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="arduinoCommunicator">
<implementation class="arduinoCommunicator.ArduinoCommunicator"/>
<reference bind="setDataService" cardinality="1..1" interface="org.eclipse.kura.data.DataService" name="DataService" policy="static" unbind="unsetDataService"/>
<service>
<provide interface="org.eclipse.kura.data.DataServiceListener"/>
</service>
</scr:component>
这是我的捆绑激活器类,我只保留了所需的代码,我实现了 DataServiceListener,我订阅了 onConnectionEstablished() 上的主题,我想我在 onMessageArrived 函数上收到了消息,问题是我没有看到已订阅且未执行 onMessageArrived() 函数的日志:
public class ArduinoCommunicator implements DataServiceListener {
public static DataService dataService;
@Override
public void onConnectionEstablished() {
String topic="egmkey/device2/cmd/PING";
System.out.println("connection has been established");
try {
ArduinoCommunicator.dataService.subscribe(topic, 1);//egmkey/device2/cmd/PING
System.out.println("subscription done to topic"+topic);
} catch (KuraException e) {
System.out.println("failed to subscribe: "+ e);
}
}
@Override
public void onMessageArrived(String topic, byte[] payload, int qos, boolean retained) {
System.out.println("message received!!!!!!!!!!!!!!!!!!!!!!:"+topic);
}
}
在 KURA 上使用 DataService 接收 MQTT 消息时,我错过了什么?谢谢。