use of org.eclipse.hono.application.client.ApplicationClient in project hono by eclipse.
the class HonoExampleApplicationBase method consumeData.
/**
* Start the application client and set the message handling method to treat data that is received.
*/
protected void consumeData() {
final CompletableFuture<ApplicationClient<? extends MessageContext>> startup = new CompletableFuture<>();
if (client instanceof AmqpApplicationClient) {
final AmqpApplicationClient ac = (AmqpApplicationClient) client;
ac.addDisconnectListener(c -> LOG.info("lost connection to Hono, trying to reconnect ..."));
ac.addReconnectListener(c -> LOG.info("reconnected to Hono"));
}
client.start().compose(v -> CompositeFuture.all(createEventConsumer(), createTelemetryConsumer())).onSuccess(ok -> startup.complete(client)).onFailure(startup::completeExceptionally);
try {
startup.join();
LOG.info("Consumer ready for telemetry and event messages");
System.in.read();
} catch (final CompletionException e) {
LOG.error("{} consumer failed to start [{}:{}]", USE_KAFKA ? "Kafka" : "AMQP", HonoExampleConstants.HONO_MESSAGING_HOST, port, e.getCause());
} catch (final IOException e) {
// nothing we can do
}
final CompletableFuture<ApplicationClient<? extends MessageContext>> shutDown = new CompletableFuture<>();
@SuppressWarnings("rawtypes") final List<Future> closeFutures = new ArrayList<>();
Optional.ofNullable(eventConsumer).map(MessageConsumer::close).ifPresent(closeFutures::add);
Optional.ofNullable(telemetryConsumer).map(MessageConsumer::close).ifPresent(closeFutures::add);
Optional.ofNullable(client).map(Lifecycle::stop).ifPresent(closeFutures::add);
CompositeFuture.join(closeFutures).compose(ok -> vertx.close()).recover(t -> vertx.close()).onComplete(ar -> shutDown.complete(client));
// wait for clients to be closed
shutDown.join();
LOG.info("Consumer has been shut down");
}
Aggregations