Search in sources :

Example 1 with MessageConsumer

use of org.eclipse.hono.client.MessageConsumer in project hono by eclipse.

the class HonoConsumerBase method consumeData.

/**
 * Initiate the connection and set the message handling method to treat data that is received.
 *
 * @throws Exception Thrown if the latch is interrupted during waiting or if the read from System.in throws an IOException.
 */
protected void consumeData() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final Future<MessageConsumer> consumerFuture = Future.future();
    consumerFuture.setHandler(result -> {
        if (!result.succeeded()) {
            System.err.println("honoClient could not create telemetry consumer for " + HonoExampleConstants.HONO_AMQP_CONSUMER_HOST + ":" + HonoExampleConstants.HONO_AMQP_CONSUMER_PORT + " : " + result.cause());
        }
        latch.countDown();
    });
    honoClient.connect(new ProtonClientOptions()).compose(connectedClient -> {
        if (eventMode) {
            return connectedClient.createEventConsumer(HonoExampleConstants.TENANT_ID, this::handleMessage, closeHook -> System.err.println("remotely detached consumer link"));
        } else {
            return connectedClient.createTelemetryConsumer(HonoExampleConstants.TENANT_ID, this::handleMessage, closeHook -> System.err.println("remotely detached consumer link"));
        }
    }).setHandler(consumerFuture.completer());
    latch.await();
    if (consumerFuture.succeeded()) {
        System.in.read();
    }
    vertx.close();
}
Also used : Data(org.apache.qpid.proton.amqp.messaging.Data) CountDownLatch(java.util.concurrent.CountDownLatch) Section(org.apache.qpid.proton.amqp.messaging.Section) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Vertx(io.vertx.core.Vertx) Message(org.apache.qpid.proton.message.Message) MessageConsumer(org.eclipse.hono.client.MessageConsumer) HonoClientImpl(org.eclipse.hono.client.impl.HonoClientImpl) MessageHelper(org.eclipse.hono.util.MessageHelper) HonoClient(org.eclipse.hono.client.HonoClient) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Future(io.vertx.core.Future) MessageConsumer(org.eclipse.hono.client.MessageConsumer) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Future (io.vertx.core.Future)1 Vertx (io.vertx.core.Vertx)1 ProtonClientOptions (io.vertx.proton.ProtonClientOptions)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Data (org.apache.qpid.proton.amqp.messaging.Data)1 Section (org.apache.qpid.proton.amqp.messaging.Section)1 Message (org.apache.qpid.proton.message.Message)1 HonoClient (org.eclipse.hono.client.HonoClient)1 MessageConsumer (org.eclipse.hono.client.MessageConsumer)1 HonoClientImpl (org.eclipse.hono.client.impl.HonoClientImpl)1 ClientConfigProperties (org.eclipse.hono.config.ClientConfigProperties)1 MessageHelper (org.eclipse.hono.util.MessageHelper)1