Updated Branches:
  refs/heads/master d33db16a3 -> 868a358ad

CAMEL-6442 fix the IllegalStateException issue in camel-netty-http route


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

Branch: refs/heads/master
Commit: 868a358adb801498e2e82b7d8f65547fd4e62eba
Parents: c59092e
Author: Willem Jiang <ningji...@apache.org>
Authored: Sat Jun 8 22:33:01 2013 +0800
Committer: Willem Jiang <ningji...@apache.org>
Committed: Sun Jun 9 08:56:34 2013 +0800

----------------------------------------------------------------------
 .../netty/http/NettyRouteSimpleTest.java        | 51 ++++++++++++++++++++
 .../camel/component/netty/NettyProducer.java    | 19 +++++++-
 2 files changed, 69 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/868a358a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyRouteSimpleTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyRouteSimpleTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyRouteSimpleTest.java
new file mode 100644
index 0000000..ae5e1ee
--- /dev/null
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyRouteSimpleTest.java
@@ -0,0 +1,51 @@
+/**
+ * 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.netty.http;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class NettyRouteSimpleTest extends BaseNettyTest {
+
+    @Test
+    public void testHttpSimple() throws Exception {
+        getMockEndpoint("mock:input1").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:input2").expectedBodiesReceived("Hello World");
+
+        String out = 
template.requestBody("netty-http:http://localhost:{{port}}/foo";, "Hello World", 
String.class);
+        assertEquals("Bye World", out);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            int port2 = getNextPort();
+
+            @Override
+            public void configure() throws Exception {
+                from("netty-http:http://0.0.0.0:{{port}}/foo";)
+                    .to("mock:input1")
+                    .to("netty-http:http://0.0.0.0:"; + port2 + "/bar");
+                from("netty-http:http://0.0.0.0:"; + port2 + "/bar")
+                    .to("mock:input2")
+                    .transform().constant("Bye World");
+            }
+        };
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/868a358a/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
 
b/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
index df3c40e..77cee1c 100644
--- 
a/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
+++ 
b/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
@@ -18,8 +18,10 @@ package org.apache.camel.component.netty;
 
 import java.net.InetSocketAddress;
 import java.util.Map;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.CamelContext;
@@ -399,7 +401,22 @@ public class NettyProducer extends DefaultAsyncProducer {
         if (LOG.isTraceEnabled()) {
             LOG.trace("Waiting for operation to complete {} for {} millis", 
channelFuture, configuration.getConnectTimeout());
         }
-        channelFuture.awaitUninterruptibly(configuration.getConnectTimeout());
+        // here we need to wait it in other thread
+        final CountDownLatch channelLatch = new CountDownLatch(1);
+        channelFuture.addListener(new ChannelFutureListener() {
+            @Override
+            public void operationComplete(ChannelFuture cf) throws Exception {
+                channelLatch.countDown();
+            }
+        });
+         
+        try {
+            channelLatch.await(configuration.getConnectTimeout(), 
TimeUnit.MILLISECONDS);
+        } catch (InterruptedException ex) {
+            throw new CamelException("Interrupted while waiting for " + 
"connection to "
+                                     + configuration.getAddress());
+        }
+        
 
         if (!channelFuture.isDone() || !channelFuture.isSuccess()) {
             throw new CamelException("Cannot connect to " + 
configuration.getAddress(), channelFuture.getCause());

Reply via email to