You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/08/30 09:21:04 UTC
camel git commit: camel-cdi - Add support for injecting
FluentProducerTemplate
Repository: camel
Updated Branches:
refs/heads/master 0d14485d0 -> 1c70401e9
camel-cdi - Add support for injecting FluentProducerTemplate
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1c70401e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1c70401e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1c70401e
Branch: refs/heads/master
Commit: 1c70401e99669482231571d826caebf32ad853ed
Parents: 0d14485
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Aug 30 11:20:55 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Aug 30 11:20:55 2016 +0200
----------------------------------------------------------------------
components/camel-cdi/src/main/docs/cdi.adoc | 19 ++++-
.../org/apache/camel/cdi/CdiCamelExtension.java | 5 ++
.../org/apache/camel/cdi/CdiCamelFactory.java | 33 +++++++++
.../cdi/bean/FluentProduceTemplateBean.java | 30 ++++++++
.../cdi/test/FluentProduceTemplateTest.java | 76 ++++++++++++++++++++
5 files changed, 162 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/1c70401e/components/camel-cdi/src/main/docs/cdi.adoc
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/docs/cdi.adoc b/components/camel-cdi/src/main/docs/cdi.adoc
index 0a68714..05832d1 100644
--- a/components/camel-cdi/src/main/docs/cdi.adoc
+++ b/components/camel-cdi/src/main/docs/cdi.adoc
@@ -14,7 +14,7 @@ The Camel CDI component provides auto-configuration for Apache Camel
using CDI as dependency injection framework based
on�_convention-over-configuration_. It auto-detects Camel routes
available in the application and provides beans for common Camel
-primitives like `Endpoint`,�`ProducerTemplate` or�`TypeConverter`. It
+primitives like `Endpoint`,�`FluentProducerTemplate`, `ProducerTemplate` or�`TypeConverter`. It
implements standard link:bean-integration.html[Camel bean integration]
so that Camel annotations like�`@Consume`,�`@Produce`
and�`@PropertyInject` can be used seamlessly in CDI beans. Besides, it
@@ -92,6 +92,10 @@ injected in any CDI beans, e.g.:
ProducerTemplate producerTemplate;
@Inject
+@Uri("direct:inbound")
+FluentProducerTemplate fluentProducerTemplate;
+
+@Inject
MockEndpoint outbound; // URI defaults to the member name, i.e. mock:outbound
@Inject
@@ -286,6 +290,11 @@ bind the corresponding Camel primitives, e.g.:
ProducerTemplate producerTemplate;
@Inject
+@ContextName("foo")
+@Uri("direct:inbound")
+FluentProducerTemplate fluentProducerTemplate;
+
+@Inject
@BarContextQualifier
MockEndpoint outbound; // URI defaults to the member name, i.e. mock:outbound
@@ -412,6 +421,10 @@ See link:cdi.html[configuration properties] for more details.
----
@Produce(uri = "mock:outbound")
ProducerTemplate producer;
+
+// or using fluent template
+@Produce(uri = "mock:outbound")
+FluentProducerTemplate producer;
----
a|
@@ -420,6 +433,10 @@ ProducerTemplate producer;
@Inject
@Uri("direct:outbound")
ProducerTemplate producer;
+
+// or using fluent template
+@Produce(uri = "direct:outbound")
+FluentProducerTemplate producer;
----
|Endpoint injection (default Camel context) a|
http://git-wip-us.apache.org/repos/asf/camel/blob/1c70401e/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 4803ef3..f4bb9bb 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
@@ -66,6 +66,7 @@ import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Converter;
import org.apache.camel.Endpoint;
import org.apache.camel.EndpointInject;
+import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.PropertyInject;
@@ -179,6 +180,10 @@ public class CdiCamelExtension implements Extension {
producerBeans.put(ppm.getAnnotatedProducerMethod().getJavaMember(), ppm.getBean());
}
+ private void fluentProducerTemplateBeans(@Observes ProcessProducerMethod<FluentProducerTemplate, CdiCamelFactory> ppm) {
+ producerBeans.put(ppm.getAnnotatedProducerMethod().getJavaMember(), ppm.getBean());
+ }
+
private void camelFactoryProducers(@Observes ProcessAnnotatedType<CdiCamelFactory> pat, BeanManager manager) {
pat.setAnnotatedType(
new AnnotatedTypeDelegate<>(
http://git-wip-us.apache.org/repos/asf/camel/blob/1c70401e/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 b0e87aa..8c4c0f3 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
@@ -36,6 +36,7 @@ import javax.enterprise.inject.spi.InjectionPoint;
import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Endpoint;
+import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.TypeConverter;
import org.apache.camel.component.mock.MockEndpoint;
@@ -90,6 +91,38 @@ final class CdiCamelFactory {
}
@Produces
+ @Default @Uri("")
+ // Qualifiers are dynamically added in CdiCamelExtension
+ private static FluentProducerTemplate fluentProducerTemplate(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension) {
+ return getQualifierByType(ip, Uri.class)
+ .map(uri -> fluentProducerTemplateFromUri(ip, instance, extension, uri))
+ .orElseGet(() -> defaultFluentProducerTemplate(ip, instance, extension));
+ }
+
+ private static FluentProducerTemplate fluentProducerTemplateFromUri(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension, Uri uri) {
+ try {
+ CamelContext context = uri.context().isEmpty()
+ ? selectContext(ip, instance, extension)
+ : selectContext(uri.context(), instance);
+ FluentProducerTemplate producerTemplate = context.createFluentProducerTemplate();
+ Endpoint endpoint = context.getEndpoint(uri.value(), Endpoint.class);
+ producerTemplate.setDefaultEndpoint(endpoint);
+ return producerTemplate;
+ } catch (Exception cause) {
+ throw new InjectionException("Error injecting fluent producer template annotated with " + uri + " into " + ip, cause);
+ }
+ }
+
+ private static FluentProducerTemplate defaultFluentProducerTemplate(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension) {
+ try {
+ CamelContext context = selectContext(ip, instance, extension);
+ return context.createFluentProducerTemplate();
+ } catch (Exception cause) {
+ throw new InjectionException("Error injecting fluent producer template into " + ip, cause);
+ }
+ }
+
+ @Produces
@Typed(MockEndpoint.class)
// Qualifiers are dynamically added in CdiCamelExtension
private static MockEndpoint mockEndpointFromMember(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension) {
http://git-wip-us.apache.org/repos/asf/camel/blob/1c70401e/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/FluentProduceTemplateBean.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/FluentProduceTemplateBean.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/FluentProduceTemplateBean.java
new file mode 100644
index 0000000..929a669
--- /dev/null
+++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/FluentProduceTemplateBean.java
@@ -0,0 +1,30 @@
+/**
+ * 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.bean;
+
+import org.apache.camel.FluentProducerTemplate;
+import org.apache.camel.Produce;
+
+public class FluentProduceTemplateBean {
+
+ @Produce(uri = "mock:outbound")
+ private FluentProducerTemplate producer;
+
+ public void sendToProducer(String body) {
+ producer.withBody(body + "-processed").send();
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/1c70401e/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/FluentProduceTemplateTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/FluentProduceTemplateTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/FluentProduceTemplateTest.java
new file mode 100644
index 0000000..4864c57
--- /dev/null
+++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/FluentProduceTemplateTest.java
@@ -0,0 +1,76 @@
+/**
+ * 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 org.apache.camel.CamelContext;
+import org.apache.camel.FluentProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.cdi.CdiCamelExtension;
+import org.apache.camel.cdi.Uri;
+import org.apache.camel.cdi.bean.FluentProduceTemplateBean;
+import org.apache.camel.component.mock.MockEndpoint;
+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.apache.camel.component.mock.MockEndpoint.assertIsSatisfied;
+
+@RunWith(Arquillian.class)
+public class FluentProduceTemplateTest {
+
+ @Deployment
+ public static Archive<?> deployment() {
+ return ShrinkWrap.create(JavaArchive.class)
+ // Camel CDI
+ .addPackage(CdiCamelExtension.class.getPackage())
+ // Test class
+ .addClass(FluentProduceTemplateBean.class)
+ // Bean archive deployment descriptor
+ .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+ }
+
+ @Test
+ @InSequence(1)
+ public void configureCamelContext(CamelContext context) throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:inbound").bean(FluentProduceTemplateBean.class);
+ }
+ });
+ }
+
+ @Test
+ @InSequence(2)
+ public void sendMessageToInbound(@Uri("direct:inbound") FluentProducerTemplate in,
+ @Uri("mock:outbound") MockEndpoint out) throws InterruptedException {
+ out.expectedMessageCount(1);
+ out.expectedBodiesReceived("test-processed");
+
+ in.withBody("test").send();
+
+ assertIsSatisfied(2L, TimeUnit.SECONDS, out);
+ }
+}