use of org.fusesource.mqtt.client.Listener 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);
}
});
}
});
}
Aggregations