ConsulServiceListener implements ServerLifecycleListener { void register( String applicationScheme, int applicationPort, int adminPort, Collection<String> hosts) { try { advertiser.register(applicationScheme, applicationPort, adminPort, hosts); scheduler.ifPresent(ScheduledExecutorService::shutdownNow); } catch (ConsulException e) { LOGGER.error("Failed to register service in Consul", e); retryInterval.ifPresent( (interval) -> { scheduler.ifPresent( (service) -> { LOGGER.info( "Will try to register service again in {} seconds", interval.toSeconds()); service.schedule( () -> register(applicationScheme, applicationPort, adminPort, hosts), interval.toSeconds(), TimeUnit.SECONDS); }); }); } } ConsulServiceListener(
      final ConsulAdvertiser advertiser,
      final Optional<Duration> retryInterval,
      final Optional<ScheduledExecutorService> scheduler); @Override void serverStarted(final Server server);  }
@Test public void testRegister() { final ConsulServiceListener listener = new ConsulServiceListener( advertiser, Optional.of(Duration.milliseconds(1)), Optional.of(scheduler)); when(advertiser.register(any(), anyInt(), anyInt(), anyCollection())) .thenThrow(new ConsulException("Cannot connect to Consul")) .thenReturn(true); Collection<String> hosts = Sets.of("192.168.1.22"); listener.register("http", 0, 0, hosts); verify(advertiser, timeout(100).atLeast(1)).register("http", 0, 0, hosts); }