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/05/06 11:17:46 UTC

svn commit: r941620 - in /camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty: NettyHelper.java NettyProducer.java

Author: davsclaus
Date: Thu May  6 09:17:43 2010
New Revision: 941620

URL: http://svn.apache.org/viewvc?rev=941620&view=rev
Log:
CAMEL-2698: Netty should detect write failures using future.

Added:
    camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyHelper.java   (with props)
Modified:
    camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java

Added: camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyHelper.java?rev=941620&view=auto
==============================================================================
--- camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyHelper.java (added)
+++ camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyHelper.java Thu May  6 09:17:43 2010
@@ -0,0 +1,62 @@
+/**
+ * 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;
+
+import org.apache.camel.CamelExchangeException;
+import org.apache.camel.Exchange;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jboss.netty.channel.Channel;
+import org.jboss.netty.channel.ChannelFuture;
+
+/**
+ * Helper class used internally by camel-netty using Netty.
+ *
+ * @version $Revision$
+ */
+public final class NettyHelper {
+
+    private static final transient Log LOG = LogFactory.getLog(NettyHelper.class);
+
+    private NettyHelper() {
+        // Utility class
+    }
+
+    /**
+     * Writes the given body to Netty channel. Will wait until the body has been written.
+     *
+     * @param channel  the Netty channel
+     * @param body     the body to write (send)
+     * @param exchange the exchange
+     * @throws CamelExchangeException is thrown if the body could not be written for some reasons
+     *                                (eg remote connection is closed etc.)
+     */
+    public static void writeBody(Channel channel, Object body, Exchange exchange) throws CamelExchangeException {
+        // the write operation is asynchronous. Use future to wait until the session has been written
+        ChannelFuture future = channel.write(body);
+
+        // wait for the write
+        future.awaitUninterruptibly();
+
+        // if it was not a success then thrown an exception
+        if (future.isSuccess() == false) {
+            LOG.warn("Cannot write body: " + body + " using channel: " + channel);
+            throw new CamelExchangeException("Cannot write body", exchange);
+        }
+    }
+
+}

Propchange: camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java?rev=941620&r1=941619&r2=941620&view=diff
==============================================================================
--- camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java (original)
+++ camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java Thu May  6 09:17:43 2010
@@ -73,9 +73,10 @@ public class NettyProducer extends Defau
         super.doStop();
     }
 
-    
     @Override
     public boolean isSingleton() {
+        // the producer should not be singleton otherwise cannot use concurrent producers and safely
+        // use request/reply with correct correlation
         return false;
     }
 
@@ -83,10 +84,11 @@ public class NettyProducer extends Defau
         if (configuration.isSync()) {
             countdownLatch = new CountDownLatch(1);
         }
-        
+
+        // write the body
         Channel channel = channelFuture.getChannel();
-        channel.write(exchange.getIn().getBody());
-        
+        NettyHelper.writeBody(channel, exchange.getIn().getBody(), exchange);
+
         if (configuration.isSync()) {
             boolean success = countdownLatch.await(configuration.getReceiveTimeoutMillis(), TimeUnit.MILLISECONDS);
             if (!success) {
@@ -97,7 +99,7 @@ public class NettyProducer extends Defau
         }                 
     }
 
-    public void setupTCPCommunication() throws Exception {
+    protected void setupTCPCommunication() throws Exception {
         if (channelFactory == null) {
             ExecutorService bossExecutor = 
                 context.getExecutorServiceStrategy().newThreadPool(this, 
@@ -128,7 +130,7 @@ public class NettyProducer extends Defau
         LOG.info("Netty TCP Producer started and now listening on Host: " + configuration.getHost() + "Port : " + configuration.getPort());
     }
     
-    public void setupUDPCommunication() throws Exception {
+    protected void setupUDPCommunication() throws Exception {
         if (datagramChannelFactory == null) {
             ExecutorService workerExecutor = 
                 context.getExecutorServiceStrategy().newThreadPool(this,