Example usage for io.vertx.core CompositeFuture all

List of usage examples for io.vertx.core CompositeFuture all

Introduction

In this page you can find the example usage for io.vertx.core CompositeFuture all.

Prototype

static <T1, T2> CompositeFuture all(Future<T1> f1, Future<T2> f2) 

Source Link

Document

Return a composite future, succeeded when all futures are succeeded, failed when any future is failed.

Usage

From source file:org.eclipse.hono.adapter.mqtt.VertxBasedMqttProtocolAdapter.java

License:Open Source License

private void publishMessage(final MqttEndpoint endpoint, final String tenantId, final String logicalDeviceId,
        final MqttPublishMessage message, final ResourceIdentifier resource) {
    LOG.trace("received message [client ID: {}, topic: {}, QoS: {}, payload {}]", endpoint.clientIdentifier(),
            message.topicName(), message.qosLevel(), message.payload().toString(Charset.defaultCharset()));

    try {/*from   w  w  w.j a v a 2s . c  o  m*/
        Future<Void> messageTracker = Future.future();
        messageTracker.setHandler(s -> {
            if (s.failed()) {
                LOG.debug("cannot process message [client ID: {}, deviceId: {}, topic: {}, QoS: {}]: {}",
                        endpoint.clientIdentifier(), logicalDeviceId, resource, message.qosLevel(),
                        s.cause().getMessage());
                metrics.incrementUndeliverableMqttMessages(resource.getEndpoint(), tenantId);
                close(endpoint);
            } else {
                LOG.trace("successfully processed message [client ID: {}, deviceId: {}, topic: {}, QoS: {}]",
                        endpoint.clientIdentifier(), logicalDeviceId, resource, message.qosLevel());
                metrics.incrementProcessedMqttMessages(resource.getEndpoint(), tenantId);
            }
        });

        // check that MQTT client tries to publish on topic with device_id same as on connection
        if (!getConfig().isAuthenticationRequired()
                && !resource.getResourceId().equals(endpoint.clientIdentifier())) {
            // MQTT client is trying to publish on a different device_id used on connection (MQTT has no way for
            // errors)
            messageTracker.fail("client not authorized");
        } else {
            Future<String> assertionTracker = getRegistrationAssertion(endpoint, tenantId, logicalDeviceId);
            Future<MessageSender> senderTracker = getSenderTracker(message, resource, tenantId);

            CompositeFuture.all(assertionTracker, senderTracker).compose(ok -> {
                doUploadMessage(logicalDeviceId, assertionTracker.result(), endpoint, message,
                        senderTracker.result(), messageTracker);
            }, messageTracker);
        }

    } catch (IllegalArgumentException e) {

        // MQTT client is trying to publish on invalid topic; it does not contain at least two segments
        LOG.debug("client [ID: {}] tries to publish on unsupported topic", endpoint.clientIdentifier());
        close(endpoint);
    }
}

From source file:org.eclipse.hono.service.http.HttpServiceBase.java

License:Open Source License

@Override
protected final Future<Void> startInternal() {

    return preStartServers().compose(s -> checkPortConfiguration()).compose(s -> startEndpoints())
            .compose(router -> {/*from  w w w.java2 s  . c o m*/
                return CompositeFuture.all(bindSecureHttpServer(router), bindInsecureHttpServer(router));
            }).compose(s -> onStartupSuccess());
}

From source file:org.eclipse.hono.service.monitoring.AbstractMessageSenderConnectionEventProducer.java

License:Open Source License

private Future<?> sendNotificationEvent(final Device authenticatedDevice, final String protocolAdapter,
        final String remoteId, final String cause, final JsonObject data) {

    if (authenticatedDevice == null) {
        // we only handle authenticated devices
        return Future.succeededFuture();
    }// w  ww.j  a  va2s.c o  m

    // get an assertion

    final Future<String> assertionFuture = this.deviceRegistryClient
            .getOrCreateRegistrationClient(authenticatedDevice.getTenantId()).compose(registrationClient -> {
                return registrationClient.assertRegistration(authenticatedDevice.getDeviceId())
                        .map(registration -> {
                            return registration.getString(RegistrationConstants.FIELD_ASSERTION);
                        });
            });

    // get a sender

    final Future<MessageSender> senderFuture = getOrCreateSender(authenticatedDevice);

    // send message with assertion and sender

    return CompositeFuture.all(assertionFuture, senderFuture).compose(f -> {
        final String deviceId = authenticatedDevice.getDeviceId();

        final JsonObject payload = new JsonObject();
        payload.put("cause", cause);
        payload.put("remote-id", remoteId);
        payload.put("source", protocolAdapter);

        if (data != null) {
            payload.put("data", data);
        }

        return senderFuture.result().send(deviceId, payload.encode().getBytes(StandardCharsets.UTF_8),
                EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE, assertionFuture.result(), v -> {
                });
    });
}

From source file:org.eclipse.hono.vertx.example.base.HonoSenderBase.java

License:Open Source License

/**
 * Get both Hono clients and connect them to Hono's microservices.
 *
 * @return The result of the creation and connection of the Hono clients.
 *///from   w w  w  .  j av  a 2s.  c o  m
private Future<Void> getHonoClients() {
    // we need two clients to get it working, define futures for them
    final Future<RegistrationClient> registrationClientTracker = getRegistrationClient();
    final Future<MessageSender> messageSenderTracker = getMessageSender();

    final Future<Void> result = Future.future();

    CompositeFuture.all(registrationClientTracker, messageSenderTracker).setHandler(compositeResult -> {
        if (compositeResult.failed()) {
            System.err.println("hono clients could not be created : " + compositeResult.cause().getMessage());
            result.fail(compositeResult.cause());
        } else {
            registrationClient = registrationClientTracker.result();
            messageSender = messageSenderTracker.result();
            result.complete();
        }
    });

    return result;
}

From source file:org.eclipse.hono.vertx.example.base.HonoSenderBase.java

License:Open Source License

private Future<Void> closeClients() {
    final Future<Void> messagingClient = Future.future();
    final Future<Void> regClient = Future.future();
    honoMessagingClient.shutdown(messagingClient.completer());
    honoRegistryClient.shutdown(regClient.completer());
    return CompositeFuture.all(messagingClient, regClient).compose(ok -> Future.succeededFuture());
}