Following is the implementation of addconsumer. Problem that i am facing is the line with <<<-----. I treplaces all '+' with regex format which i think MQTT broker like mosquitto do not understand. I have also attached an output from mosquitto when a topic has '+' character in it.
public boolean addConsumer(MqttMessageSubscriber subscriber) throws MqttException {
String topic = subscriber.getTopic();
// Prepare topic for regex pattern matching taking place in messageArrived.
topic = StringUtils.replace(StringUtils.replace(topic, "+", "[^/]"), "#", "."); <<<------
synchronized (consumers) {
List subscriberList = consumers.get(topic);
if (subscriberList == null) {
subscriberList = new ArrayList<>();
}
subscriberList.add(subscriber);
consumers.put(topic, subscriberList);
}
if (isConnected()) {
try {
client.subscribe(topic, qos);
} catch (org.eclipse.paho.client.mqttv3.MqttException e) {
throw new MqttException(e);
}
}
return true;
}
MOSQUITTO.log
1506352001: Received SUBSCRIBE from openhab
1506352001: cctvchannel/motion_alert/2/2/[^/]/reply (QoS 0)
1506352001: openhab 0 cctvchannel/motion_alert/2/2/[^/]/reply
1506352001: Sending SUBACK to openhab
1506352001: Received SUBSCRIBE from openhab
1506352001: cctvchannel/motion_alert/2/3/[^/]/reply (QoS 0)
1506352001: openhab 0 cctvchannel/motion_alert/2/3/[^/]/reply
1506352001: Sending SUBACK to openhab
1506352006: Received SUBSCRIBE from openhab