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 2017/01/29 12:21:03 UTC

[2/3] camel git commit: CAMEL-10747: Pojo @Consume should have CamelContext injected/started its lifecycle. Thanks to Christopher Harris for reporting and the patch.

CAMEL-10747: Pojo @Consume should have CamelContext injected/started its lifecycle. Thanks to Christopher Harris for reporting and the patch.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/26d77300
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/26d77300
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/26d77300

Branch: refs/heads/camel-2.18.x
Commit: 26d773002e8f78db165273ad386ea59844dcc11f
Parents: 2cec6e2
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Jan 29 13:04:58 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Jan 29 13:18:38 2017 +0100

----------------------------------------------------------------------
 .../camel/spring/CamelBeanPostProcessor.java    | 24 +++++++++
 .../camel/spring/example/MyVmConsumer.java      | 55 ++++++++++++++++++++
 .../spring/example/PojoVmConsumerTest.java      | 50 ++++++++++++++++++
 .../camel/spring/example/pojoVmConsumer.xml     | 31 +++++++++++
 4 files changed, 160 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/26d77300/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
index f4c2e61..b444a0c 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.spring;
 
+import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
 import javax.xml.bind.annotation.XmlAccessType;
@@ -25,10 +26,12 @@ import javax.xml.bind.annotation.XmlTransient;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
+import org.apache.camel.Service;
 import org.apache.camel.core.xml.CamelJMXAgentDefinition;
 import org.apache.camel.impl.CamelPostProcessorHelper;
 import org.apache.camel.impl.DefaultCamelBeanPostProcessor;
 import org.apache.camel.spi.Metadata;
+import org.apache.camel.util.ServiceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanInstantiationException;
@@ -48,6 +51,8 @@ import org.springframework.context.ApplicationContextAware;
 public class CamelBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
     private static final Logger LOG = LoggerFactory.getLogger(CamelBeanPostProcessor.class);
     @XmlTransient
+    Set<String> prototypeBeans = new LinkedHashSet<>();
+    @XmlTransient
     private CamelContext camelContext;
     @XmlTransient
     private ApplicationContext applicationContext;
@@ -108,6 +113,7 @@ public class CamelBeanPostProcessor implements BeanPostProcessor, ApplicationCon
                         return new BeanInstantiationException(type, "Could not instantiate proxy of type " + type.getName() + " on endpoint " + endpoint, e);
                     }
 
+                    @Override
                     protected boolean isSingleton(Object bean, String beanName) {
                         // no application context has been injected which means the bean
                         // has not been enlisted in Spring application context
@@ -117,6 +123,21 @@ public class CamelBeanPostProcessor implements BeanPostProcessor, ApplicationCon
                             return applicationContext.isSingleton(beanName);
                         }
                     }
+
+                    @Override
+                    protected void startService(Service service, CamelContext context, Object bean, String beanName) throws Exception {
+                        if (isSingleton(bean, beanName)) {
+                            getCamelContext().addService(service);
+                        } else {
+                            // only start service and do not add it to CamelContext
+                            ServiceHelper.startService(service);
+                            if (prototypeBeans.add(beanName)) {
+                                // do not spam the log with WARN so do this only once per bean name
+                                CamelBeanPostProcessor.LOG.warn("The bean with id [" + beanName + "] is prototype scoped and cannot stop the injected service when bean is destroyed: "
+                                    + service + ". You may want to stop the service manually from the bean.");
+                            }
+                        }
+                    }
                 };
             }
             return camelPostProcessorHelper;
@@ -126,6 +147,7 @@ public class CamelBeanPostProcessor implements BeanPostProcessor, ApplicationCon
     public CamelBeanPostProcessor() {
     }
 
+    @Override
     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
         try {
             return delegate.postProcessBeforeInitialization(bean, beanName);
@@ -138,6 +160,7 @@ public class CamelBeanPostProcessor implements BeanPostProcessor, ApplicationCon
         }
     }
 
+    @Override
     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
         try {
             return delegate.postProcessAfterInitialization(bean, beanName);
@@ -153,6 +176,7 @@ public class CamelBeanPostProcessor implements BeanPostProcessor, ApplicationCon
     // Properties
     // -------------------------------------------------------------------------
 
+    @Override
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
         this.applicationContext = applicationContext;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/26d77300/components/camel-spring/src/test/java/org/apache/camel/spring/example/MyVmConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/example/MyVmConsumer.java b/components/camel-spring/src/test/java/org/apache/camel/spring/example/MyVmConsumer.java
new file mode 100644
index 0000000..57b4e80
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/example/MyVmConsumer.java
@@ -0,0 +1,55 @@
+/**
+ * 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.example;
+
+import org.apache.camel.Consume;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An example POJO which is injected with a CamelTemplate
+ *
+ * @version 
+ */
+public class MyVmConsumer {
+    private static final Logger LOG = LoggerFactory.getLogger(MyVmConsumer.class);
+    @EndpointInject(uri = "mock:result")
+    private ProducerTemplate destination;
+
+    @Consume(uri = "vm:start")
+    public void doSomething(String body, Exchange exchange) {
+        ObjectHelper.notNull(destination, "destination");
+
+        ObjectHelper.notNull(exchange, "exchange");
+        ObjectHelper.notNull(exchange.getContext(), "exchange.getContext");
+
+        LOG.info("Received body: " + body);
+        destination.sendBody(body);
+    }
+
+    public ProducerTemplate getDestination() {
+        return destination;
+    }
+
+    public void setDestination(ProducerTemplate destination) {
+        this.destination = destination;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/26d77300/components/camel-spring/src/test/java/org/apache/camel/spring/example/PojoVmConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/example/PojoVmConsumerTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/example/PojoVmConsumerTest.java
new file mode 100644
index 0000000..48acbee
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/example/PojoVmConsumerTest.java
@@ -0,0 +1,50 @@
+/**
+ * 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.example;
+
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version 
+ */
+public class PojoVmConsumerTest extends SpringTestSupport {
+    protected MockEndpoint resultEndpoint;
+
+    public void testMessagesSentToConsumerArrive() throws Exception {
+        String body = "<hello>world!</hello>";
+        resultEndpoint.expectedBodiesReceived(body);
+
+        template.sendBody("vm:start", body);
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        resultEndpoint = getMockEndpoint("mock:result");
+    }
+
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/spring/example/pojoVmConsumer.xml");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/26d77300/components/camel-spring/src/test/resources/org/apache/camel/spring/example/pojoVmConsumer.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/example/pojoVmConsumer.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/example/pojoVmConsumer.xml
new file mode 100644
index 0000000..4e27cc7
--- /dev/null
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/example/pojoVmConsumer.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+  <!-- START SNIPPET: example -->
+  <camelContext xmlns="http://camel.apache.org/schema/spring">
+  </camelContext>
+  <!-- END SNIPPET: example -->
+
+  <bean id="myVmConsumer" class="org.apache.camel.spring.example.MyVmConsumer"/>
+</beans>