Updated Branches:
  refs/heads/master 7cdf1203f -> 59f03554e

CAMEL-5828 Applied the new patch of camel-disruptor


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

Branch: refs/heads/master
Commit: 59f03554e16066c75d5a339f91a4fc03365b3533
Parents: 7cdf120
Author: Willem Jiang <[email protected]>
Authored: Thu May 23 15:43:46 2013 +0800
Committer: Willem Jiang <[email protected]>
Committed: Thu May 23 15:44:40 2013 +0800

----------------------------------------------------------------------
 .../disruptor/AbstractSynchronizedExchange.java    |   58 ++++++++++
 .../component/disruptor/DisruptorConsumer.java     |   60 ++++++-----
 .../component/disruptor/DisruptorReference.java    |    2 +-
 .../camel/component/disruptor/ExchangeEvent.java   |   42 ++------
 .../MultipleConsumerSynchronizedExchange.java      |   65 ++++++++++++
 .../SingleConsumerSynchronizedExchange.java        |   43 ++++++++
 .../component/disruptor/SynchronizedExchange.java  |   30 ++++++
 ...rectRequestReplyMultipleConsumersInOutTest.java |   81 ---------------
 8 files changed, 238 insertions(+), 143 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/59f03554/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/AbstractSynchronizedExchange.java
----------------------------------------------------------------------
diff --git 
a/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/AbstractSynchronizedExchange.java
 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/AbstractSynchronizedExchange.java
