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 2010/03/22 10:27:33 UTC

svn commit: r926008 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/processor/SendAsyncProcessor.java components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaToAsyncTest.java

Author: davsclaus
Date: Mon Mar 22 09:27:32 2010
New Revision: 926008

URL: http://svn.apache.org/viewvc?rev=926008&view=rev
Log:
CAMEL-2540: Fixed ToAsync having to keep producer until task is complete, to avoid pooled producers being reused to early.

Added:
    camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaToAsyncTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/SendAsyncProcessor.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/SendAsyncProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/SendAsyncProcessor.java?rev=926008&r1=926007&r2=926008&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/SendAsyncProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/SendAsyncProcessor.java Mon Mar 22 09:27:32 2010
@@ -35,8 +35,10 @@ import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.ProducerCallback;
 import org.apache.camel.impl.LoggingExceptionHandler;
+import org.apache.camel.impl.ProducerCache;
 import org.apache.camel.spi.ExceptionHandler;
 import org.apache.camel.util.ExchangeHelper;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * @version $Revision$
@@ -83,31 +85,44 @@ public class SendAsyncProcessor extends 
     public Exchange doProcess(Exchange exchange) throws Exception {
         // now we are done, we should have a API callback for this
         // send the exchange to the destination using a producer
-        Exchange answer = getProducerCache(exchange).doInProducer(destination, exchange, pattern, new ProducerCallback<Exchange>() {
-            public Exchange doInProducer(Producer producer, Exchange exchange, ExchangePattern pattern) throws Exception {
-                exchange = configureExchange(exchange, pattern);
-
-                // pass in the callback that adds the exchange to the completed list of tasks
-                final AsyncCallback callback = new AsyncCallback() {
-                    public void onTaskCompleted(Exchange exchange) {
-                        completedTasks.add(exchange);
+        final ProducerCache cache = getProducerCache(exchange);
+        // acquire the producer from the service pool
+        final Producer producer = cache.acquireProducer(destination);
+        ObjectHelper.notNull(producer, "producer");
+
+        // pass in the callback that adds the exchange to the completed list of tasks
+        final AsyncCallback callback = new AsyncCallback() {
+            public void onTaskCompleted(Exchange exchange) {
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("onTaskCompleted " + exchange);
+                }
+                try {
+                    completedTasks.add(exchange);
+                } finally {
+                    // must return the producer to service pool when we are done
+                    try {
+                        cache.releaseProducer(destination, producer);
+                    } catch (Exception e) {
+                        LOG.warn("Error releasing producer: " + producer + ". This exception will be ignored.", e);
                     }
-                };
-
-                if (producer instanceof AsyncProcessor) {
-                    // producer is async capable so let it process it directly
-                    doAsyncProcess((AsyncProcessor) producer, exchange, callback);
-                } else {
-                    // producer is a regular processor so simulate async behaviour
-                    doSimulateAsyncProcess(producer, exchange, callback);
                 }
-
-                // and return the exchange
-                return exchange;
             }
-        });
+        };
 
-        return answer;
+        // prepare exchange for async processing
+        exchange = configureExchange(exchange, pattern);
+
+        // process the exchange async
+        if (producer instanceof AsyncProcessor) {
+            // producer is async capable so let it process it directly
+            doAsyncProcess((AsyncProcessor) producer, exchange, callback);
+        } else {
+            // producer is a regular processor so simulate async behaviour
+            doSimulateAsyncProcess(producer, exchange, callback);
+        }
+
+        // and return the exchange
+        return exchange;
     }
 
     /**

Added: camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaToAsyncTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaToAsyncTest.java?rev=926008&view=auto
==============================================================================
--- camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaToAsyncTest.java (added)
+++ camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaToAsyncTest.java Mon Mar 22 09:27:32 2010
@@ -0,0 +1,63 @@
+/**
+ * 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.mina;
+
+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;
+
+/**
+ * @version $Revision$
+ */
+public class MinaToAsyncTest extends ContextTestSupport {
+
+    public void testToAsync() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceivedInAnyOrder("Bye Camel", "Bye World", "Bye Donkey", "Bye Tiger", "Bye Elephant");
+
+        template.sendBody("direct:start", "Camel");
+        template.sendBody("direct:start", "World");
+        template.sendBody("direct:start", "Donkey");
+        template.sendBody("direct:start", "Tiger");
+        template.sendBody("direct:start", "Elephant");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("mina:tcp://localhost:6202?textline=true&sync=true").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        String body = exchange.getIn().getBody(String.class);
+                        Thread.sleep(2000);
+                        exchange.getOut().setBody("Bye " + body);
+                    }
+                });
+
+                from("direct:start")
+                    .toAsync("mina:tcp://localhost:6202?sync=true&textline=true")
+                    .to("log:reply")
+                    .to("mock:result");
+            }
+        };
+    }
+}

Propchange: camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaToAsyncTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaToAsyncTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date