camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/3] camel git commit: CAMEL-10747: Pojo @Consume should have CamelContext injected/started its lifecycle. Thanks to Christopher Harris for reporting and the patch.
Date Sun, 29 Jan 2017 12:21:02 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.17.x 00be849dc -> a46b89019
  refs/heads/camel-2.18.x 2cec6e2a4 -> 26d773002
  refs/heads/master 9211cba50 -> c9d95e211


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/c9d95e21
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c9d95e21
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c9d95e21

Branch: refs/heads/master
Commit: c9d95e211bd3181949bfa13517848168c723121c
Parents: 9211cba
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sun Jan 29 13:04:58 2017 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun Jan 29 13:04:58 2017 +0100

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


http://git-wip-us.apache.org/repos/asf/camel/blob/c9d95e21/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 9bee353..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,7 +16,9 @@
  */
 package org.apache.camel.spring;
 
+import java.util.LinkedHashSet;
 import java.util.Map;
+import java.util.Set;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlRootElement;
@@ -24,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;
@@ -47,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;
@@ -107,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
@@ -116,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;
@@ -125,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);
@@ -137,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);
@@ -152,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/c9d95e21/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/c9d95e21/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/c9d95e21/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>


Mime
View raw message