Search in sources :

Example 1 with Mqtt3SubAck

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;
}
Also used : Mqtt3ConnAckException(com.hivemq.client.mqtt.mqtt3.exceptions.Mqtt3ConnAckException) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionFailedException(com.hivemq.client.mqtt.exceptions.ConnectionFailedException) Mqtt3SubAck(com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAck) Level(java.util.logging.Level) HashSet(java.util.HashSet) Container(org.openremote.container.Container) MqttClient(com.hivemq.client.mqtt.MqttClient) UniqueIdentifierGenerator(org.openremote.container.util.UniqueIdentifierGenerator) Map(java.util.Map) RECONNECT_DELAY_MAX_MILLIS(org.openremote.agent.protocol.io.AbstractNettyIOClient.RECONNECT_DELAY_MAX_MILLIS) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Mqtt3ConnectBuilder(com.hivemq.client.mqtt.mqtt3.message.connect.Mqtt3ConnectBuilder) SyslogCategory(org.openremote.model.syslog.SyslogCategory) URI(java.net.URI) Mqtt3DisconnectException(com.hivemq.client.mqtt.mqtt3.exceptions.Mqtt3DisconnectException) IOClient(org.openremote.agent.protocol.io.IOClient) RECONNECT_DELAY_INITIAL_MILLIS(org.openremote.agent.protocol.io.AbstractNettyIOClient.RECONNECT_DELAY_INITIAL_MILLIS) Mqtt3ConnAck(com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Logger(java.util.logging.Logger) PROTOCOL(org.openremote.model.syslog.SyslogCategory.PROTOCOL) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Mqtt3AsyncClient(com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient) Mqtt3ClientBuilder(com.hivemq.client.mqtt.mqtt3.Mqtt3ClientBuilder) Mqtt3ClientDisconnectedContext(com.hivemq.client.mqtt.mqtt3.lifecycle.Mqtt3ClientDisconnectedContext) MqttDisconnectSource(com.hivemq.client.mqtt.lifecycle.MqttDisconnectSource) UsernamePassword(org.openremote.model.auth.UsernamePassword) ConnectionClosedException(com.hivemq.client.mqtt.exceptions.ConnectionClosedException) RECONNECT_DELAY_MAX_MILLIS(org.openremote.agent.protocol.io.AbstractNettyIOClient.RECONNECT_DELAY_MAX_MILLIS) RECONNECT_DELAY_INITIAL_MILLIS(org.openremote.agent.protocol.io.AbstractNettyIOClient.RECONNECT_DELAY_INITIAL_MILLIS) Mqtt3SubAck(com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAck) Mqtt3ConnAckException(com.hivemq.client.mqtt.mqtt3.exceptions.Mqtt3ConnAckException) ConnectionFailedException(com.hivemq.client.mqtt.exceptions.ConnectionFailedException) Mqtt3DisconnectException(com.hivemq.client.mqtt.mqtt3.exceptions.Mqtt3DisconnectException) ConnectionClosedException(com.hivemq.client.mqtt.exceptions.ConnectionClosedException)

Aggregations

MqttClient (com.hivemq.client.mqtt.MqttClient)1 ConnectionClosedException (com.hivemq.client.mqtt.exceptions.ConnectionClosedException)1 ConnectionFailedException (com.hivemq.client.mqtt.exceptions.ConnectionFailedException)1 MqttDisconnectSource (com.hivemq.client.mqtt.lifecycle.MqttDisconnectSource)1 Mqtt3AsyncClient (com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient)1 Mqtt3ClientBuilder (com.hivemq.client.mqtt.mqtt3.Mqtt3ClientBuilder)1 Mqtt3ConnAckException (com.hivemq.client.mqtt.mqtt3.exceptions.Mqtt3ConnAckException)1 Mqtt3DisconnectException (com.hivemq.client.mqtt.mqtt3.exceptions.Mqtt3DisconnectException)1 Mqtt3ClientDisconnectedContext (com.hivemq.client.mqtt.mqtt3.lifecycle.Mqtt3ClientDisconnectedContext)1 Mqtt3ConnectBuilder (com.hivemq.client.mqtt.mqtt3.message.connect.Mqtt3ConnectBuilder)1 Mqtt3ConnAck (com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck)1 Mqtt3SubAck (com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAck)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1