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 2022/06/29 14:00:08 UTC

[camel] branch main updated: CAMEL-18248: @BeanInject can inject ProducerTemplate/FluentProducerTemplate/ConsumerTemplate

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new f5ca4dbace4 CAMEL-18248: @BeanInject can inject ProducerTemplate/FluentProducerTemplate/ConsumerTemplate
f5ca4dbace4 is described below

commit f5ca4dbace4e43111938119e625c921b046ac3b0
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jun 29 15:59:50 2022 +0200

    CAMEL-18248: @BeanInject can inject ProducerTemplate/FluentProducerTemplate/ConsumerTemplate
---
 .../impl/engine/CamelPostProcessorHelper.java      | 27 ++++++++++
 .../camel/impl/BeanInjectProducerTemplateTest.java | 63 ++++++++++++++++++++++
 2 files changed, 90 insertions(+)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelPostProcessorHelper.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelPostProcessorHelper.java
index 533fb49ec7d..9890302f8f4 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelPostProcessorHelper.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/CamelPostProcessorHelper.java
@@ -312,6 +312,33 @@ public class CamelPostProcessorHelper implements CamelContextAware {
             }
             Set<?> found = getCamelContext() != null ? getCamelContext().getRegistry().findByType(type) : null;
             if (found == null || found.isEmpty()) {
+                // this may be a common type so lets check this first
+                if (getCamelContext() != null && type.isAssignableFrom(Registry.class)) {
+                    return getCamelContext().getRegistry();
+                }
+                if (getCamelContext() != null && type.isAssignableFrom(TypeConverter.class)) {
+                    return getCamelContext().getTypeConverter();
+                }
+                // for templates then create a new instance and let camel manage its lifecycle
+                Service answer = null;
+                if (getCamelContext() != null && type.isAssignableFrom(FluentProducerTemplate.class)) {
+                    answer = getCamelContext().createFluentProducerTemplate();
+                }
+                if (getCamelContext() != null && type.isAssignableFrom(ProducerTemplate.class)) {
+                    answer = getCamelContext().createProducerTemplate();
+                }
+                if (getCamelContext() != null && type.isAssignableFrom(ConsumerTemplate.class)) {
+                    answer = getCamelContext().createConsumerTemplate();
+                }
+                if (answer != null) {
+                    // lets make camel context manage its lifecycle
+                    try {
+                        getCamelContext().addService(answer);
+                    } catch (Exception e) {
+                        throw RuntimeCamelException.wrapRuntimeException(e);
+                    }
+                    return answer;
+                }
                 throw new NoSuchBeanException(name, type.getName());
             } else if (found.size() > 1) {
                 throw new NoSuchBeanException(
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/BeanInjectProducerTemplateTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/BeanInjectProducerTemplateTest.java
new file mode 100644
index 00000000000..9adf34bb73c
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/BeanInjectProducerTemplateTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.impl;
+
+import org.apache.camel.BeanInject;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.FluentProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.CamelBeanPostProcessor;
+import org.junit.jupiter.api.Test;
+
+public class BeanInjectProducerTemplateTest extends ContextTestSupport {
+
+    @BeanInject
+    private FluentProducerTemplate fluent;
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+
+        // manual post process us as ContextTestSupport in camel-core doesn't do
+        // that out of the box
+        CamelBeanPostProcessor post = context.adapt(ExtendedCamelContext.class).getBeanPostProcessor();
+        post.postProcessBeforeInitialization(this, "BeanInjectProducerTemplateTest");
+        post.postProcessAfterInitialization(this, "BeanInjectProducerTemplateTest");
+        return context;
+    }
+
+    @Test
+    public void testBeanInjectTemplate() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+
+        fluent.withBody("Hello World").to("direct:start").send();
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("mock:result");
+            }
+        };
+    }
+}