Repository: camel Updated Branches: refs/heads/camel-2.15.x b657207da -> ff554c053
[CAMEL-8532] Spring Boot applications should block the main thread of the execution. (cherry picked from commit 472903b) Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ff554c05 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ff554c05 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ff554c05 Branch: refs/heads/camel-2.15.x Commit: ff554c053f9532d57037066564c029f8a6747c0d Parents: b657207 Author: Henryk Konsek <hekon...@gmail.com> Authored: Mon Mar 23 11:04:30 2015 +0100 Committer: Henryk Konsek <hekon...@gmail.com> Committed: Thu Apr 16 09:12:45 2015 +0200 ---------------------------------------------------------------------- .../spring/boot/CamelAutoConfiguration.java | 5 ++ .../CamelSpringBootApplicationController.java | 54 ++++++++++++++++++++ .../apache/camel/spring/boot/FatJarRouter.java | 6 ++- .../StandaloneFatJarRouterTest.java | 7 ++- 4 files changed, 70 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ff554c05/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 aba9623..dc9abc8 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 @@ -60,6 +60,11 @@ public class CamelAutoConfiguration { } @Bean + CamelSpringBootApplicationController applicationController(ApplicationContext applicationContext, CamelContext camelContext) { + return new CamelSpringBootApplicationController(applicationContext, camelContext); + } + + @Bean @ConditionalOnMissingBean(RoutesCollector.class) RoutesCollector routesCollector(ApplicationContext applicationContext) { Collection<CamelContextConfiguration> configurations = applicationContext.getBeansOfType(CamelContextConfiguration.class).values(); http://git-wip-us.apache.org/repos/asf/camel/blob/ff554c05/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationController.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationController.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationController.java new file mode 100644 index 0000000..7ff3714 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationController.java @@ -0,0 +1,54 @@ +/** + * 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 java.util.Collections; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.main.MainSupport; +import org.springframework.context.ApplicationContext; + +public class CamelSpringBootApplicationController { + + private final MainSupport mainSupport; + + public CamelSpringBootApplicationController(final ApplicationContext applicationContext, final CamelContext camelContext) { + this.mainSupport = new MainSupport() { + @Override + protected ProducerTemplate findOrCreateCamelTemplate() { + return applicationContext.getBean(ProducerTemplate.class); + } + + @Override + protected Map<String, CamelContext> getCamelContextMap() { + return Collections.singletonMap("camelContext", camelContext); + } + }; + } + + public void blockMainThread() { + try { + mainSupport.enableHangupSupport(); + mainSupport.run(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/ff554c05/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/FatJarRouter.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/FatJarRouter.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/FatJarRouter.java index d712bb0..94458ef 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/FatJarRouter.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/FatJarRouter.java @@ -18,11 +18,15 @@ package org.apache.camel.spring.boot; import org.apache.camel.builder.RouteBuilder; import org.springframework.boot.SpringApplication; +import org.springframework.context.ApplicationContext; public class FatJarRouter extends RouteBuilder { public static void main(String... args) { - new SpringApplication(FatJarRouter.class).run(args); + ApplicationContext applicationContext = new SpringApplication(FatJarRouter.class).run(args); + CamelSpringBootApplicationController applicationController = + applicationContext.getBean(CamelSpringBootApplicationController.class); + applicationController.blockMainThread(); } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/ff554c05/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/fatjarroutertests/StandaloneFatJarRouterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/fatjarroutertests/StandaloneFatJarRouterTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/fatjarroutertests/StandaloneFatJarRouterTest.java index af8916b..2d194a7 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/fatjarroutertests/StandaloneFatJarRouterTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/fatjarroutertests/StandaloneFatJarRouterTest.java @@ -37,7 +37,12 @@ public class StandaloneFatJarRouterTest extends Assert { // Given final int port = SocketUtils.findAvailableTcpPort(); final URL httpEndpoint = new URL("http://localhost:" + port); - TestFatJarRouter.main("--spring.main.sources=org.apache.camel.spring.boot.fatjarroutertests.TestFatJarRouter", "--http.port=" + port); + new Thread() { + @Override + public void run() { + TestFatJarRouter.main("--spring.main.sources=org.apache.camel.spring.boot.fatjarroutertests.TestFatJarRouter", "--http.port=" + port); + } + }.start(); await().atMost(1, MINUTES).until(new Callable<Boolean>() { @Override public Boolean call() throws Exception {