@Override public final T handleException(Request request, Exception exception) { if (exception instanceof InterruptedException) { throw new DiscoveryException(name + " was interrupted for " + uri); } if (exception instanceof CancellationException) { throw new DiscoveryException(name + " was canceled for " + uri); } if (exception instanceof DiscoveryException) { throw (DiscoveryException) exception; } throw new DiscoveryException(name + " failed for " + uri, exception); } }
@PreDestroy public void destroy() { executor.shutdownNow(); try { executor.awaitTermination(30, SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } // unannounce try { getFutureValue(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); } } }
@Override public void run() { Duration duration = DEFAULT_DELAY; try { duration = future.checkedGet(); if (serverUp.compareAndSet(false, true)) { log.info("Discovery server connect succeeded for announce"); } } catch (DiscoveryException e) { if (serverUp.compareAndSet(true, false)) { log.error("Cannot connect to discovery server for announce: %s", e.getMessage()); } log.debug(e, "Cannot connect to discovery server for announce"); } finally { scheduleNextAnnouncement(duration); } } }, executor);
@PreDestroy public void destroy() { executor.shutdownNow(); try { executor.awaitTermination(30, 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); } } }
@Override public void run() { Duration delay = DEFAULT_DELAY; try { ServiceDescriptors newDescriptors = future.checkedGet(); delay = newDescriptors.getMaxAge(); serviceDescriptors.set(newDescriptors); if (serverUp.compareAndSet(false, true)) { log.info("Discovery server connect succeeded for refresh (%s/%s)", type, pool); } } catch (DiscoveryException e) { if (serverUp.compareAndSet(true, false)) { log.error("Cannot connect to discovery server for refresh (%s/%s): %s", type, pool, e.getMessage()); } log.debug(e, "Cannot connect to discovery server for refresh (%s/%s)", type, pool); } finally { scheduleRefresh(delay); } } }, executor);
@Override public final T handleException(Request request, Exception exception) { if (exception instanceof InterruptedException) { throw new DiscoveryException(name + " was interrupted for " + uri); } if (exception instanceof CancellationException) { throw new DiscoveryException(name + " was canceled for " + uri); } if (exception instanceof DiscoveryException) { throw (DiscoveryException) exception; } throw new DiscoveryException(name + " failed for " + uri, exception); } }
@PreDestroy public void destroy() { executor.shutdownNow(); try { executor.awaitTermination(30, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } // unannounce try { announcementClient.unannounce().checkedGet(); } 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); } } }
@Override public final DiscoveryException handleException(Request request, Exception exception) { if (exception instanceof InterruptedException) { return new DiscoveryException(name + " was interrupted"); } if (exception instanceof CancellationException) { return new DiscoveryException(name + " was canceled"); } if (exception instanceof DiscoveryException) { throw (DiscoveryException) exception; } return new DiscoveryException(name + " failed", exception); } }
@Override public final DiscoveryException handleException(Request request, Exception exception) { if (exception instanceof InterruptedException) { return new DiscoveryException(name + " was interrupted"); } if (exception instanceof CancellationException) { return new DiscoveryException(name + " was canceled"); } if (exception instanceof DiscoveryException) { throw (DiscoveryException) exception; } return new DiscoveryException(name + " failed", exception); } }
@Override public final T handleException(Request request, Exception exception) { if (exception instanceof InterruptedException) { throw new DiscoveryException(name + " was interrupted for " + uri); } if (exception instanceof CancellationException) { throw new DiscoveryException(name + " was canceled for " + uri); } if (exception instanceof DiscoveryException) { throw (DiscoveryException) exception; } throw new DiscoveryException(name + " failed for " + uri, exception); } }
@Override public final T handleException(Request request, Exception exception) { if (exception instanceof InterruptedException) { throw new DiscoveryException(name + " was interrupted for " + uri); } if (exception instanceof CancellationException) { throw new DiscoveryException(name + " was canceled for " + uri); } if (exception instanceof DiscoveryException) { throw (DiscoveryException) exception; } throw new DiscoveryException(name + " failed for " + uri, exception); } }
@Override public DiscoveryException apply(Exception e) { if (e instanceof InterruptedException) { Thread.currentThread().interrupt(); return new DiscoveryException(name + " was interrupted"); } if (e instanceof CancellationException) { return new DiscoveryException(name + " was canceled"); } Throwable cause = e; if (e instanceof ExecutionException) { if (e.getCause() != null) { cause = e.getCause(); } } if (cause instanceof DiscoveryException) { return (DiscoveryException) cause; } return new DiscoveryException(name + " failed", cause); } });
@Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } Duration maxAge = extractMaxAge(response); return maxAge; } });
@Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } Duration maxAge = extractMaxAge(response); return maxAge; } });
@Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } Duration maxAge = extractMaxAge(response); return maxAge; } });
public ServiceDescriptors(String type, String pool, List<ServiceDescriptor> serviceDescriptors, Duration maxAge, String eTag) { requireNonNull(type, "type is null"); requireNonNull(serviceDescriptors, "serviceDescriptors is null"); requireNonNull(maxAge, "maxAge is null"); this.type = type; this.pool = pool; this.serviceDescriptors = ImmutableList.copyOf(serviceDescriptors); this.maxAge = maxAge; this.eTag = eTag; // verify service descriptors match expected type for (ServiceDescriptor serviceDescriptor : this.serviceDescriptors) { if (!type.equals(serviceDescriptor.getType()) || (pool != null && !pool.equals(serviceDescriptor.getPool()))) { throw new DiscoveryException(format("Expected %s service descriptor from pool %s, but was %s service descriptor from pool %s", type, pool, serviceDescriptor.getType(), serviceDescriptor.getPool())); } } }
public ServiceDescriptors(String type, String pool, List<ServiceDescriptor> serviceDescriptors, Duration maxAge, String eTag) { Preconditions.checkNotNull(type, "type is null"); Preconditions.checkNotNull(serviceDescriptors, "serviceDescriptors is null"); Preconditions.checkNotNull(maxAge, "maxAge is null"); this.type = type; this.pool = pool; this.serviceDescriptors = ImmutableList.copyOf(serviceDescriptors); this.maxAge = maxAge; this.eTag = eTag; // verify service descriptors match expected type for (ServiceDescriptor serviceDescriptor : this.serviceDescriptors) { if (!type.equals(serviceDescriptor.getType()) || (pool != null && !pool.equals(serviceDescriptor.getPool()))) { throw new DiscoveryException(format("Expected %s service descriptor from pool %s, but was %s service descriptor from pool %s", type, pool, serviceDescriptor.getType(), serviceDescriptor.getPool())); } } }
public ServiceDescriptors(String type, String pool, List<ServiceDescriptor> serviceDescriptors, Duration maxAge, String eTag) { Preconditions.checkNotNull(type, "type is null"); Preconditions.checkNotNull(serviceDescriptors, "serviceDescriptors is null"); Preconditions.checkNotNull(maxAge, "maxAge is null"); this.type = type; this.pool = pool; this.serviceDescriptors = ImmutableList.copyOf(serviceDescriptors); this.maxAge = maxAge; this.eTag = eTag; // verify service descriptors match expected type for (ServiceDescriptor serviceDescriptor : this.serviceDescriptors) { if (!type.equals(serviceDescriptor.getType()) || (pool != null && !pool.equals(serviceDescriptor.getPool()))) { throw new DiscoveryException(format("Expected %s service descriptor from pool %s, but was %s service descriptor from pool %s", type, pool, serviceDescriptor.getType(), serviceDescriptor.getPool())); } } }
return immediateFailedFuture(new DiscoveryException("No discovery servers are available")); throw new DiscoveryException(format("Lookup of %s failed with status code %s", type, response.getStatusCode())); throw new DiscoveryException(format("Lookup of %s failed", type), e); throw new DiscoveryException(format("Expected environment to be %s, but was %s", environment, serviceDescriptorsRepresentation.getEnvironment()));
return Futures.immediateFailedCheckedFuture(new DiscoveryException("No discovery servers are available")); throw new DiscoveryException(format("Lookup of %s failed with status code %s", type, response.getStatusCode())); throw new DiscoveryException(format("Lookup of %s failed", type), e); throw new DiscoveryException(format("Expected environment to be %s, but was %s", environment, serviceDescriptorsRepresentation.getEnvironment()));