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 02a01c73..fc65130b 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 @@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.metrics.GaugeService; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.SmartLifecycle; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -58,17 +59,22 @@ public class HystrixConfiguration implements ImportAware { public HystrixCommandAspect hystrixCommandAspect() { return new HystrixCommandAspect(); } - + @Bean public HystrixShutdownHook hystrixShutdownHook() { return new HystrixShutdownHook(); } - @Bean - @ConditionalOnExpression("${hystrix.stream.endpoint.enabled:true}") - // TODO: make it @ConditionalOnWebApp (need a nested class) - public HystrixStreamEndpoint hystrixStreamEndpoint() { - return new HystrixStreamEndpoint(); + @Configuration + @ConditionalOnExpression("${hystrix.stream.endpoint.enabled:true}") + @ConditionalOnWebApplication + protected static class HystrixWebConfiguration { + + @Bean + public HystrixStreamEndpoint hystrixStreamEndpoint() { + return new HystrixStreamEndpoint(); + } + } @Override @@ -88,7 +94,7 @@ public class HystrixConfiguration implements ImportAware { private static Log logger = LogFactory .getLog(HystrixMetricsPollerConfiguration.class); - @Autowired + @Autowired(required=false) private GaugeService gauges; private ObjectMapper mapper = new ObjectMapper(); @@ -100,6 +106,9 @@ public class HystrixConfiguration implements ImportAware { @Override public void start() { + if (gauges==null) { + return; + } MetricsAsJsonPollerListener listener = new MetricsAsJsonPollerListener() { @Override public void handleJsonMetric(String json) { @@ -157,7 +166,7 @@ public class HystrixConfiguration implements ImportAware { @Override public boolean isRunning() { - return poller!=null ? poller.isRunning() : false; + return poller != null ? poller.isRunning() : false; } @Override @@ -179,10 +188,10 @@ public class HystrixConfiguration implements ImportAware { } } - + /** - * {@link DisposableBean} that makes sure that Hystrix internal state is cleared - * when {@link ApplicationContext} shuts down. + * {@link DisposableBean} that makes sure that Hystrix internal state is cleared when + * {@link ApplicationContext} shuts down. */ private class HystrixShutdownHook implements DisposableBean { @@ -191,6 +200,6 @@ public class HystrixConfiguration implements ImportAware { // Just call Hystrix to reset thread pool etc. Hystrix.reset(); } - + } } 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 031068ec..fdb74d36 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 @@ -25,6 +25,6 @@ import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServl public class HystrixStreamEndpoint extends ServletWrappingEndpoint { public HystrixStreamEndpoint() { - super(HystrixMetricsStreamServlet.class, "hystrixStream", "hystrix.stream", false, true); + super(HystrixMetricsStreamServlet.class, "hystrixStream", "/hystrix.stream", false, true); } } diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurationTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurationTests.java new file mode 100644 index 00000000..00afcaeb --- /dev/null +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixConfigurationTests.java @@ -0,0 +1,32 @@ +/* + * 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.junit.Test; +import org.springframework.boot.builder.SpringApplicationBuilder; + +/** + * @author Dave Syer + * + */ +public class HystrixConfigurationTests { + + @Test + public void nonWebAppStartsUp() { + new SpringApplicationBuilder(HystrixConfiguration.class).web(false).run().close(); + } + +} diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java new file mode 100644 index 00000000..02d8c020 --- /dev/null +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java @@ -0,0 +1,34 @@ +/* + * 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 static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * @author Dave Syer + * + */ +public class HystrixStreamEndpointTests { + + @Test + public void pathStartsWithSlash() { + HystrixStreamEndpoint endpoint = new HystrixStreamEndpoint(); + assertEquals("/hystrix.stream", endpoint.getPath()); + } + +}