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 2009/04/10 10:22:03 UTC

svn commit: r763886 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/component/seda/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/model/ main/java/org/apache/camel/processor/ main/java/org/apache/c...

Author: davsclaus
Date: Fri Apr 10 08:22:03 2009
New Revision: 763886

URL: http://svn.apache.org/viewvc?rev=763886&view=rev
Log:
CAMEL-1525: Fixed seda producer copying exchange should not use same UoW as source. Fixed CS.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/seda/SedaUnitOfWorkTest.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MulticastUnitOfWorkTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/seda/CollectionProducer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/WireTapProcessor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=763886&r1=763885&r2=763886&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Fri Apr 10 08:22:03 2009
@@ -254,13 +254,20 @@
     Exchange copy();
 
     /**
+     * Creates a new instance and copies from the current message exchange so that it can be
+     * forwarded to another destination as a new instance. Unlike regular copy this operation
+     * will not share the same {@link org.apache.camel.spi.UnitOfWork} so its should be used
+     * for async messaging, where the original and copied exchange are independent.
+     */
+    Exchange newCopy();
+
+    /**
      * Copies the data into this exchange from the given exchange
      *
      * @param source is the source from which headers and messages will be copied
      */
     void copyFrom(Exchange source);
 
-
     /**
      * Returns the endpoint which originated this message exchange if a consumer on an endpoint created the message exchange
      * otherwise this property will be null

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/seda/CollectionProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/seda/CollectionProducer.java?rev=763886&r1=763885&r2=763886&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/seda/CollectionProducer.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/seda/CollectionProducer.java Fri Apr 10 08:22:03 2009
@@ -38,11 +38,13 @@
     }
 
     public void process(Exchange exchange) throws Exception {
-        queue.add(exchange.copy());
+        // use new copy to not share the same unit of work
+        queue.add(exchange.newCopy());
     }
 
     public boolean process(Exchange exchange, AsyncCallback callback) {
-        queue.add(exchange.copy());
+        // use new copy to not share the same unit of work
+        queue.add(exchange.newCopy());
         callback.done(true);
         return true;
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java?rev=763886&r1=763885&r2=763886&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java Fri Apr 10 08:22:03 2009
@@ -87,6 +87,13 @@
         return exchange;
     }
 
+    public Exchange newCopy() {
+        Exchange exchange = copy();
+        // do not share the unit of work
+        exchange.setUnitOfWork(null);
+        return exchange;
+    }
+
     public void copyFrom(Exchange exchange) {
         if (exchange == this) {
             return;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java?rev=763886&r1=763885&r2=763886&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java Fri Apr 10 08:22:03 2009
@@ -44,12 +44,12 @@
     // if we extend PolicyDefinition so we must make a copy of the class
     @XmlTransient
     public static final String PROPAGATION_REQUIRED = "PROPAGATION_REQUIRED";
+    @XmlTransient
+    protected Class<? extends Policy> type = TransactedPolicy.class;
     @XmlAttribute
     protected String ref;
     @XmlTransient
     private Policy policy;
-    @XmlTransient
-    protected Class<? extends Policy> type = TransactedPolicy.class;
 
     public TransactedDefinition() {
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java?rev=763886&r1=763885&r2=763886&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java Fri Apr 10 08:22:03 2009
@@ -189,8 +189,8 @@
                 updateNewExchange(subExchange, i, pairs);
                 try {
                     producer.process(subExchange);
-                } catch (Exception exception) {
-                    subExchange.setException(exception);
+                } catch (Exception e) {
+                    subExchange.setException(e);
                 }
                 doAggregate(result, subExchange);
                 i++;
@@ -223,9 +223,10 @@
 
     protected Iterable<ProcessorExchangePair> createProcessorExchangePairs(Exchange exchange) {
         List<ProcessorExchangePair> result = new ArrayList<ProcessorExchangePair>(processors.size());
-        Processor[] processorsArray = processors.toArray(new Processor[processors.size()]);
-        for (int i = 0; i < processorsArray.length; i++) {
-            result.add(new ProcessorExchangePair(processorsArray[i], exchange.copy()));
+
+        for (Processor processor : processors) {
+            Exchange copy = exchange.copy();
+            result.add(new ProcessorExchangePair(processor, copy));
         }
         return result;
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/WireTapProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/WireTapProcessor.java?rev=763886&r1=763885&r2=763886&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/WireTapProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/WireTapProcessor.java Fri Apr 10 08:22:03 2009
@@ -142,7 +142,7 @@
 
     private Exchange configureCopyExchange(Exchange exchange) {
         // must use a copy as we dont want it to cause side effects of the original exchange
-        Exchange copy = exchange.copy();
+        Exchange copy = exchange.newCopy();
         // set MEP to InOnly as this wire tap is a fire and forget
         copy.setPattern(ExchangePattern.InOnly);
         return copy;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java?rev=763886&r1=763885&r2=763886&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java Fri Apr 10 08:22:03 2009
@@ -224,7 +224,11 @@
     //-------------------------------------------------------------------------
     protected Object getBreadCrumbID(Exchange exchange) {
         UnitOfWork unitOfWork = exchange.getUnitOfWork();
-        return unitOfWork.getId();
+        if (unitOfWork != null) {
+            return unitOfWork.getId();
+        } else {
+            return exchange.getExchangeId();
+        }
     }
 
     protected String getNodeMessage(ProcessorDefinition node) {

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/seda/SedaUnitOfWorkTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/seda/SedaUnitOfWorkTest.java?rev=763886&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/seda/SedaUnitOfWorkTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/seda/SedaUnitOfWorkTest.java Fri Apr 10 08:22:03 2009
@@ -0,0 +1,102 @@
+/**
+ * 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.component.seda;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.interceptor.Tracer;
+import org.apache.camel.spi.Synchronization;
+
+/**
+ * Unit test to verify unit of work with seda.
+ *
+ * @version $Revision$
+ */
+public class SedaUnitOfWorkTest extends ContextTestSupport {
+
+    private static String sync;
+    private static String lastOne;
+
+    public void testSedaUOW() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals("onCompleteB", sync);
+        assertEquals("onCompleteB", lastOne);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                context.addInterceptStrategy(new Tracer());
+
+                from("direct:start")
+                        .process(new MyUOWProcessor("A"))
+                        .to("seda:foo");
+
+                from("seda:foo")
+                        // use a little delay to allow the first route to complete
+                        .delay(100)
+                        .process(new Processor() {
+                            public void process(Exchange exchange) throws Exception {
+                                assertEquals("onCompleteA", sync);
+                            }
+                        })
+                        .process(new MyUOWProcessor("B"))
+                        .process(new Processor() {
+                            public void process(Exchange exchange) throws Exception {
+                                lastOne = "processor";
+                            }
+                        })
+                        .to("mock:result");
+            }
+        };
+    }
+
+    private final class MyUOWProcessor implements Processor {
+
+        private String id;
+
+        private MyUOWProcessor(String id) {
+            this.id = id;
+        }
+
+        public void process(Exchange exchange) throws Exception {
+            exchange.getUnitOfWork().addSynchronization(new Synchronization() {
+                public void onComplete(Exchange exchange) {
+                    sync = "onComplete" + id;
+                    lastOne = sync;
+                }
+
+                public void onFailure(Exchange exchange) {
+                    sync = "onFailure" + id;
+                    lastOne = sync;
+                }
+            });
+        }
+    }
+
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/seda/SedaUnitOfWorkTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/seda/SedaUnitOfWorkTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MulticastUnitOfWorkTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MulticastUnitOfWorkTest.java?rev=763886&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MulticastUnitOfWorkTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MulticastUnitOfWorkTest.java Fri Apr 10 08:22:03 2009
@@ -0,0 +1,104 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.interceptor.Tracer;
+import org.apache.camel.spi.Synchronization;
+
+/**
+ * Unit test to verify unit of work with multicast.
+ *
+ * @version $Revision$
+ */
+public class MulticastUnitOfWorkTest extends ContextTestSupport {
+
+    private static String sync;
+    private static String lastOne;
+
+    public void testMulticastUOW() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        // the UoW is shared for multicast with direct
+        // so B should be the last
+        assertEquals("onCompleteB", sync);
+        assertEquals("onCompleteB", lastOne);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                context.addInterceptStrategy(new Tracer());
+
+                from("direct:start")
+                        .process(new MyUOWProcessor("A"))
+                        .multicast().to("direct:foo", "direct:bar");
+
+                from("direct:foo")
+                        .process(new Processor() {
+                            public void process(Exchange exchange) throws Exception {
+                                assertNull("First exchange is not complete yet", sync);
+                            }
+                        })
+                        .process(new MyUOWProcessor("B"))
+                        .process(new Processor() {
+                            public void process(Exchange exchange) throws Exception {
+                                lastOne = "processor";
+                            }
+                        })
+                        .to("mock:result");
+
+                from("direct:bar").to("mock:result");
+            }
+        };
+    }
+
+    private final class MyUOWProcessor implements Processor {
+
+        private String id;
+
+        private MyUOWProcessor(String id) {
+            this.id = id;
+        }
+
+        public void process(Exchange exchange) throws Exception {
+            exchange.getUnitOfWork().addSynchronization(new Synchronization() {
+                public void onComplete(Exchange exchange) {
+                    sync = "onComplete" + id;
+                    lastOne = sync;
+                }
+
+                public void onFailure(Exchange exchange) {
+                    sync = "onFailure" + id;
+                    lastOne = sync;
+                }
+            });
+        }
+    }
+
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MulticastUnitOfWorkTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MulticastUnitOfWorkTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date