Author: ningjiang
Date: Fri Dec  2 12:38:22 2011
New Revision: 1209449

URL: http://svn.apache.org/viewvc?rev=1209449&view=rev
Log:
Merged revisions 1209401 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r1209401 | ningjiang | 2011-12-02 18:56:36 +0800 (Fri, 02 Dec 2011) | 1 line
  
  CAMEL-4724 reset the camel-context of the exchange on the seda producer
........

Modified:
    camel/branches/camel-2.8.x/   (props changed)
    
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/seda/SedaConsumer.java
    
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
    
camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/vm/AbstractVmTestSupport.java
    
camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/vm/VmDifferentOptionsOnConsumerAndProducerTest.java

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Dec  2 12:38:22 2011
@@ -1 +1 @@
-/camel/trunk:1202148,1202167,1202204-1202206,1202215,1202223,1202659,1202685,1203879,1203978,1204338,1205124,1205372,1205412,1205429,1205431,1205713,1206116,1206414,1207743,1207784,1208301,1208930,1208964-1208965,1209006-1209007,1209382
+/camel/trunk:1202148,1202167,1202204-1202206,1202215,1202223,1202659,1202685,1203879,1203978,1204338,1205124,1205372,1205412,1205429,1205431,1205713,1206116,1206414,1207743,1207784,1208301,1208930,1208964-1208965,1209006-1209007,1209382,1209401

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/seda/SedaConsumer.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/seda/SedaConsumer.java?rev=1209449&r1=1209448&r2=1209449&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/seda/SedaConsumer.java
 (original)
+++ 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/seda/SedaConsumer.java
 Fri Dec  2 12:38:22 2011
@@ -37,6 +37,7 @@ import org.apache.camel.processor.Multic
 import org.apache.camel.spi.ExceptionHandler;
 import org.apache.camel.spi.ShutdownAware;
 import org.apache.camel.util.AsyncProcessorHelper;
+import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -153,10 +154,20 @@ public class SedaConsumer extends Servic
                 exchange = queue.poll(1000, TimeUnit.MILLISECONDS);
                 if (exchange != null) {
                     try {
-                        sendToConsumers(exchange);
-
+                        // send a new copied exchange with new camel context
+                        Exchange newExchange = 
ExchangeHelper.copyExchangeAndSetCamelContext(exchange, 
endpoint.getCamelContext());
+                        // set the fromEndpoint 
+                        newExchange.setFromEndpoint(endpoint);
+                        sendToConsumers(newExchange);
+                        // copy the message back
+                        if (newExchange.hasOut()) {
+                            exchange.setOut(newExchange.getOut().copy());
+                        } else {
+                            exchange.setIn(newExchange.getIn());
+                        }
                         // log exception if an exception occurred and was not 
handled
-                        if (exchange.getException() != null) {
+                        if (newExchange.getException() != null) {
+                            exchange.setException(newExchange.getException());
                             getExceptionHandler().handleException("Error 
processing exchange", exchange, exchange.getException());
                         }
                     } catch (Exception e) {

Modified: 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java?rev=1209449&r1=1209448&r2=1209449&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
 (original)
+++ 
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
 Fri Dec  2 12:38:22 2011
@@ -18,6 +18,7 @@ package org.apache.camel.util;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
@@ -678,4 +679,27 @@ public final class ExchangeHelper {
             exchange.setOut(null);
         }
     }
+    
+    public static Exchange copyExchangeAndSetCamelContext(Exchange exchange, 
CamelContext context) {
+        DefaultExchange answer = new DefaultExchange(context, 
exchange.getPattern());
+        if (exchange.hasProperties()) {
+            answer.setProperties(safeCopy(exchange.getProperties()));
+        }
+        // Need to hand over the completion for async invocation
+        exchange.handoverCompletions(answer);        
+        answer.setIn(exchange.getIn().copy());
+        if (exchange.hasOut()) {
+            answer.setOut(exchange.getOut().copy());
+        }
+        answer.setException(exchange.getException());
+        return answer;
+        
+    }
+    
+    private static Map<String, Object> safeCopy(Map<String, Object> 
properties) {
+        if (properties == null) {
+            return null;
+        }
+        return new ConcurrentHashMap<String, Object>(properties);
+    }
 }

Modified: 
camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/vm/AbstractVmTestSupport.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/vm/AbstractVmTestSupport.java?rev=1209449&r1=1209448&r2=1209449&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/vm/AbstractVmTestSupport.java
 (original)
+++ 
camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/vm/AbstractVmTestSupport.java
 Fri Dec  2 12:38:22 2011
@@ -39,6 +39,7 @@ public abstract class AbstractVmTestSupp
         super.setUp();
         
         context2 = new DefaultCamelContext();
+        
         RouteBuilder routeBuilder = createRouteBuilderForSecondContext();
         if (routeBuilder != null) {
             context2.addRoutes(routeBuilder);            

Modified: 
camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/vm/VmDifferentOptionsOnConsumerAndProducerTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/vm/VmDifferentOptionsOnConsumerAndProducerTest.java?rev=1209449&r1=1209448&r2=1209449&view=diff
==============================================================================
--- 
camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/vm/VmDifferentOptionsOnConsumerAndProducerTest.java
 (original)
+++ 
camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/vm/VmDifferentOptionsOnConsumerAndProducerTest.java
 Fri Dec  2 12:38:22 2011
@@ -20,6 +20,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.util.ServiceHelper;
 import org.junit.After;
@@ -63,11 +64,16 @@ public class VmDifferentOptionsOnConsume
 
     @Test
     public void testSendToVm() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.expectedBodiesReceived("Hello World");
+        
 
         template2.sendBody("direct:start", "Hello World");
 
         assertMockEndpointsSatisfied();
+        
+        // check the camel context of the exchange
+        assertEquals("Get a wrong context. ", context, 
result.getExchanges().get(0).getContext());
     }
 
     @Override
@@ -80,4 +86,4 @@ public class VmDifferentOptionsOnConsume
             }
         };
     }
-}
\ No newline at end of file
+}


Reply via email to