Example usage for io.vertx.core Future future

List of usage examples for io.vertx.core Future future

Introduction

In this page you can find the example usage for io.vertx.core Future future.

Prototype

future

Source Link

Usage

From source file:org.eclipse.hono.client.impl.HonoClientImpl.java

License:Open Source License

@Override
public HonoClient createEventConsumer(final String tenantId, final int prefetch,
        final BiConsumer<ProtonDelivery, Message> eventConsumer,
        final Handler<AsyncResult<MessageConsumer>> creationHandler) {

    // register a handler to be notified if the underlying connection to the server fails
    // so that we can fail the result handler passed in
    final Handler<Void> connectionFailureHandler = connectionLost -> {
        creationHandler.handle(Future.failedFuture("connection to server lost"));
    };// www. j  a  v  a  2s  . c o  m
    creationRequests.add(connectionFailureHandler);

    Future<MessageConsumer> consumerTracker = Future.future();
    consumerTracker.setHandler(attempt -> {
        creationRequests.remove(connectionFailureHandler);
        creationHandler.handle(attempt);
    });
    checkConnection().compose(
            connected -> EventConsumerImpl.create(context, connection, tenantId,
                    connectionFactory.getPathSeparator(), prefetch, eventConsumer, consumerTracker.completer()),
            consumerTracker);
    return this;
}

From source file:org.eclipse.hono.client.impl.HonoClientImpl.java

License:Open Source License

private HonoClient createEventSender(final String tenantId, final String deviceId,
        final Handler<AsyncResult<MessageSender>> creationHandler) {

    Future<MessageSender> senderTracker = Future.future();
    senderTracker.setHandler(creationHandler);
    checkConnection().compose(/*w  w  w  .ja v a  2s .  co  m*/
            connected -> EventSenderImpl.create(context, connection, tenantId, deviceId, onSenderClosed -> {
                activeSenders.remove(EventSenderImpl.getTargetAddress(tenantId, deviceId));
            }, senderTracker.completer()), senderTracker);
    return this;
}

From source file:org.eclipse.hono.client.impl.TenantClientImpl.java

License:Open Source License

/**
 * {@inheritDoc}//from   w w  w .ja  v  a  2s.com
 */
@Override
public final Future<TenantObject> get(final String tenantId) {

    final TriTuple<TenantAction, String, Object> key = TriTuple.of(TenantAction.get, tenantId, null);

    return getResponseFromCache(key).recover(t -> {
        final Future<TenantResult<TenantObject>> tenantResult = Future.future();
        final JsonObject payload = new JsonObject().put(TenantConstants.FIELD_PAYLOAD_TENANT_ID, tenantId);
        createAndSendRequest(TenantConstants.TenantAction.get.toString(),
                customizeRequestApplicationProperties(), payload.toBuffer(), tenantResult.completer(), key);
        return tenantResult;
    }).map(tenantResult -> {
        switch (tenantResult.getStatus()) {
        case HttpURLConnection.HTTP_OK:
            return tenantResult.getPayload();
        default:
            throw StatusCodeMapper.from(tenantResult);
        }
    });
}

From source file:org.eclipse.hono.client.impl.TenantClientImpl.java

License:Open Source License

/**
 * {@inheritDoc}//w ww  .java 2  s.  c o  m
 */
@Override
public final Future<TenantObject> get(final X500Principal subjectDn) {

    final TriTuple<TenantAction, X500Principal, Object> key = TriTuple.of(TenantAction.get, subjectDn, null);

    return getResponseFromCache(key).recover(t -> {
        final Future<TenantResult<TenantObject>> tenantResult = Future.future();
        final JsonObject payload = new JsonObject().put(TenantConstants.FIELD_PAYLOAD_SUBJECT_DN,
                subjectDn.getName(X500Principal.RFC2253));
        createAndSendRequest(TenantConstants.TenantAction.get.toString(),
                customizeRequestApplicationProperties(), payload.toBuffer(), tenantResult.completer(), key);
        return tenantResult;
    }).map(tenantResult -> {
        switch (tenantResult.getStatus()) {
        case HttpURLConnection.HTTP_OK:
            return tenantResult.getPayload();
        default:
            throw StatusCodeMapper.from(tenantResult);
        }
    });
}

