/** * Construct a message consumer that detects an indication that a device is ready to receive an upstream * message and delegates to the passed callback handlers. * * @param messageConsumer Message consumer that handles generic messages. * @param notificationReadyToDeliverConsumer Consumer<TimeUntilDisconnectNotification> that is called when the message indicates the current readiness * to receive an upstream message. * @return The message consumer that invokes the specific handlers depending on the event type. * @throws NullPointerException if any of the parameters are {@code null}. */ public static Consumer<Message> getConsumer(final Consumer<Message> messageConsumer, final Consumer<TimeUntilDisconnectNotification> notificationReadyToDeliverConsumer) { Objects.requireNonNull(messageConsumer); Objects.requireNonNull(notificationReadyToDeliverConsumer); return new MessageTap( msg -> { if (msg.getBody() == null || MessageHelper.hasDataBody(msg)) { TimeUntilDisconnectNotification.fromMessage(msg).ifPresent( notificationObject -> notificationReadyToDeliverConsumer.accept(notificationObject)); messageConsumer.accept(msg); } }); }
/** * 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); }
/** * Construct a message consumer that detects an indication that a device is ready to receive an upstream * message and delegates to the passed callback handlers. * * @param messageConsumer Message consumer that handles generic messages. * @param notificationReadyToDeliverConsumer Consumer<TimeUntilDisconnectNotification> that is called when the message indicates the current readiness * to receive an upstream message. * @return The message consumer that invokes the specific handlers depending on the event type. * @throws NullPointerException if any of the parameters are {@code null}. */ public static Consumer<Message> getConsumer(final Consumer<Message> messageConsumer, final Consumer<TimeUntilDisconnectNotification> notificationReadyToDeliverConsumer) { Objects.requireNonNull(messageConsumer); Objects.requireNonNull(notificationReadyToDeliverConsumer); return new MessageTap( msg -> { if (msg.getBody() == null || MessageHelper.hasDataBody(msg, false)) { TimeUntilDisconnectNotification.fromMessage(msg).ifPresent( notificationObject -> notificationReadyToDeliverConsumer.accept(notificationObject)); messageConsumer.accept(msg); } }); }