我的问题是,当我想响应到达消息时,我没有从经纪人那里获得递送令牌,但消息到达了经纪人。我可以在日志中看到它。所以我的问题是如何通过发布对收到的消息进行响应?这是我的方式,它不起作用...public void setMqttCallback() { mqttClient.setCallback(new MqttCallback() { @Override public void connectionLost(final Throwable throwable) { System.out.println("Lost connection to Broker because of: " + throwable.getMessage()); } @Override public void messageArrived(final String topic, final MqttMessage mqttMessage) throws Exception { System.out.println("Received on " + topic + ": " + new String(mqttMessage.getPayload())); topics = new Topics(); //Answer to arriving messages (Logic) if (topic.equals(topics.getVehicleNavLandmarkInfo(Mqtt.VIN_ID))) { landmarks = new Landmarks(); MqttMessage message = new MqttMessage(landmarks.getLandmarks().getBytes()); message.setQos(2); System.out.println("Sending on " + topic + ": " + landmarks.getLandmarks()); mqttClient.publish(topics.getBackendNavLandsmarks(Mqtt.VIN_ID), message); } if (topic.equals(topics.getVehicleNavDestination(Mqtt.VIN_ID))) { routing = new Routing(49.0000, 8.0000, "A"); MqttMessage message = new MqttMessage(routing.getShortestPath().getBytes()); message.setQos(2); System.out.println("Sending on " + topic + ": " + routing.getShortestPath()); mqttClient.publish(topics.getBackendNavRoute(Mqtt.VIN_ID), message); } } @Override public void deliveryComplete(final IMqttDeliveryToken iMqttDeliveryToken) { System.out.println("Delivered Message: " + iMqttDeliveryToken.toString()); } }); }
1 回答
茅侃侃
TA贡献1842条经验 获得超21个赞
messageArrived方法正在阻止,因此您不能再尝试使用相同的mqttClient连接来发布消息。您需要使用另一个与代理具有自己连接的线程。
如果是一次性的情况,那么您可以启动一个新线程并执行其中的所有操作,但是如果您的应用程序将不断地将消息发布到到达的消息中,那么最好启动一个线程,连接到代理然后等待信息从messageArrived方法传递。
添加回答
举报
0/150
提交
取消