From source file:org.eclipse.hono.deviceregistry.Application.java

License:Open Source License

@Override
protected final Future<Void> deployRequiredVerticles(int maxInstances) {

    Future<Void> result = Future.future();
    CompositeFuture.all(deployAuthenticationService(), // we only need 1 authentication service
            deployRegistrationService(), deployCredentialsService()).setHandler(ar -> {
                if (ar.succeeded()) {
                    result.complete();//from  w  w  w . j  a  v a 2s  . co  m
                } else {
                    result.fail(ar.cause());
                }
            });
    return result;
}

From source file:org.eclipse.hono.deviceregistry.Application.java

License:Open Source License

private Future<String> deployCredentialsService() {
    Future<String> result = Future.future();
    if (credentialsService != null) {
        log.info("Starting credentials service {}", credentialsService);
        getVertx().deployVerticle(credentialsService, result.completer());
    } else {/*  ww  w.j a v a2 s .c o m*/
        result.complete();
    }
    return result;
}

From source file:org.eclipse.hono.deviceregistry.Application.java

License:Open Source License

private Future<String> deployAuthenticationService() {
    Future<String> result = Future.future();
    if (!Verticle.class.isInstance(authenticationService)) {
        result.fail("authentication service is not a verticle");
    } else {/*from  www. j  av  a 2 s  .c om*/
        log.info("Starting authentication service {}", authenticationService);
        getVertx().deployVerticle((Verticle) authenticationService, result.completer());
    }
    return result;
}

From source file:org.eclipse.hono.deviceregistry.Application.java

License:Open Source License

private Future<String> deployRegistrationService() {
    log.info("Starting registration service {}", registrationService);
    Future<String> result = Future.future();
    getVertx().deployVerticle(registrationService, result.completer());
    return result;
}

From source file:org.eclipse.hono.deviceregistry.FileBasedCredentialsService.java

License:Open Source License

@Override
protected void doStart(final Future<Void> startFuture) throws Exception {
    if (!running) {
        loadCredentials().compose(s -> {
            if (getConfig().isSaveToFile()) {
                log.info("saving credentials to file every 3 seconds");
                vertx.setPeriodic(3000, saveIdentities -> {
                    saveToFile(Future.future());
                });//from  ww w . j a  v a2  s.  c om
            } else {
                log.info("persistence is disabled, will not save credentials to file");
            }
            running = true;
            startFuture.complete();
        }, startFuture);
    } else {
        startFuture.complete();
    }
}

From source file:org.eclipse.hono.deviceregistry.FileBasedCredentialsService.java

License:Open Source License

Future<Void> loadCredentials() {
    Future<Void> result = Future.future();
    if (getConfig().getCredentialsFilename() == null) {
        result.fail(new IllegalStateException("credentials filename is not set"));
    } else {// w w  w  .j  a  va2 s  .c o  m
        final FileSystem fs = vertx.fileSystem();
        log.debug("trying to load credentials information from file {}", getConfig().getCredentialsFilename());

        if (fs.existsBlocking(getConfig().getCredentialsFilename())) {
            log.info("loading credentials from file [{}]", getConfig().getCredentialsFilename());
            fs.readFile(getConfig().getCredentialsFilename(), readAttempt -> {
                if (readAttempt.succeeded()) {
                    JsonArray allObjects = readAttempt.result().toJsonArray();
                    parseCredentials(allObjects);
                    result.complete();
                } else {
                    result.fail(readAttempt.cause());
                }
            });
        } else {
            log.debug("credentials file [{}] does not exist (yet)", getConfig().getCredentialsFilename());
            result.complete();
        }
    }
    return result;
}