Repository: camel Updated Branches: refs/heads/master 36d250532 -> 5c5c3a5b3
CAMEL-9811: Add support for consumer template injection Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5c5c3a5b Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5c5c3a5b Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5c5c3a5b Branch: refs/heads/master Commit: 5c5c3a5b3ce162a67dee0fddccf9f27af65d1726 Parents: 36d2505 Author: Antonin Stefanutti <[email protected]> Authored: Mon Apr 4 14:05:24 2016 +0200 Committer: Antonin Stefanutti <[email protected]> Committed: Mon Apr 4 14:05:55 2016 +0200 ---------------------------------------------------------------------- .../org/apache/camel/cdi/CdiCamelExtension.java | 13 +- .../org/apache/camel/cdi/CdiCamelFactory.java | 7 ++ .../camel/cdi/test/ConsumerTemplateTest.java | 64 ++++++++++ .../test/MultiContextConsumerTemplateTest.java | 123 +++++++++++++++++++ 4 files changed, 205 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/5c5c3a5b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelExtension.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelExtension.java b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelExtension.java index 5d79f46..19e3eba 100755 --- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelExtension.java +++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelExtension.java @@ -55,6 +55,7 @@ import org.apache.camel.BeanInject; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.Consume; +import org.apache.camel.ConsumerTemplate; import org.apache.camel.Converter; import org.apache.camel.Endpoint; import org.apache.camel.EndpointInject; @@ -157,6 +158,10 @@ public class CdiCamelExtension implements Extension { producerBeans.put(ppm.getAnnotatedProducerMethod().getJavaMember(), ppm.getBean()); } + private void consumerTemplateBeans(@Observes ProcessProducerMethod<ConsumerTemplate, CdiCamelFactory> ppm) { + producerBeans.put(ppm.getAnnotatedProducerMethod().getJavaMember(), ppm.getBean()); + } + private void producerTemplateBeans(@Observes ProcessProducerMethod<ProducerTemplate, CdiCamelFactory> ppm) { producerBeans.put(ppm.getAnnotatedProducerMethod().getJavaMember(), ppm.getBean()); } @@ -169,7 +174,9 @@ public class CdiCamelExtension implements Extension { continue; } Class<?> type = CdiSpiHelper.getRawType(am.getBaseType()); - if (Endpoint.class.isAssignableFrom(type) || ProducerTemplate.class.equals(type)) { + if (Endpoint.class.isAssignableFrom(type) + || ConsumerTemplate.class.equals(type) + || ProducerTemplate.class.equals(type)) { Set<Annotation> qualifiers = CdiSpiHelper.getQualifiers(am, manager); producerQualifiers.put(am.getJavaMember(), qualifiers); Set<Annotation> annotations = new HashSet<>(am.getAnnotations()); @@ -237,7 +244,9 @@ public class CdiCamelExtension implements Extension { qualifiers.addAll(ip.getQualifiers()); } } else { - if (Endpoint.class.isAssignableFrom(type) || ProducerTemplate.class.isAssignableFrom(type)) { + if (Endpoint.class.isAssignableFrom(type) + || ConsumerTemplate.class.equals(type) + || ProducerTemplate.class.equals(type)) { qualifiers.addAll(CdiSpiHelper.excludeElementOfTypes(contextQualifiers, Default.class, Named.class)); } } http://git-wip-us.apache.org/repos/asf/camel/blob/5c5c3a5b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelFactory.java b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelFactory.java index 9ee9f84..965da41 100755 --- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelFactory.java +++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelFactory.java @@ -38,6 +38,7 @@ import javax.enterprise.inject.spi.InjectionPoint; import javax.enterprise.util.TypeLiteral; import org.apache.camel.CamelContext; +import org.apache.camel.ConsumerTemplate; import org.apache.camel.Endpoint; import org.apache.camel.ProducerTemplate; import org.apache.camel.TypeConverter; @@ -51,6 +52,12 @@ final class CdiCamelFactory { } @Produces + // Qualifiers are dynamically added in CdiCamelExtension + private static ConsumerTemplate consumerTemplate(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension) { + return selectContext(ip, instance, extension).createConsumerTemplate(); + } + + @Produces @Default @Uri("") // Qualifiers are dynamically added in CdiCamelExtension private static ProducerTemplate producerTemplate(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension) { http://git-wip-us.apache.org/repos/asf/camel/blob/5c5c3a5b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ConsumerTemplateTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ConsumerTemplateTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ConsumerTemplateTest.java new file mode 100644 index 0000000..98e0c10 --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ConsumerTemplateTest.java @@ -0,0 +1,64 @@ +/** + * 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.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import org.apache.camel.ConsumerTemplate; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.cdi.CdiCamelExtension; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class ConsumerTemplateTest { + + @Inject + private ProducerTemplate producer; + + @Inject + private ConsumerTemplate consumer; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void sendThenReceiveBody() { + producer.sendBody("seda:foo", "test"); + + String body = consumer.receiveBody("seda:foo", TimeUnit.SECONDS.toMillis(1L), String.class); + + assertThat("Body is incorrect!", body, is(equalTo("test"))); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5c5c3a5b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/MultiContextConsumerTemplateTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/MultiContextConsumerTemplateTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/MultiContextConsumerTemplateTest.java new file mode 100644 index 0000000..e5940ea --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/MultiContextConsumerTemplateTest.java @@ -0,0 +1,123 @@ +/** + * 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.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import org.apache.camel.CamelContext; +import org.apache.camel.ConsumerTemplate; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.ContextName; +import org.apache.camel.cdi.bean.DefaultCamelContextBean; +import org.apache.camel.cdi.bean.FirstCamelContextBean; +import org.apache.camel.cdi.bean.SecondCamelContextBean; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.junit.InSequence; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class MultiContextConsumerTemplateTest { + + @Inject + private CamelContext defaultCamelContext; + + @Inject + private ConsumerTemplate defaultConsumer; + + @Inject + private ProducerTemplate defaultProducer; + + @Inject @ContextName("first") + private CamelContext firstCamelContext; + + @Inject @ContextName("first") + private ConsumerTemplate firstConsumer; + + @Inject @ContextName("first") + private ProducerTemplate firstProducer; + + @Inject @ContextName("second") + private CamelContext secondCamelContext; + + @Inject @ContextName("second") + private ConsumerTemplate secondConsumer; + + @Inject @ContextName("second") + private ProducerTemplate secondProducer; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test classes + .addClasses( + DefaultCamelContextBean.class, + FirstCamelContextBean.class, + SecondCamelContextBean.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + @InSequence(1) + public void configureCamelContexts() throws Exception { + secondCamelContext.startAllRoutes(); + } + + @Test + @InSequence(2) + public void receiveBodyFromDefaultCamelContext() { + defaultProducer.sendBody("seda:foo", "foo"); + + String body = defaultConsumer.receiveBody("seda:foo", TimeUnit.SECONDS.toMillis(1L), String.class); + + assertThat("Body is incorrect!", body, is(equalTo("foo"))); + } + + @Test + @InSequence(3) + public void receiveBodyFromFirstCamelContext() { + firstProducer.sendBody("seda:bar", "bar"); + + String body = firstConsumer.receiveBody("seda:bar", TimeUnit.SECONDS.toMillis(1L), String.class); + + assertThat("Body is incorrect!", body, is(equalTo("bar"))); + } + + @Test + @InSequence(4) + public void receiveBodyFromSecondCamelContext() { + secondProducer.sendBody("seda:baz", "baz"); + + String body = secondConsumer.receiveBody("seda:baz", TimeUnit.SECONDS.toMillis(1L), String.class); + + assertThat("Body is incorrect!", body, is(equalTo("baz"))); + } +}
