Search in sources :

Example 1 with Lifecycle

use of org.eclipse.hono.util.Lifecycle 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");
}
Also used : ApplicationClient(org.eclipse.hono.application.client.ApplicationClient) MessagingKafkaConsumerConfigProperties(org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) MessagingKafkaProducerConfigProperties(org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties) Lifecycle(org.eclipse.hono.util.Lifecycle) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) TimeUntilDisconnectNotification(org.eclipse.hono.util.TimeUntilDisconnectNotification) ArrayList(java.util.ArrayList) CompositeFuture(io.vertx.core.CompositeFuture) ProtonBasedApplicationClient(org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) HonoConnection(org.eclipse.hono.client.HonoConnection) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Logger(org.slf4j.Logger) MessageContext(org.eclipse.hono.application.client.MessageContext) KafkaApplicationClientImpl(org.eclipse.hono.application.client.kafka.impl.KafkaApplicationClientImpl) CachingKafkaProducerFactory(org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Future(io.vertx.core.Future) List(java.util.List) KafkaProducerFactory(org.eclipse.hono.client.kafka.producer.KafkaProducerFactory) Buffer(io.vertx.core.buffer.Buffer) AmqpApplicationClient(org.eclipse.hono.application.client.amqp.AmqpApplicationClient) Optional(java.util.Optional) MessageConsumer(org.eclipse.hono.application.client.MessageConsumer) CommonKafkaClientConfigProperties(org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties) Handler(io.vertx.core.Handler) ApplicationClient(org.eclipse.hono.application.client.ApplicationClient) ProtonBasedApplicationClient(org.eclipse.hono.application.client.amqp.ProtonBasedApplicationClient) AmqpApplicationClient(org.eclipse.hono.application.client.amqp.AmqpApplicationClient) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AmqpApplicationClient(org.eclipse.hono.application.client.amqp.AmqpApplicationClient) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) CompletableFuture(java.util.concurrent.CompletableFuture) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future) MessageContext(org.eclipse.hono.application.client.MessageContext)

Aggregations

CompositeFuture (io.vertx.core.CompositeFuture)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 JsonObject (io.vertx.core.json.JsonObject)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Random (java.util.Random)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 ApplicationClient (org.eclipse.hono.application.client.ApplicationClient)1 DownstreamMessage (org.eclipse.hono.application.client.DownstreamMessage)1 MessageConsumer (org.eclipse.hono.application.client.MessageConsumer)1 MessageContext (org.eclipse.hono.application.client.MessageContext)1 AmqpApplicationClient (org.eclipse.hono.application.client.amqp.AmqpApplicationClient)1