new file mode 100644
index 0000000..3289dbc
--- /dev/null
+++ 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/AbstractSynchronizedExchange.java
@@ -0,0 +1,58 @@
+/**
+ * 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.disruptor;
+
+import java.util.List;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.Synchronization;
+import org.apache.camel.util.UnitOfWorkHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class AbstractSynchronizedExchange implements 
SynchronizedExchange {
+    private static final Logger LOG = 
LoggerFactory.getLogger(SynchronizedExchange.class);
+    protected final List<Synchronization> synchronizations;
+    private final Exchange exchange;
+
+    public AbstractSynchronizedExchange(Exchange exchange) {
+        this.exchange = exchange;
+        synchronizations = exchange.handoverCompletions();
+    }
+
+    @Override
+    public Exchange getExchange() {
+        return exchange;
+    }
+
+    @Override
+    public Exchange cancelAndGetOriginalExchange() {
+        if (synchronizations != null) {
+            for (Synchronization synchronization : synchronizations) {
+                exchange.addOnCompletion(synchronization);
+            }
+        }
+
+        return exchange;
+    }
+
+    protected void performSynchronization() {
+        //call synchronizations with the result
+        UnitOfWorkHelper.doneSynchronizations(getExchange(),
+                synchronizations, AbstractSynchronizedExchange.LOG);
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/59f03554/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/DisruptorConsumer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/DisruptorConsumer.java
 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/DisruptorConsumer.java
index 16f85ce..a73cd5d 100644
--- 
a/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/DisruptorConsumer.java
+++ 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/DisruptorConsumer.java
@@ -131,27 +131,38 @@ public class DisruptorConsumer extends ServiceSupport 
implements Consumer, Suspe
         return newExchange;
     }
 
-    private void process(final ExchangeEvent exchangeEvent) {
-        Exchange exchange = exchangeEvent.getExchange();
-
-        final boolean ignore = exchange
-                .getProperty(DisruptorEndpoint.DISRUPTOR_IGNORE_EXCHANGE, 
false, boolean.class);
-        if (ignore) {
-            // Property was set and it was set to true, so don't process 
Exchange.
-            LOGGER.trace("Ignoring exchange {}", exchange);
-            return;
-        }
+    private void process(final SynchronizedExchange synchronizedExchange) {
+        try {
+            Exchange exchange = synchronizedExchange.getExchange();
+
+            final boolean ignore = exchange.hasProperties() && exchange
+                    
.getProperties().containsKey(DisruptorEndpoint.DISRUPTOR_IGNORE_EXCHANGE);
+            if (ignore) {
+                // Property was set and it was set to true, so don't process 
Exchange.
+                LOGGER.trace("Ignoring exchange {}", exchange);
+                return;
+            }
 
-        // send a new copied exchange with new camel context
-        final Exchange result = prepareExchange(exchange);
-        // use the regular processor and use the asynchronous routing engine 
to support it
-        AsyncCallback callback = new AsyncCallback() {
-            @Override
-            public void done(boolean doneSync) {
-                exchangeEvent.consumed(result);
+            // send a new copied exchange with new camel context
+            final Exchange result = prepareExchange(exchange);
+            // use the regular processor and use the asynchronous routing 
engine to support it
+            AsyncCallback callback = new AsyncCallback() {
+                @Override
+                public void done(boolean doneSync) {
+                    synchronizedExchange.consumed(result);
+                }
+            };
+            AsyncProcessorHelper.process(processor, result, callback);
+        } catch (Exception e) {
+            Exchange exchange = synchronizedExchange.getExchange();
+
+            if (exchange != null) {
+                getExceptionHandler().handleException("Error processing 
exchange",
+                        exchange, e);
+            } else {
+                getExceptionHandler().handleException(e);
             }
-        };
-        AsyncProcessorHelper.process(processor, result, callback);
+        }
     }
 
     /**
@@ -178,16 +189,7 @@ public class DisruptorConsumer extends ServiceSupport 
implements Consumer, Suspe
             // which can be used to determine whether he should process the 
exchange, or leave it for his brethren.
             //see 
http://code.google.com/p/disruptor/wiki/FrequentlyAskedQuestions#How_do_you_arrange_a_Disruptor_with_multiple_consumers_so_that_e
             if (sequence % concurrentConsumers == ordinal) {
-                try {
-                    process(event);
-                } catch (Exception e) {
-                    final Exchange exchange = event.getExchange();
-                    if (exchange != null) {
-                        getExceptionHandler().handleException("Error 
processing exchange", exchange, e);
-                    } else {
-                        getExceptionHandler().handleException(e);
-                    }
-                }
+                process(event.getSynchronizedExchange());
             }
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/59f03554/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/DisruptorReference.java
----------------------------------------------------------------------
diff --git 
a/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/DisruptorReference.java
 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/DisruptorReference.java
index 8444ea9..581473f 100644
--- 
a/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/DisruptorReference.java
+++ 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/DisruptorReference.java
@@ -386,7 +386,7 @@ public class DisruptorReference {
         @Override
         public void onEvent(final ExchangeEvent event, final long sequence, 
final boolean endOfBatch) throws Exception {
             blockingLatch.await();
-            final Exchange exchange = event.getExchange();
+            final Exchange exchange = 
event.getSynchronizedExchange().cancelAndGetOriginalExchange();
 
             if 
(exchange.getProperty(DisruptorEndpoint.DISRUPTOR_IGNORE_EXCHANGE, false, 
boolean.class)) {
                 // Property was set and it was set to true, so don't process 
Exchange.

http://git-wip-us.apache.org/repos/asf/camel/blob/59f03554/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/ExchangeEvent.java
----------------------------------------------------------------------
diff --git 
a/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/ExchangeEvent.java
 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/ExchangeEvent.java
index 1ff1ccd..007351c 100644
--- 
a/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/ExchangeEvent.java
+++ 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/ExchangeEvent.java
@@ -32,48 +32,26 @@ import org.slf4j.LoggerFactory;
  */
 public class ExchangeEvent {
 
-    private static final Logger LOG = 
LoggerFactory.getLogger(ExchangeEvent.class);
-
-    private Exchange exchange;
-
-    private volatile int expectedConsumers;
-
-    private final AtomicInteger processedConsumers = new AtomicInteger(0);
-
-    private final AtomicReference<List<Exchange>> results = new 
AtomicReference<List<Exchange>>(new ArrayList <Exchange>());
-
-    public Exchange getExchange() {
-        return exchange;
+    private SynchronizedExchange synchronizedExchange;
+    
+    public SynchronizedExchange getSynchronizedExchange() {
+        return synchronizedExchange;
     }
 
     public void setExchange(final Exchange exchange, int expectedConsumers) {
-        this.exchange = exchange;
-        this.expectedConsumers = expectedConsumers;
-        processedConsumers.set(0);
+        synchronizedExchange = createSynchronizedExchange(exchange, 
expectedConsumers);
     }
 
-    public void consumed(Exchange result) {
+    private SynchronizedExchange createSynchronizedExchange(Exchange exchange, 
int expectedConsumers) {
         if (expectedConsumers > 1) {
-            results.get().add(result);
-        }
-
-        if (processedConsumers.incrementAndGet() == expectedConsumers) {
-            // all consumers are done processing
-            if (expectedConsumers == 1) {
-                // this was the only consumer, call synchronizations with this 
result
-                UnitOfWorkHelper.doneSynchronizations(result, 
exchange.handoverCompletions(), LOG);
-            } else {
-                // this was the last consumer but we had more
-                // set the list of results as GROUPED_EXCHANGE property on the 
original exchange instead
-                List<Exchange> localResults = results.getAndSet(new 
ArrayList<Exchange>());
-                exchange.setProperty(Exchange.GROUPED_EXCHANGE, localResults);
-                UnitOfWorkHelper.doneSynchronizations(exchange, 
exchange.handoverCompletions(), LOG);
-            }
+            return new MultipleConsumerSynchronizedExchange(exchange, 
expectedConsumers);
+        } else {
+            return new SingleConsumerSynchronizedExchange(exchange);
         }
     }
 
     @Override
     public String toString() {
-        return "ExchangeEvent{" + "exchange=" + exchange + '}';
+        return "ExchangeEvent{" + "exchange=" + 
synchronizedExchange.getExchange()  + '}';
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/59f03554/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/MultipleConsumerSynchronizedExchange.java
----------------------------------------------------------------------
diff --git 
a/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/MultipleConsumerSynchronizedExchange.java
 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/MultipleConsumerSynchronizedExchange.java
new file mode 100644
index 0000000..18b66f3
--- /dev/null
+++ 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/MultipleConsumerSynchronizedExchange.java
@@ -0,0 +1,65 @@
+/**
+ * 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.disruptor;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.util.ExchangeHelper;
+
+/**
+ * TODO: documentation
+ */
+public class MultipleConsumerSynchronizedExchange extends 
AbstractSynchronizedExchange {
+
+    private final int expectedConsumers;
+
+    private final AtomicInteger processedConsumers = new AtomicInteger(0);
+
+    private final AtomicBoolean resultHandled = new AtomicBoolean(false);
+
+    public MultipleConsumerSynchronizedExchange(Exchange exchange, int 
expectedConsumers) {
+        super(exchange);
+        this.expectedConsumers = expectedConsumers;
+        processedConsumers.set(0);
+    }
+
+
+    public void consumed(Exchange result) {
+
+        if (processedConsumers.incrementAndGet() == expectedConsumers || 
result.getException() != null 
+            && !resultHandled.getAndSet(true)) {
+            // all consumers are done processing or an exception occurred
+
+            //SEDA Does not configure an aggregator in the internally used 
MulticastProcessor
+            //As a result, the behaviour of SEDA in case of multicast is to 
only copy the results on an error
+            if (result.getException() != null) {
+                ExchangeHelper.copyResults(getExchange(), result);
+            }
+
+            performSynchronization();
+        }
+    }
+
+    @Override
+    public Exchange cancelAndGetOriginalExchange() {
+        resultHandled.set(true);
+
+        return super.cancelAndGetOriginalExchange();
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/59f03554/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/SingleConsumerSynchronizedExchange.java
----------------------------------------------------------------------
diff --git 
a/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/SingleConsumerSynchronizedExchange.java
 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/SingleConsumerSynchronizedExchange.java
new file mode 100644
index 0000000..9222673
--- /dev/null
+++ 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/SingleConsumerSynchronizedExchange.java
@@ -0,0 +1,43 @@
+/**
+ * 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.disruptor;
+
+import java.util.List;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.Synchronization;
+import org.apache.camel.util.ExchangeHelper;
+
+/**
+ * TODO: documentation
+ */
+public class SingleConsumerSynchronizedExchange extends  
AbstractSynchronizedExchange {
+
+    private final List<Synchronization> synchronizations;
+
+    public SingleConsumerSynchronizedExchange(Exchange exchange) {
+        super(exchange);
+        synchronizations = exchange.handoverCompletions();
+    }
+
+    @Override
+    public void consumed(Exchange result) {
+        ExchangeHelper.copyResults(getExchange(), result);
+
+        performSynchronization();
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/59f03554/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/SynchronizedExchange.java
----------------------------------------------------------------------
diff --git 
a/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/SynchronizedExchange.java
 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/SynchronizedExchange.java
new file mode 100644
index 0000000..2cef627
--- /dev/null
+++ 
b/components/camel-disruptor/src/main/java/org/apache/camel/component/disruptor/SynchronizedExchange.java
@@ -0,0 +1,30 @@
+/**
+ * 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.disruptor;
+
+import org.apache.camel.Exchange;
+
+/**
+ * TODO: documentation
+ */
+public interface SynchronizedExchange {
+    Exchange getExchange();
+
+    void consumed(Exchange result);
+
+    Exchange cancelAndGetOriginalExchange();
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/59f03554/components/camel-disruptor/src/test/java/org/apache/camel/component/disruptor/DirectRequestReplyMultipleConsumersInOutTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-disruptor/src/test/java/org/apache/camel/component/disruptor/DirectRequestReplyMultipleConsumersInOutTest.java
 
b/components/camel-disruptor/src/test/java/org/apache/camel/component/disruptor/DirectRequestReplyMultipleConsumersInOutTest.java
deleted file mode 100644
index 2ce870c..0000000
--- 
a/components/camel-disruptor/src/test/java/org/apache/camel/component/disruptor/DirectRequestReplyMultipleConsumersInOutTest.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/**
- * 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.disruptor;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.test.junit4.CamelTestSupport;
-import org.apache.camel.util.CastUtils;
-import org.junit.Test;
-
-/**
- * @version
- */
-public class DirectRequestReplyMultipleConsumersInOutTest extends 
CamelTestSupport {
-    @Test
-    public void testInOut() throws Exception {
-        List<String> expectedBodies = new ArrayList<String>(Arrays.asList("Bye 
World-1", "Bye World-2"));
-        getMockEndpoint("mock:log").expectedBodiesReceived(expectedBodies);
-
-        final Exchange out = template.request("direct:start", new Processor() {
-            @Override
-            public void process(Exchange exchange) throws Exception {
-                exchange.getIn().setBody("Hello World");
-            }
-        });
-
-        assertEquals("Bye World", getOutBody(out));
-        final List<Exchange> groupedExchange = CastUtils.cast(
-                out.getProperty(Exchange.GROUPED_EXCHANGE, List.class));
-
-        assertNotNull("Expected a grouped exchange property, found none", 
groupedExchange);
-        for (Exchange exchange : groupedExchange) {
-            assertTrue("Body of grouped exchange property '" + 
getOutBody(exchange) + "' did not match any expected body", 
expectedBodies.remove(getOutBody(exchange)));
-        }
-
-        log.info("Got reply " + out);
-
-        assertMockEndpointsSatisfied();
-    }
-
-    private Object getOutBody(Exchange exchange) {
-        return (exchange.hasOut() ? exchange.getOut() : 
exchange.getIn()).getBody();
-    }
-
-    @Override
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                // send the message as InOnly to DISRUPTOR as we want to 
continue routing
-                // (as we don't want to do request/reply over DISRUPTOR)
-                // In EIP patterns the WireTap pattern is what this would be
-                from("direct:start").transform(constant("Bye 
World")).inOut("disruptor:log?multipleConsumers=true");
-
-                
from("disruptor:log?multipleConsumers=true").delay(100).transform(body().append("-1"))
-                        .to("mock:log");
-                
from("disruptor:log?multipleConsumers=true").delay(1000).transform(body().append("-2"))
-                        .to("mock:log");
-            }
-        };
-    }
-}

Reply via email to