我有一个通过MQTT订阅了几个主题的监听器类(Java)。我想从另一个类访问发布到我的监听器的消息。这该怎么做? 目标是将监听器类的输出提供给持续刷新的JFrame。
答案 0 :(得分:2)
你有一个客户
private MqttClient sampleClient;
您需要订阅一些主题:
sampleClient.subscribe(topicToSubscribe);
和SET A CALLBACK,这样您就可以获得经纪人推送给您的信息(这肯定是您缺少的)
sampleClient.setCallback(new MqttCallback() {
@Override
public void messageArrived(String topicInforming, MqttMessage mqttMessage) throws Exception {
System.out.println("messageArrived: " + mqttMessage.toString());
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
System.out.println("DeliveryComplete: " + arg0.getMessageId());
}
@Override
public void connectionLost(Throwable arg0) {
System.err.println("Connection lost: " + arg0.getMessage());
}
});