From 5bd32c01b879da5220e7e309e86bc730f0fab33b Mon Sep 17 00:00:00 2001 From: Christian Dupuis Date: Wed, 8 Oct 2014 14:22:52 +0200 Subject: [PATCH] Add shutdown hook for Hystrix to reset internal state and thread pool. Additionally added some license headers and @author tags. fixes #17 --- .../cloud/netflix/hystrix/EnableHystrix.java | 18 +++++++++- .../netflix/hystrix/HystrixConfiguration.java | 34 ++++++++++++++++++- .../hystrix/HystrixConfigurationSelector.java | 18 +++++++++- .../netflix/hystrix/HystrixConfigurer.java | 15 ++++++++ .../hystrix/HystrixStreamEndpoint.java | 19 +++++++++-- 5 files changed, 98 insertions(+), 6 deletions(-) diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/EnableHystrix.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/EnableHystrix.java index d2c5a4db..509548f6 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/EnableHystrix.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/EnableHystrix.java @@ -1,3 +1,18 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.cloud.netflix.hystrix; import org.springframework.context.annotation.AdviceMode; @@ -14,7 +29,8 @@ import java.lang.annotation.*; @Documented @Import(HystrixConfigurationSelector.class) public @interface EnableHystrix { - /** + + /** * Indicate whether subclass-based (CGLIB) proxies are to be created ({@code true}) as * opposed to standard Java interface-based proxies ({@code false}). The default is * {@code false}. Applicable only if {@link #mode()} is set to diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfiguration.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfiguration.java index 32485d3c..443a8463 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfiguration.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfiguration.java @@ -1,3 +1,18 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.cloud.netflix.hystrix; import java.io.IOException; @@ -9,6 +24,7 @@ import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.metrics.GaugeService; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -23,12 +39,14 @@ import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.hystrix.Hystrix; import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsPoller; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsPoller.MetricsAsJsonPollerListener; /** * @author Spencer Gibb + * @author Christian Dupuis */ @Configuration public class HystrixConfiguration implements ImportAware { @@ -36,9 +54,14 @@ public class HystrixConfiguration implements ImportAware { private AnnotationAttributes enableHystrix; @Bean - HystrixCommandAspect hystrixCommandAspect() { + public HystrixCommandAspect hystrixCommandAspect() { return new HystrixCommandAspect(); } + + @Bean + public HystrixShutdownHook hystrixShutdownHook() { + return new HystrixShutdownHook(); + } @Bean // TODO: add enable/disable @@ -169,4 +192,13 @@ public class HystrixConfiguration implements ImportAware { } } + + private class HystrixShutdownHook implements DisposableBean { + + @Override + public void destroy() throws Exception { + Hystrix.reset(); + + } + } } diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurationSelector.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurationSelector.java index 85aa07ea..f1b75f80 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurationSelector.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurationSelector.java @@ -1,3 +1,18 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.cloud.netflix.hystrix; import org.springframework.context.annotation.AdviceMode; @@ -8,7 +23,8 @@ import org.springframework.context.annotation.AutoProxyRegistrar; * @author Spencer Gibb */ public class HystrixConfigurationSelector extends AdviceModeImportSelector { - /** + + /** * The name of the AspectJ transaction management @{@code Configuration} class. */ private static final String TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME = diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurer.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurer.java index 51e2f8d3..6d3d2725 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurer.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurer.java @@ -1,3 +1,18 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.cloud.netflix.hystrix; /** diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpoint.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpoint.java index 9c0aa3ca..031068ec 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpoint.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpoint.java @@ -1,3 +1,18 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.cloud.netflix.hystrix; import org.springframework.cloud.netflix.endpoint.ServletWrappingEndpoint; @@ -5,9 +20,7 @@ import org.springframework.cloud.netflix.endpoint.ServletWrappingEndpoint; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; /** - * User: spencergibb - * Date: 4/22/14 - * Time: 3:16 PM + * @author Spencer Gibb */ public class HystrixStreamEndpoint extends ServletWrappingEndpoint {