Repository: camel Updated Branches: refs/heads/master cc8b44a12 -> 38c538020
CAMEL-11752: Using XML routes with Spring Boot now supports spring auto configuration so you can configure in application.properties but still define Camel in XML file. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/38c53802 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/38c53802 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/38c53802 Branch: refs/heads/master Commit: 38c538020e4e84d9269de0bc270a3552399b25aa Parents: cc8b44a Author: Claus Ibsen <[email protected]> Authored: Wed Sep 6 20:20:46 2017 +0200 Committer: Claus Ibsen <[email protected]> Committed: Wed Sep 6 20:20:46 2017 +0200 ---------------------------------------------------------------------- .../spring/boot/CamelAutoConfiguration.java | 25 ++++++++--- .../SpringBootXmlCamelContextConfigurer.java | 47 ++++++++++++++++++++ .../boot/MixedBootAndXmlConfigurationTest.java | 9 ++-- .../camel/spring/CamelContextFactoryBean.java | 17 ++++++- .../spring/spi/XmlCamelContextConfigurer.java | 37 +++++++++++++++ 5 files changed, 125 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/38c53802/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java index 7a1307fd..2f99fca 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java @@ -56,6 +56,7 @@ import org.apache.camel.spi.ThreadPoolProfile; import org.apache.camel.spi.UnitOfWorkFactory; import org.apache.camel.spring.CamelBeanPostProcessor; import org.apache.camel.spring.SpringCamelContext; +import org.apache.camel.spring.spi.XmlCamelContextConfigurer; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.jsse.GlobalSSLContextParametersSupplier; import org.slf4j.Logger; @@ -79,6 +80,15 @@ public class CamelAutoConfiguration { private static final Logger LOG = LoggerFactory.getLogger(CamelAutoConfiguration.class); /** + * Allows to do custom configuration when running XML based Camel in Spring Boot + */ + // must be named xmlCamelContextConfigurer + @Bean(name = "xmlCamelContextConfigurer") + XmlCamelContextConfigurer springBootCamelContextConfigurer() { + return new SpringBootXmlCamelContextConfigurer(); + } + + /** * Spring-aware Camel context for the application. Auto-detects and loads all routes available in the Spring context. */ // We explicitly declare the destroyMethod to be "" as the Spring @Bean @@ -91,6 +101,13 @@ public class CamelAutoConfiguration { @ConditionalOnMissingBean(CamelContext.class) CamelContext camelContext(ApplicationContext applicationContext, CamelConfigurationProperties config) throws Exception { + CamelContext camelContext = new SpringCamelContext(applicationContext); + return doConfigureCamelContext(applicationContext, camelContext, config); + } + + static CamelContext doConfigureCamelContext(ApplicationContext applicationContext, + CamelContext camelContext, + CamelConfigurationProperties config) throws Exception { if (ObjectHelper.isNotEmpty(config.getFileConfigurations())) { Environment env = applicationContext.getEnvironment(); @@ -104,8 +121,6 @@ public class CamelAutoConfiguration { } } - CamelContext camelContext = new SpringCamelContext(applicationContext); - if (!config.isJmxEnabled()) { camelContext.disableJMX(); } @@ -324,7 +339,7 @@ public class CamelAutoConfiguration { * <p/> * Similar code in camel-core-xml module in class org.apache.camel.core.xml.AbstractCamelContextFactoryBean. */ - void afterPropertiesSet(ApplicationContext applicationContext, CamelContext camelContext) throws Exception { + static void afterPropertiesSet(ApplicationContext applicationContext, CamelContext camelContext) throws Exception { Tracer tracer = getSingleBeanOfType(applicationContext, Tracer.class); if (tracer != null) { // use formatter if there is a TraceFormatter bean defined @@ -472,7 +487,7 @@ public class CamelAutoConfiguration { initThreadPoolProfiles(applicationContext, camelContext); } - private void initThreadPoolProfiles(ApplicationContext applicationContext, CamelContext camelContext) { + private static void initThreadPoolProfiles(ApplicationContext applicationContext, CamelContext camelContext) { Set<String> defaultIds = new HashSet<String>(); // lookup and use custom profiles from the registry @@ -497,7 +512,7 @@ public class CamelAutoConfiguration { } } - private <T> T getSingleBeanOfType(ApplicationContext applicationContext, Class<T> type) { + private static <T> T getSingleBeanOfType(ApplicationContext applicationContext, Class<T> type) { Map<String, T> beans = applicationContext.getBeansOfType(type); if (beans.size() == 1) { return beans.values().iterator().next(); http://git-wip-us.apache.org/repos/asf/camel/blob/38c53802/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootXmlCamelContextConfigurer.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootXmlCamelContextConfigurer.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootXmlCamelContextConfigurer.java new file mode 100644 index 0000000..7350d13 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootXmlCamelContextConfigurer.java @@ -0,0 +1,47 @@ +/** + * 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.spring.boot; + +import org.apache.camel.spring.SpringCamelContext; +import org.apache.camel.spring.spi.XmlCamelContextConfigurer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; + +/** + * Used to merge Camel Spring Boot configuration with {@link org.apache.camel.CamelContext} that + * has been created from XML files. This allows to configure your Camel applications with Spring Boot + * configuration for both Java and XML Camel routes in similar way. + */ +public class SpringBootXmlCamelContextConfigurer implements XmlCamelContextConfigurer { + + private static final Logger LOG = LoggerFactory.getLogger(SpringBootXmlCamelContextConfigurer.class); + + @Override + public void configure(ApplicationContext applicationContext, SpringCamelContext camelContext) { + CamelConfigurationProperties config = applicationContext.getBean(CamelConfigurationProperties.class); + if (config != null) { + try { + LOG.debug("Merging XML based CamelContext with Spring Boot configuration properties"); + CamelAutoConfiguration.doConfigureCamelContext(applicationContext, camelContext, config); + } catch (Exception e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/38c53802/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/MixedBootAndXmlConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/MixedBootAndXmlConfigurationTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/MixedBootAndXmlConfigurationTest.java index 17eda49..6cfdc12 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/MixedBootAndXmlConfigurationTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/MixedBootAndXmlConfigurationTest.java @@ -18,7 +18,6 @@ package org.apache.camel.spring.boot; import org.apache.camel.CamelContext; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -33,17 +32,19 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest( properties = { - "camel.springboot.name = camel-spring-boot" + "camel.springboot.name = camel-spring-boot", + "camel.springboot.shutdownTimeout = 5" } ) public class MixedBootAndXmlConfigurationTest { + @Autowired private CamelContext camel; - @Ignore("See https://issues.apache.org/jira/browse/CAMEL-11752") @Test - public void thereShouldBeTwoRoutesConfigured() { + public void thereShouldBeAutoConfiguredFromSpringBoot() { Assert.assertEquals("camel-spring-boot", camel.getName()); + Assert.assertEquals(5, camel.getShutdownStrategy().getTimeout()); } @Configuration http://git-wip-us.apache.org/repos/asf/camel/blob/38c53802/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java index 331563a..c72f98e 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java @@ -19,7 +19,6 @@ package org.apache.camel.spring; import java.util.ArrayList; import java.util.List; import java.util.Map; - import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; @@ -69,13 +68,16 @@ import org.apache.camel.spi.Metadata; import org.apache.camel.spi.PackageScanFilter; import org.apache.camel.spi.Registry; import org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer; +import org.apache.camel.spring.spi.XmlCamelContextConfigurer; import org.apache.camel.util.CamelContextHelper; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.StopWatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -448,6 +450,19 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Spr SpringCamelContext ctx = newCamelContext(); ctx.setName(getId()); + try { + // allow any custom configuration, such as when running in camel-spring-boot + if (applicationContext.containsBean("xmlCamelContextConfigurer")) { + XmlCamelContextConfigurer configurer = applicationContext.getBean("xmlCamelContextConfigurer", XmlCamelContextConfigurer.class); + if (configurer != null) { + configurer.configure(applicationContext, ctx); + } + } + } catch (Exception e) { + // error during configuration + throw ObjectHelper.wrapRuntimeCamelException(e); + } + return ctx; } http://git-wip-us.apache.org/repos/asf/camel/blob/38c53802/components/camel-spring/src/main/java/org/apache/camel/spring/spi/XmlCamelContextConfigurer.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/XmlCamelContextConfigurer.java b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/XmlCamelContextConfigurer.java new file mode 100644 index 0000000..806a0ea --- /dev/null +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/XmlCamelContextConfigurer.java @@ -0,0 +1,37 @@ +/** + * 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.spring.spi; + +import org.apache.camel.spring.SpringCamelContext; +import org.springframework.context.ApplicationContext; + +/** + * Allows to do custom configuration when a new XML based {@link org.apache.camel.spring.SpringCamelContext} has + * been created. For example we use this to enable camel-spring-boot to configure Camel created + * from XML files with the existing Spring Boot auto configuration. + */ +public interface XmlCamelContextConfigurer { + + /** + * Configures XML based CamelContext with the given configuration + * + * @param applicationContext the Spring context + * @param camelContext the XML based CamelContext + * @throws Exception is thrown if error during configuration + */ + void configure(ApplicationContext applicationContext, SpringCamelContext camelContext) throws Exception; +}
