private ListenableFuture<Duration> announce() { final ListenableFuture<Duration> future = announcementClient.announce(ImmutableSet.copyOf(announcements.values())); Futures.addCallback( future, new FutureCallback<Duration>() { @Override public void onSuccess(Duration duration) { errorBackOff.success(); // wait 80% of the suggested delay duration = new Duration(duration.toMillis() * 0.8, MILLISECONDS); scheduleNextAnnouncement(duration); } @Override public void onFailure(Throwable t) { Duration duration = errorBackOff.failed(t); scheduleNextAnnouncement(duration); } }, executor); return future; }
@PreDestroy public void destroy() { executor.shutdownNow(); try { executor.awaitTermination(30, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } // unannounce try { getFutureResult(announcementClient.unannounce(), DiscoveryException.class); } catch (DiscoveryException e) { if (e.getCause() instanceof ConnectException) { log.error( "Cannot connect to discovery server for unannounce: %s", e.getCause().getMessage()); } else { log.error(e); } } }