Search in sources :

Example 1 with Callback

use of org.fusesource.mqtt.client.Callback in project camel by apache.

the class MQTTEndpoint method createConnection.

protected void createConnection() {
    connection = configuration.callbackConnection();
    connection.listener(new Listener() {

        public void onConnected() {
            connected = true;
            LOG.info("MQTT Connection connected to {}", configuration.getHost());
        }

        public void onDisconnected() {
            // no connected = false required here because the MQTT client should trigger its own reconnect;
            // setting connected = false would make the publish() method to launch a new connection while the original
            // one is still reconnecting, likely leading to duplicate messages as observed in CAMEL-9092;
            // if retries are exhausted and it desists, we should get a callback on onFailure, and then we can set
            // connected = false safely
            LOG.debug("MQTT Connection disconnected from {}", configuration.getHost());
        }

        public void onPublish(UTF8Buffer topic, Buffer body, Runnable ack) {
            if (!consumers.isEmpty()) {
                Exchange exchange = createExchange();
                exchange.getIn().setBody(body.toByteArray());
                exchange.getIn().setHeader(MQTTConfiguration.MQTT_SUBSCRIBE_TOPIC, topic.toString());
                for (MQTTConsumer consumer : consumers) {
                    consumer.processExchange(exchange);
                }
            }
            if (ack != null) {
                ack.run();
            }
        }

        public void onFailure(Throwable value) {
            // mark this connection as disconnected so we force re-connect
            connected = false;
            LOG.warn("Connection to " + configuration.getHost() + " failure due " + value.getMessage() + ". Forcing a disconnect to re-connect on next attempt.");
            connection.disconnect(new Callback<Void>() {

                public void onSuccess(Void value) {
                }

                public void onFailure(Throwable e) {
                    LOG.debug("Failed to disconnect from " + configuration.getHost() + ". This exception is ignored.", e);
                }
            });
        }
    });
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) Exchange(org.apache.camel.Exchange) Listener(org.fusesource.mqtt.client.Listener) Callback(org.fusesource.mqtt.client.Callback) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer)

Example 2 with Callback

use of org.fusesource.mqtt.client.Callback in project camel by apache.

the class MQTTEndpoint method doStop.

@Override
protected void doStop() throws Exception {
    super.doStop();
    if (connection != null && connected) {
        final Promise<Void> promise = new Promise<>();
        connection.getDispatchQueue().execute(new Task() {

            @Override
            public void run() {
                connection.disconnect(new Callback<Void>() {

                    public void onSuccess(Void value) {
                        connected = false;
                        promise.onSuccess(value);
                    }

                    public void onFailure(Throwable value) {
                        promise.onFailure(value);
                    }
                });
            }
        });
        promise.await(configuration.getDisconnectWaitInSeconds(), TimeUnit.SECONDS);
    }
}
Also used : Promise(org.fusesource.mqtt.client.Promise) Task(org.fusesource.hawtdispatch.Task) Callback(org.fusesource.mqtt.client.Callback)

Aggregations

Callback (org.fusesource.mqtt.client.Callback)2 Exchange (org.apache.camel.Exchange)1 Buffer (org.fusesource.hawtbuf.Buffer)1 UTF8Buffer (org.fusesource.hawtbuf.UTF8Buffer)1 Task (org.fusesource.hawtdispatch.Task)1 Listener (org.fusesource.mqtt.client.Listener)1 Promise (org.fusesource.mqtt.client.Promise)1