private void onDisconnect() { LOG.info("Connecting client..."); vertx.setTimer(connectionRetryInterval, reconnect -> { LOG.info("attempting to re-connect to Hono ..."); client.connect(con -> onDisconnect()); }); }
private void onDisconnect(final ProtonConnection con) { // give Vert.x some time to clean up NetClient vertx.setTimer(connectionRetryInterval, reconnect -> { LOG.info("attempting to re-connect to Hono ..."); client.connect(this::onDisconnect) .compose(this::createConsumer); }); }
/** * Starts this component. * <p> * * @return A future indicating the outcome of the startup process. */ @PostConstruct Future<CompositeFuture> start() { return client.connect(this::onDisconnect) .compose(this::createConsumer) .setHandler(this::handleCreateConsumerStatus); }
private Handler<ProtonConnection> getHandlerForDisconnectHonoService( final HonoClient client, final String serviceName, final Handler<HonoClient> connectHandler, final Handler<HonoClient> connectionLostHandler) { return (connection) -> { connectionLostHandler.handle(client); vertx.setTimer(Constants.DEFAULT_RECONNECT_INTERVAL_MILLIS, reconnect -> { LOG.info("attempting to reconnect to {}", serviceName); client.connect(getHandlerForDisconnectHonoService(client, serviceName, connectHandler, connectionLostHandler)).setHandler(connectAttempt -> { if (connectAttempt.succeeded()) { LOG.info("reconnected to {}", serviceName); connectHandler.handle(connectAttempt.result()); } else { LOG.debug("cannot reconnect to {}: {}", serviceName, connectAttempt.cause().getMessage()); } }); }); }; }
private Handler<ProtonConnection> getHandlerForDisconnectHonoService( final HonoClient client, final String serviceName, final Handler<HonoClient> connectHandler, final Handler<HonoClient> connectionLostHandler) { return (connection) -> { connectionLostHandler.handle(client); vertx.setTimer(Constants.DEFAULT_RECONNECT_INTERVAL_MILLIS, reconnect -> { LOG.info("attempting to reconnect to {}", serviceName); client.connect(getHandlerForDisconnectHonoService(client, serviceName, connectHandler, connectionLostHandler)).setHandler(connectAttempt -> { if (connectAttempt.succeeded()) { LOG.info("reconnected to {}", serviceName); connectHandler.handle(connectAttempt.result()); } else { LOG.debug("cannot reconnect to {}: {}", serviceName, connectAttempt.cause().getMessage()); } }); }); }; }
/** * Starts this component. * */ @PostConstruct void start() { workerExecutor = vertx.createSharedWorkerExecutor("user-input-pool", 3, TimeUnit.HOURS.toNanos(1)); startCommandClient(client.connect(x -> onDisconnect())); }
private Future<HonoClient> connectToRegistrationService() { if (registrationHonoClient == null) { LOGGER.info("no client for Registration Service configured"); return Future.succeededFuture(null); } else { return registrationHonoClient .connect(getClientOptions()) .map(client -> { LOGGER.info("connected to Device Registration service [{}:{}]", sampler.getRegistryHost(), sampler.getRegistryPort()); return client; }); } }
private Future<HonoClient> connect() { return client .connect() .map(client -> { LOG.info("connected to Hono [{}:{}]", sampler.getHost(), sampler.getPort()); return client; }); }
connectionEstablishedHandler, connectionLostHandler); return client.connect(disconnectHandler).map(connectedClient -> { LOG.info("connected to {}", serviceName); connectionEstablishedHandler.handle(connectedClient);
connectionEstablishedHandler, connectionLostHandler); return client.connect(disconnectHandler).map(connectedClient -> { LOG.info("connected to {}", serviceName); connectionEstablishedHandler.handle(connectedClient);
private Future<HonoClient> connect() { return amqpNetworkClient .connect(getClientOptions()) .map(client -> { LOGGER.info("connected to AMQP Messaging Network [{}:{}]", sampler.getHost(), sampler.getPort()); return client; }); }
private Future<HonoClient> connectToHonoMessaging() { return honoClient .connect(getClientOptions()) .map(client -> { LOGGER.info("connected to Hono Messaging [{}:{}]", sampler.getHost(), sampler.getPort()); return client; }); }
private Future<RegistrationClient> getRegistrationClient() { return honoRegistryClient .connect(new ProtonClientOptions()) .compose(connectedClient -> connectedClient.getOrCreateRegistrationClient(HonoExampleConstants.TENANT_ID)); }
/** * Method to act as a disconnect handler. If called, it tries to reconnect to Hono by creating the MessageConsumer * again and continue to wait for incoming downstream messages. * <p> * The reconnection attempt is delayed by {@link #DEFAULT_CONNECT_TIMEOUT_MILLIS} milliseconds and set this method * as disconnect handler again (for potential future disconnect handling). * * @param con The ProtonConnection for that the connection was lost. */ private void onDisconnect(final ProtonConnection con) { // give Vert.x some time to clean up NetClient vertx.setTimer(DEFAULT_CONNECT_TIMEOUT_MILLIS, reconnect -> { LOG.info("attempting to re-connect to Hono ..."); honoClient.connect(this::onDisconnect). compose(connectedClient -> createConsumer()). map(messageConsumer -> { LOG.info("Reconnected to Hono."); return null; }); }); }
/** * Initiate the connection and set the message handling method to treat data that is received. * * @throws Exception Thrown if the latch is interrupted during waiting or if the read from System.in throws an IOException. */ protected void consumeData() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final Future<MessageConsumer> consumerFuture = Future.future(); consumerFuture.setHandler(result -> { if (!result.succeeded()) { LOG.error("honoClient could not create downstream consumer for [{}:{}]", HonoExampleConstants.HONO_AMQP_CONSUMER_HOST, HonoExampleConstants.HONO_AMQP_CONSUMER_PORT, result.cause()); } latch.countDown(); }); honoClient.connect(this::onDisconnect). compose(connectedClient -> createConsumer()). setHandler(consumerFuture.completer()); latch.await(); if (consumerFuture.succeeded()) { System.in.read(); } vertx.close(); }
private Future<MessageSender> getMessageSender() { return honoMessagingClient .connect(new ProtonClientOptions()) .compose(connectedClient -> { if (isEventMode()) { return connectedClient.getOrCreateEventSender(HonoExampleConstants.TENANT_ID); } else { return connectedClient.getOrCreateTelemetrySender(HonoExampleConstants.TENANT_ID); } }); }
/** * Verifies that the adapter connects to required services during * startup and invokes the <em>onCommandConnectionEstablished</em> and * <em>doStart</em> methods. * * @param ctx The vert.x test context. */ @SuppressWarnings("unchecked") @Test public void testStartInternalConnectsToServices(final TestContext ctx) { // GIVEN an adapter configured with service clients // that can connect to the corresponding services final Handler<Void> startupHandler = mock(Handler.class); final Handler<Void> commandConnectionHandler = mock(Handler.class); final Handler<Void> commandConnectionLostHandler = mock(Handler.class); givenAnAdapterConfiguredWithServiceClients(startupHandler, commandConnectionHandler, commandConnectionLostHandler); // WHEN starting the adapter adapter.startInternal().setHandler(ctx.asyncAssertSuccess(ok -> { // THEN the service clients have connected verify(tenantService).connect(any(Handler.class)); verify(registrationService).connect(any(Handler.class)); verify(messagingService).connect(any(Handler.class)); verify(credentialsService).connect(any(Handler.class)); verify(commandConnection).connect(any(Handler.class)); verify(startupHandler).handle(null); verify(commandConnectionHandler).handle(null); verify(commandConnectionLostHandler, never()).handle(null); })); }
when(tenantService.connect(any(Handler.class))).thenReturn(Future.succeededFuture(tenantService)); when(registrationService.connect(any(Handler.class))).thenReturn(Future.succeededFuture(registrationService)); when(credentialsService.connect(any(Handler.class))).thenReturn(Future.succeededFuture(credentialsService)); when(messagingService.connect(any(Handler.class))).thenReturn(Future.succeededFuture(messagingService));