List of usage examples for io.vertx.core Future failedFuture
static <T> Future<T> failedFuture(String failureMessage)
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
License:Open Source License
private void reconnect(final Handler<AsyncResult<HonoClient>> connectionHandler, final Handler<ProtonConnection> disconnectHandler) { if (clientOptions == null || clientOptions.getReconnectAttempts() == 0) { connectionHandler.handle(Future.failedFuture("failed to connect")); } else {// w w w. j a v a 2 s . c om LOG.trace("scheduling re-connect attempt ..."); // give Vert.x some time to clean up NetClient vertx.setTimer(Constants.DEFAULT_RECONNECT_INTERVAL_MILLIS, tid -> { LOG.debug("attempting to re-connect to server [{}:{}]", connectionFactory.getHost(), connectionFactory.getPort()); connect(clientOptions, connectionHandler, disconnectHandler); }); } }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
License:Open Source License
void getOrCreateSender(final String key, final Consumer<Handler<AsyncResult<MessageSender>>> newSenderSupplier, final Handler<AsyncResult<MessageSender>> resultHandler) { final MessageSender sender = activeSenders.get(key); if (sender != null && sender.isOpen()) { LOG.debug("reusing existing message sender [target: {}, credit: {}]", key, sender.getCredit()); resultHandler.handle(Future.succeededFuture(sender)); } else if (!senderCreationLocks.computeIfAbsent(key, k -> Boolean.FALSE)) { // 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 -> { // remove lock so that next attempt to open a sender doesn't fail senderCreationLocks.remove(key); resultHandler.handle(Future.failedFuture("connection to server lost")); };//from w w w.ja v a 2s . c om creationRequests.add(connectionFailureHandler); senderCreationLocks.put(key, Boolean.TRUE); LOG.debug("creating new message sender for {}", key); newSenderSupplier.accept(creationAttempt -> { if (creationAttempt.succeeded()) { MessageSender newSender = creationAttempt.result(); LOG.debug("successfully created new message sender for {}", key); activeSenders.put(key, newSender); } else { LOG.debug("failed to create new message sender for {}", key, creationAttempt.cause()); activeSenders.remove(key); } senderCreationLocks.remove(key); creationRequests.remove(connectionFailureHandler); resultHandler.handle(creationAttempt); }); } else { LOG.debug("already trying to create a message sender for {}", key); resultHandler.handle(Future.failedFuture("sender link not established yet")); } }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
License:Open Source License
@Override public HonoClient createTelemetryConsumer(final String tenantId, final int prefetch, final Consumer<Message> telemetryConsumer, 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")); };//from ww w . jav a2 s.co m creationRequests.add(connectionFailureHandler); Future<MessageConsumer> consumerTracker = Future.future(); consumerTracker.setHandler(attempt -> { creationRequests.remove(connectionFailureHandler); creationHandler.handle(attempt); }); checkConnection().compose(connected -> TelemetryConsumerImpl.create(context, connection, tenantId, connectionFactory.getPathSeparator(), prefetch, telemetryConsumer, consumerTracker.completer()), consumerTracker); return this; }
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")); };// w w w. ja v a 2 s. c om 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 Future<ProtonConnection> checkConnection() { if (connection == null || connection.isDisconnected()) { return Future.failedFuture("client is not connected to server (yet)"); } else {//from w w w .j a v a2 s . c o m return Future.succeededFuture(connection); } }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
License:Open Source License
@Override public HonoClient createRegistrationClient(final String tenantId, final Handler<AsyncResult<RegistrationClient>> creationHandler) { Objects.requireNonNull(tenantId); if (connection == null || connection.isDisconnected()) { creationHandler.handle(Future.failedFuture("client is not connected to server (yet)")); } else {/*from w w w. ja va2 s . c o m*/ // 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")); }; creationRequests.add(connectionFailureHandler); LOG.debug("creating new registration client for [{}]", tenantId); RegistrationClientImpl.create(context, connection, tenantId, creationAttempt -> { if (creationAttempt.succeeded()) { activeRegClients.put(tenantId, creationAttempt.result()); LOG.debug("successfully created registration client for [{}]", tenantId); } else { LOG.debug("failed to create registration client for [{}]", tenantId, creationAttempt.cause()); } creationRequests.remove(connectionFailureHandler); creationHandler.handle(creationAttempt); }); } return this; }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
License:Open Source License
@Override public HonoClient createCredentialsClient(final String tenantId, final Handler<AsyncResult<CredentialsClient>> creationHandler) { Objects.requireNonNull(tenantId); Objects.requireNonNull(creationHandler); if (connection == null || connection.isDisconnected()) { creationHandler.handle(Future.failedFuture("client is not connected to server (yet)")); } else {/*from www . j a v a 2 s . c om*/ // 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")); }; creationRequests.add(connectionFailureHandler); LOG.debug("creating new credentials client for [{}]", tenantId); CredentialsClientImpl.create(context, connection, tenantId, creationAttempt -> { if (creationAttempt.succeeded()) { activeCredClients.put(tenantId, creationAttempt.result()); LOG.debug("successfully created credentials client for [{}]", tenantId); } else { LOG.debug("failed to create credentials client for [{}]", tenantId, creationAttempt.cause()); } creationRequests.remove(connectionFailureHandler); creationHandler.handle(creationAttempt); }); } return this; }
From source file:org.eclipse.hono.client.impl.TelemetryConsumerImpl.java
License:Open Source License
/** * Creates a new telemetry data consumer for a tenant. * /* w w w.j av a 2 s .co m*/ * @param context The vert.x context to run all interactions with the server on. * @param con The AMQP connection to the server. * @param tenantId The tenant to consumer events for. * @param pathSeparator The address path separator character used by the server. * @param prefetch the number of message credits the consumer grants and replenishes automatically as messages are * delivered. To manage credit manually, you can instead set prefetch to 0. * @param telemetryConsumer The consumer to invoke with each telemetry message received. * @param creationHandler The handler to invoke with the outcome of the creation attempt. * @throws NullPointerException if any of the parameters is {@code null}. */ public static void create(final Context context, final ProtonConnection con, final String tenantId, final String pathSeparator, final int prefetch, final Consumer<Message> telemetryConsumer, final Handler<AsyncResult<MessageConsumer>> creationHandler) { Objects.requireNonNull(context); Objects.requireNonNull(con); Objects.requireNonNull(tenantId); Objects.requireNonNull(pathSeparator); Objects.requireNonNull(telemetryConsumer); Objects.requireNonNull(creationHandler); createConsumer(context, con, tenantId, pathSeparator, TELEMETRY_ADDRESS_TEMPLATE, ProtonQoS.AT_MOST_ONCE, prefetch, (protonDelivery, message) -> telemetryConsumer.accept(message)).setHandler(created -> { if (created.succeeded()) { creationHandler.handle( Future.succeededFuture(new TelemetryConsumerImpl(context, created.result()))); } else { creationHandler.handle(Future.failedFuture(created.cause())); } }); }
From source file:org.eclipse.hono.client.impl.TelemetrySenderImpl.java
License:Open Source License
/** * Creates a new sender for publishing telemetry data to a Hono server. * //from w w w . j a v a2 s . co m * @param context The vertx context to run all interactions with the server on. * @param con The connection to the Hono server. * @param tenantId The tenant that the telemetry data will be uploaded for. * @param deviceId The device that the telemetry data will be uploaded for or {@code null} * if the data to be uploaded will be produced by arbitrary devices of the * tenant. * @param closeHook The handler to invoke when the Hono server closes the sender. The sender's * target address is provided as an argument to the handler. * @param creationHandler The handler to invoke with the result of the creation attempt. * @throws NullPointerException if any of context, connection, tenant or handler is {@code null}. */ public static void create(final Context context, final ProtonConnection con, final String tenantId, final String deviceId, final Handler<String> closeHook, final Handler<AsyncResult<MessageSender>> creationHandler) { Objects.requireNonNull(context); Objects.requireNonNull(con); Objects.requireNonNull(tenantId); Objects.requireNonNull(creationHandler); final String targetAddress = getTargetAddress(tenantId, deviceId); createSender(context, con, targetAddress, ProtonQoS.AT_MOST_ONCE, closeHook).setHandler(created -> { if (created.succeeded()) { creationHandler.handle(Future.succeededFuture( new TelemetrySenderImpl(created.result(), tenantId, targetAddress, context, closeHook))); } else { creationHandler.handle(Future.failedFuture(created.cause())); } }); }
From source file:org.eclipse.hono.client.impl.TenantClientImpl.java
License:Open Source License
/** * Creates a new tenant client./*from w ww.ja va2s. c o m*/ * * @param context The vert.x context to run all interactions with the server on. * @param clientConfig The configuration properties to use. * @param cacheProvider A factory for cache instances for tenant configuration results. If {@code null} * the client will not cache any results from the Tenant service. * @param con The AMQP connection to the server. * @param senderCloseHook A handler to invoke if the peer closes the sender link unexpectedly. * @param receiverCloseHook A handler to invoke if the peer closes the receiver link unexpectedly. * @param creationHandler The handler to invoke with the outcome of the creation attempt. * @throws NullPointerException if any of the parameters, except for senderCloseHook and receiverCloseHook, is {@code null}. */ public final static void create(final Context context, final ClientConfigProperties clientConfig, final CacheProvider cacheProvider, final ProtonConnection con, final Handler<String> senderCloseHook, final Handler<String> receiverCloseHook, final Handler<AsyncResult<TenantClient>> creationHandler) { LOG.debug("creating new tenant client"); final TenantClientImpl client = new TenantClientImpl(context, clientConfig); if (cacheProvider != null) { client.setResponseCache(cacheProvider.getCache(TenantClientImpl.getTargetAddress())); } client.createLinks(con, senderCloseHook, receiverCloseHook).setHandler(s -> { if (s.succeeded()) { LOG.debug("successfully created tenant client"); creationHandler.handle(Future.succeededFuture(client)); } else { LOG.debug("failed to create tenant client", s.cause()); creationHandler.handle(Future.failedFuture(s.cause())); } }); }