共享订阅Paho Java客户端库未调用回调
需要帮助
https://yogin16.github.io/2017/07/15/mqtt-shared-sub-with-paho/
https://github.com/eclipse/paho.mqtt.java/issues/367
公共类MqttTopicListener实现MqttCallback,IMqttMessageListener {
private static final Logger logger = LogManager.getLogger(MqttTopicListener.class);
private static final ExecutorService executor;
public MqttTopicListener(ApplicationContext applicationContext, String profile){
}
@Override
public void connectionLost(Throwable cause) {
logger.warn("MQTT: connectionLost :"+cause,cause);
try {
MqttListenerConnectionFactory.INSTANCE.connect();
Map<String, Integer> mqttQosTopicMap = CommunicationFactory.INSTANCE.getMqttQosTopicMap();
mqttQosTopicMap.forEach((topic, qos) -> {
try {
MqttListenerConnectionFactory.INSTANCE.subscribe(topic,qos);
} catch (MqttException e) {
logger.error("connectionLost reconnect error :"+e,e);
}
});
} catch (MqttException e) {
logger.error(e,e);
}
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
logger.error("[MQTT] message"+new String(message.getPayload()));
if(message.isDuplicate()) {
logger.warn("[MQTT] Duplicate Message Payload :"+topic);
return;
}
try {
AbstractTopicHandler abstractTopicHandler = (AbstractTopicHandler)
CommunicationFactory.INSTANCE.getInstanceOfHandlerByTopic(topic, message);
executor.submit(abstractTopicHandler);
}catch (Exception iu){
logger.error("Not able to launch Handler class"+iu,iu);
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
try {
logger.info("MQTT: deliveryComplete for token :"+token.getMessage()+" "+token.getMessageId());
} catch (MqttException e) {
logger.error(e,e);
}
}
}
共享订阅是一种将消息分发给共享订阅主题的一组订阅者的机制,以便每个消息仅由一个订阅者接收。
通过MQTT命令行可以正常工作 mosquitto_sub -h ***** -p 1883 -q 1 -t“ $ share / v1 / server / devices /#” -i“ terminal1”
这是一条测试消息7