Repository: camel Updated Branches: refs/heads/camel-2.18.x 73a084567 -> 25216a508
CAMEL-11008: Consumer/Producer templates are no... ...t stopped when auto-configured in Spring Boot Adds `addService` for Consumer/Producer templates when configured from Spring Boot this ensures that these are stopped on CamelContext shutdown Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/25216a50 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/25216a50 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/25216a50 Branch: refs/heads/camel-2.18.x Commit: 25216a508ce740f2041e86a97d216838451f89c0 Parents: 73a0845 Author: Zoran Regvart <zregv...@apache.org> Authored: Mon Mar 13 23:42:12 2017 +0100 Committer: Zoran Regvart <zregv...@apache.org> Committed: Tue Mar 14 13:18:13 2017 +0100 ---------------------------------------------------------------------- .../spring/boot/CamelAutoConfiguration.java | 12 ++-- .../CamelSpringBootTemplateShutdownTest.java | 74 ++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/25216a50/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 579a384..0e3979f 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 @@ -215,8 +215,10 @@ public class CamelAutoConfiguration { // Camel handles the lifecycle of this bean @ConditionalOnMissingBean(ProducerTemplate.class) ProducerTemplate producerTemplate(CamelContext camelContext, - CamelConfigurationProperties config) { - return camelContext.createProducerTemplate(config.getProducerTemplateCacheSize()); + CamelConfigurationProperties config) throws Exception { + final ProducerTemplate producerTemplate = camelContext.createProducerTemplate(config.getProducerTemplateCacheSize()); + camelContext.addService(producerTemplate); + return producerTemplate; } /** @@ -226,8 +228,10 @@ public class CamelAutoConfiguration { // Camel handles the lifecycle of this bean @ConditionalOnMissingBean(ConsumerTemplate.class) ConsumerTemplate consumerTemplate(CamelContext camelContext, - CamelConfigurationProperties config) { - return camelContext.createConsumerTemplate(config.getConsumerTemplateCacheSize()); + CamelConfigurationProperties config) throws Exception { + final ConsumerTemplate consumerTemplate = camelContext.createConsumerTemplate(config.getConsumerTemplateCacheSize()); + camelContext.addService(consumerTemplate); + return consumerTemplate; } // SpringCamelContext integration http://git-wip-us.apache.org/repos/asf/camel/blob/25216a50/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelSpringBootTemplateShutdownTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelSpringBootTemplateShutdownTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelSpringBootTemplateShutdownTest.java new file mode 100644 index 0000000..0b7b6e0 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelSpringBootTemplateShutdownTest.java @@ -0,0 +1,74 @@ +/** + * 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.CamelContext; +import org.apache.camel.ConsumerTemplate; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.support.ServiceSupport; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.AbstractApplicationContext; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class CamelSpringBootTemplateShutdownTest { + + CamelContext camelContext; + + AbstractApplicationContext applicationContext; + + ConsumerTemplate consumerTemplate; + + ProducerTemplate producerTemplate; + + @Before + public void setupApplicationContext() { + applicationContext = new AnnotationConfigApplicationContext(CamelAutoConfiguration.class); + camelContext = applicationContext.getBean(CamelContext.class); + consumerTemplate = applicationContext.getBean(ConsumerTemplate.class); + producerTemplate = applicationContext.getBean(ProducerTemplate.class); + } + + @Test + public void shouldStopTemplatesWithCamelShutdown() throws Exception { + assertTrue(((ServiceSupport) consumerTemplate).isStarted()); + assertTrue(((ServiceSupport) producerTemplate).isStarted()); + + camelContext.stop(); + + assertTrue(((ServiceSupport) camelContext).isStopped()); + assertTrue(((ServiceSupport) consumerTemplate).isStopped()); + assertTrue(((ServiceSupport) producerTemplate).isStopped()); + } + + @Test + public void shouldStopTemplatesWithApplicationContextShutdown() throws Exception { + assertTrue(((ServiceSupport) consumerTemplate).isStarted()); + assertTrue(((ServiceSupport) producerTemplate).isStarted()); + + applicationContext.close(); + + assertFalse(applicationContext.isActive()); + assertTrue(((ServiceSupport) camelContext).isStopped()); + assertTrue(((ServiceSupport) consumerTemplate).isStopped()); + assertTrue(((ServiceSupport) producerTemplate).isStopped()); + } + +} \ No newline at end of file