From 0533eef471556081e549dce0f2e916456856a2f9 Mon Sep 17 00:00:00 2001 From: Corey Fritz Date: Wed, 4 May 2016 14:46:35 -0400 Subject: [PATCH] Catch and log exceptions that occur when attempting to deregister a service during shutdown. Currently, if an exception is thrown when deregistering the service (maybe the registry is not available) the latch used by the DefaultLifecycleProcessor is not decremented and you have to wait for the latch timeout (30 seconds) for shutdown to complete. With this fix, the exception is logged and normal shutdown execution continues. --- .../discovery/AbstractDiscoveryLifecycle.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/AbstractDiscoveryLifecycle.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/AbstractDiscoveryLifecycle.java index c9ef3243..07f99a5a 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/AbstractDiscoveryLifecycle.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/AbstractDiscoveryLifecycle.java @@ -16,8 +16,13 @@ package org.springframework.cloud.client.discovery; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + import javax.annotation.PreDestroy; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; @@ -26,9 +31,6 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; import org.springframework.core.env.Environment; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; - /** * Lifecycle methods that may be useful and common to various DiscoveryClient implementations. * @author Spencer Gibb @@ -36,6 +38,8 @@ import java.util.concurrent.atomic.AtomicInteger; public abstract class AbstractDiscoveryLifecycle implements DiscoveryLifecycle, ApplicationContextAware, ApplicationListener { + private static final Log logger = LogFactory.getLog(AbstractDiscoveryLifecycle.class); + private boolean autoStartup = true; private AtomicBoolean running = new AtomicBoolean(false); @@ -74,7 +78,11 @@ public abstract class AbstractDiscoveryLifecycle implements DiscoveryLifecycle, @Override public void stop(Runnable callback) { - stop(); + try { + stop(); + } catch (Exception e) { + logger.error("A problem occurred attempting to stop discovery lifecycle", e); + } callback.run(); }