/** * Create the message consumer that handles the downstream messages and invokes the notification callback * {@link #handleCommandReadinessNotification(TimeUntilDisconnectNotification)} if the message indicates that it * stays connected for a specified time. Supported are telemetry and event MessageConsumer. * * @return Future A succeeded future that contains the MessageConsumer if the creation was successful, a failed * Future otherwise. */ private Future<MessageConsumer> createConsumer() { // create the eventHandler by using the helper functionality for demultiplexing messages to callbacks final Consumer<Message> eventHandler = MessageTap.getConsumer( this::handleEventMessage, this::handleCommandReadinessNotification); // create the telemetryHandler by using the helper functionality for demultiplexing messages to // callbacks final Consumer<Message> telemetryHandler = MessageTap.getConsumer( this::handleTelemetryMessage, this::handleCommandReadinessNotification); return honoClient.createEventConsumer(HonoExampleConstants.TENANT_ID, eventHandler, closeHook -> LOG.error("remotely detached consumer link") ).compose(messageConsumer -> honoClient.createTelemetryConsumer(HonoExampleConstants.TENANT_ID, telemetryHandler, closeHook -> LOG.error("remotely detached consumer link") ).compose(telemetryMessageConsumer -> { LOG.info("Consumer ready for telemetry and event messages."); return Future.succeededFuture(telemetryMessageConsumer); }).recover(t -> Future.failedFuture(t) )); }
private Future<MessageConsumer> createMessageConsumers() { return client .createEventConsumer(tenant, getConsumer(message -> handleMessage("Event", message), this::handleCommandReadinessNotification), closeHook -> LOG.error("remotely detached consumer link")) .compose(consumer -> client.createTelemetryConsumer(tenant, getConsumer(message -> handleMessage("Telemetry", message), this::handleCommandReadinessNotification), closeHook -> LOG.error("remotely detached consumer link"))) .map(consumer -> { LOG.info("Ready to receive command readiness notifications for tenant [{}]", tenant); return consumer; }) .recover(Future::failedFuture); }