You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2017/03/16 10:43:29 UTC
camel git commit: CAMEL-11023: Hystrix Starter : add an option to
configure additional configuration via spring-boot properties
Repository: camel
Updated Branches:
refs/heads/master 2467a1920 -> 5889715e4
CAMEL-11023: Hystrix Starter : add an option to configure additional configuration via spring-boot properties
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5889715e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5889715e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5889715e
Branch: refs/heads/master
Commit: 5889715e4fe9d9ff5259eeec6ce528b45c0ce751
Parents: 2467a19
Author: lburgazzoli <lb...@gmail.com>
Authored: Thu Mar 16 11:02:15 2017 +0100
Committer: lburgazzoli <lb...@gmail.com>
Committed: Thu Mar 16 11:42:51 2017 +0100
----------------------------------------------------------------------
.../springboot/HystrixAutoConfiguration.java | 58 +++++++++++-
.../springboot/HystrixConfiguration.java | 8 ++
.../processor/HystrixMultiConfiguration.java | 57 ++++++++++++
.../HystrixMultiConfigurationTest.java | 96 ++++++++++++++++++++
4 files changed, 217 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/5889715e/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixAutoConfiguration.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixAutoConfiguration.java
index def622c..a486065 100644
--- a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixAutoConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixAutoConfiguration.java
@@ -18,12 +18,22 @@ package org.apache.camel.component.hystrix.springboot;
import java.util.HashMap;
import java.util.Map;
+import javax.annotation.PostConstruct;
import org.apache.camel.CamelContext;
import org.apache.camel.component.hystrix.processor.HystrixConstants;
+import org.apache.camel.model.HystrixConfigurationCommon;
import org.apache.camel.model.HystrixConfigurationDefinition;
import org.apache.camel.spring.boot.CamelAutoConfiguration;
import org.apache.camel.util.IntrospectionSupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanCreationException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -41,12 +51,25 @@ import org.springframework.context.annotation.Configuration;
@ConditionalOnBean(value = CamelAutoConfiguration.class)
@AutoConfigureAfter(value = CamelAutoConfiguration.class)
@EnableConfigurationProperties(HystrixConfiguration.class)
-public class HystrixAutoConfiguration {
+public class HystrixAutoConfiguration implements BeanFactoryAware {
+ private static final Logger LOGGER = LoggerFactory.getLogger(HystrixAutoConfiguration.class);
+
+ private BeanFactory beanFactory;
+
+ @Autowired
+ private CamelContext camelContext;
+ @Autowired
+ private HystrixConfiguration config;
+
+ @Override
+ public void setBeanFactory(BeanFactory factory) throws BeansException {
+ beanFactory = factory;
+ }
@Bean(name = HystrixConstants.DEFAULT_HYSTRIX_CONFIGURATION_ID)
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(name = HystrixConstants.DEFAULT_HYSTRIX_CONFIGURATION_ID)
- HystrixConfigurationDefinition defaultHystrixConfigurationDefinition(CamelContext camelContext, HystrixConfiguration config) throws Exception {
+ public HystrixConfigurationDefinition defaultHystrixConfigurationDefinition() throws Exception {
Map<String, Object> properties = new HashMap<>();
IntrospectionSupport.getProperties(config, properties, null, false);
@@ -56,4 +79,35 @@ public class HystrixAutoConfiguration {
return definition;
}
+
+ @PostConstruct
+ public void addHystrixConfigurations() {
+ if (!(beanFactory instanceof ConfigurableBeanFactory)) {
+ LOGGER.warn("BeanFactory is not of type ConfigurableBeanFactory");
+ return;
+ }
+
+ final ConfigurableBeanFactory factory = (ConfigurableBeanFactory) beanFactory;
+ final Map<String, Object> properties = new HashMap<>();
+
+ for (Map.Entry<String, HystrixConfigurationCommon> entry : config.getConfigurations().entrySet()) {
+
+ // clear the properties map for reuse
+ properties.clear();
+
+ // extract properties
+ IntrospectionSupport.getProperties(entry.getValue(), properties, null, false);
+
+ try {
+ HystrixConfigurationDefinition definition = new HystrixConfigurationDefinition();
+ IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), definition, properties);
+
+ // Registry the definition
+ factory.registerSingleton(entry.getKey(), definition);
+
+ } catch (Exception e) {
+ throw new BeanCreationException(entry.getKey(), e);
+ }
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/5889715e/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixConfiguration.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixConfiguration.java b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixConfiguration.java
index 063f94a..deb402c 100644
--- a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixConfiguration.java
@@ -16,6 +16,9 @@
*/
package org.apache.camel.component.hystrix.springboot;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.camel.model.HystrixConfigurationCommon;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -24,4 +27,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
*/
@ConfigurationProperties(prefix = "camel.hystrix")
public class HystrixConfiguration extends HystrixConfigurationCommon {
+ private Map<String, HystrixConfigurationCommon> configurations = new HashMap<>();
+
+ public Map<String, HystrixConfigurationCommon> getConfigurations() {
+ return configurations;
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/5889715e/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/processor/HystrixMultiConfiguration.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/processor/HystrixMultiConfiguration.java b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/processor/HystrixMultiConfiguration.java
new file mode 100644
index 0000000..ecb8a8b
--- /dev/null
+++ b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/processor/HystrixMultiConfiguration.java
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.component.hystrix.processor;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.HystrixConfigurationDefinition;
+import org.springframework.context.annotation.Bean;
+
+public class HystrixMultiConfiguration {
+ @Bean(name = "bean-conf")
+ public HystrixConfigurationDefinition hystrixBeanConfiguration() {
+ return new HystrixConfigurationDefinition()
+ .groupKey("bean-group");
+ }
+
+ @Bean
+ public RouteBuilder routeBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start-1")
+ .routeId("hystrix-route-1")
+ .hystrix()
+ .hystrixConfiguration("conf-1")
+ .to("direct:foo")
+ .onFallback()
+ .transform().constant("Fallback message")
+ .end();
+ from("direct:start-2")
+ .routeId("hystrix-route-2")
+ .hystrix()
+ .hystrixConfiguration("conf-2")
+ .to("direct:foo")
+ .onFallback()
+ .transform().constant("Fallback message")
+ .end();
+
+ from("direct:foo")
+ .transform().body(b -> "Bye World");
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/5889715e/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/processor/HystrixMultiConfigurationTest.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/processor/HystrixMultiConfigurationTest.java b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/processor/HystrixMultiConfigurationTest.java
new file mode 100644
index 0000000..9bcdb61
--- /dev/null
+++ b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/processor/HystrixMultiConfigurationTest.java
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.component.hystrix.processor;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Navigate;
+import org.apache.camel.Processor;
+import org.apache.camel.Route;
+import org.apache.camel.model.HystrixConfigurationDefinition;
+import org.apache.camel.model.HystrixDefinition;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.ApplicationContext;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringRunner;
+
+/**
+ * Testing the Hystrix multi configuration
+ */
+@RunWith(SpringRunner.class)
+@SpringBootApplication
+@DirtiesContext
+@ContextConfiguration(classes = HystrixMultiConfiguration.class)
+@SpringBootTest(properties = {
+ "debug=false",
+ "camel.hystrix.enabled=true",
+ "camel.hystrix.group-key=global-group",
+ "camel.hystrix.configurations.conf-1.group-key=conf-1-group",
+ "camel.hystrix.configurations.conf-2.group-key=conf-2-group"
+})
+public class HystrixMultiConfigurationTest {
+ @Autowired
+ private ApplicationContext context;
+ @Autowired
+ private CamelContext camelContext;
+
+ @Test
+ public void testBeans() throws Exception {
+ Map<String, HystrixConfigurationDefinition> beans = context.getBeansOfType(HystrixConfigurationDefinition.class);
+
+ Assert.assertEquals(4, beans.size());
+ Assert.assertEquals("global-group", beans.get(HystrixConstants.DEFAULT_HYSTRIX_CONFIGURATION_ID).getGroupKey());
+ Assert.assertEquals("bean-group", beans.get("bean-conf").getGroupKey());
+ Assert.assertEquals("conf-1-group", beans.get("conf-1").getGroupKey());
+ Assert.assertEquals("conf-2-group", beans.get("conf-2").getGroupKey());
+ }
+
+ @Test
+ public void testConfigurations() throws Exception {
+ HystrixProcessor processor1 = findHystrixProcessor(camelContext.getRoute("hystrix-route-1").navigate());
+ HystrixProcessor processor2 = findHystrixProcessor(camelContext.getRoute("hystrix-route-2").navigate());
+
+ Assert.assertEquals("conf-1-group", processor1.getHystrixGroupKey());
+ Assert.assertEquals("conf-2-group", processor2.getHystrixGroupKey());
+ }
+
+ // **********************************************
+ // Helper
+ // **********************************************
+
+ private HystrixProcessor findHystrixProcessor(Navigate<Processor> navigate) throws Exception {
+ for (Processor processor : navigate.next()) {
+ if (processor instanceof HystrixProcessor) {
+ return (HystrixProcessor)processor;
+ }
+ if (processor instanceof Navigate) {
+ return findHystrixProcessor((Navigate<Processor>) processor);
+ }
+ }
+
+ throw new IllegalStateException("Unable to find an HystrixProcessor instance");
+ }
+}
+