use of com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAck in project openremote by openremote.
the class AbstractMQTT_IOClient method doClientSubscription.
protected synchronized boolean doClientSubscription(String topic, Set<Consumer<MQTTMessage<S>>> consumers) {
Consumer<MQTTMessage<S>> messageConsumer = message -> {
if (!topicConsumerMap.containsKey(topic)) {
return;
}
consumers.forEach(consumer -> {
try {
consumer.accept(message);
} catch (Exception e) {
LOG.log(Level.WARNING, "Message consumer threw an exception", e);
}
});
};
if (this.connectionStatus != ConnectionStatus.CONNECTED) {
// Just return true and let connection logic sort out actual subscription
return true;
}
try {
Mqtt3SubAck subAck = client.subscribeWith().topicFilter(topic).callback(publish -> {
try {
String topicStr = publish.getTopic().toString();
S payload = messageFromBytes(publish.getPayloadAsBytes());
MQTTMessage<S> message = new MQTTMessage<>(topicStr, payload);
messageConsumer.accept(message);
} catch (Exception e) {
LOG.log(Level.WARNING, "Failed to process published message on client '" + getClientUri() + "'", e);
}
}).send().get();
LOG.fine("Subscribed to topic '" + topic + "' on client '" + getClientUri() + "'");
return true;
} catch (Exception e) {
LOG.log(Level.WARNING, "Failed to subscribe to topic '" + topic + "' on client '" + getClientUri() + "'", e);
executorService.execute(() -> onSubscribeFailed(topic));
}
return false;
}
Aggregations