Author: davsclaus
Date: Wed Dec 12 10:37:20 2012
New Revision: 1420610

URL: http://svn.apache.org/viewvc?rev=1420610&view=rev
Log:
Make it easier to customize and reuse ServerChannelHandler in camel-netty.

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

Modified: 
camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ServerChannelHandler.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ServerChannelHandler.java?rev=1420610&r1=1420609&r2=1420610&view=diff
==============================================================================
--- 
camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ServerChannelHandler.java
 (original)
+++ 
camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ServerChannelHandler.java
 Wed Dec 12 10:37:20 2012
@@ -19,16 +19,13 @@ package org.apache.camel.component.netty
 import java.net.SocketAddress;
 
 import org.apache.camel.AsyncCallback;
-import org.apache.camel.CamelExchangeException;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
-import org.apache.camel.component.netty.NettyConstants;
 import org.apache.camel.component.netty.NettyConsumer;
 import org.apache.camel.component.netty.NettyHelper;
 import org.apache.camel.component.netty.NettyPayloadHelper;
 import org.apache.camel.util.CamelLogger;
 import org.apache.camel.util.IOHelper;
-import org.jboss.netty.channel.ChannelFuture;
 import org.jboss.netty.channel.ChannelFutureListener;
 import org.jboss.netty.channel.ChannelHandlerContext;
 import org.jboss.netty.channel.ChannelStateEvent;
@@ -66,6 +63,7 @@ public class ServerChannelHandler extend
         if (LOG.isTraceEnabled()) {
             LOG.trace("Channel closed: {}", e.getChannel());
         }
+        // to keep track of open sockets
         consumer.getAllChannels().remove(e.getChannel());
     }
 
@@ -167,7 +165,7 @@ public class ServerChannelHandler extend
             }
 
             // we got a body to write
-            ChannelFutureListener listener = new 
ResponseFutureListener(exchange, messageEvent.getRemoteAddress());
+            ChannelFutureListener listener = 
createResponseFutureListener(consumer, exchange, 
messageEvent.getRemoteAddress());
             if (consumer.getConfiguration().isTcp()) {
                 NettyHelper.writeBodyAsync(LOG, messageEvent.getChannel(), 
null, body, exchange, listener);
             } else {
@@ -177,47 +175,15 @@ public class ServerChannelHandler extend
     }
 
     /**
-     * A {@link ChannelFutureListener} that performs the disconnect logic when
-     * sending the response is complete.
+     * Creates the {@link ChannelFutureListener} to execute when writing the 
response is complete.
+     *
+     * @param consumer          the netty consumer
+     * @param exchange          the exchange
+     * @param remoteAddress     the remote address of the message
+     * @return the listener.
      */
-    private final class ResponseFutureListener implements 
ChannelFutureListener {
-
-        private final Exchange exchange;
-        private final SocketAddress remoteAddress;
-
-        private ResponseFutureListener(Exchange exchange, SocketAddress 
remoteAddress) {
-            this.exchange = exchange;
-            this.remoteAddress = remoteAddress;
-        }
-
-        @Override
-        public void operationComplete(ChannelFuture future) throws Exception {
-            // if it was not a success then thrown an exception
-            if (!future.isSuccess()) {
-                Exception e = new CamelExchangeException("Cannot write 
response to " + remoteAddress, exchange, future.getCause());
-                consumer.getExceptionHandler().handleException(e);
-            }
-
-            // should channel be closed after complete?
-            Boolean close;
-            if (exchange.hasOut()) {
-                close = 
exchange.getOut().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, 
Boolean.class);
-            } else {
-                close = 
exchange.getIn().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, 
Boolean.class);
-            }
-
-            // should we disconnect, the header can override the configuration
-            boolean disconnect = consumer.getConfiguration().isDisconnect();
-            if (close != null) {
-                disconnect = close;
-            }
-            if (disconnect) {
-                if (LOG.isTraceEnabled()) {
-                    LOG.trace("Closing channel when complete at address: {}", 
remoteAddress);
-                }
-                NettyHelper.close(future.getChannel());
-            }
-        }
+    protected ChannelFutureListener createResponseFutureListener(NettyConsumer 
consumer, Exchange exchange, SocketAddress remoteAddress) {
+        return new ServerResponseFutureListener(consumer, exchange, 
remoteAddress);
     }
 
 }

Added: 
camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ServerResponseFutureListener.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ServerResponseFutureListener.java?rev=1420610&view=auto
==============================================================================
--- 
camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ServerResponseFutureListener.java
 (added)
+++ 
camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ServerResponseFutureListener.java
 Wed Dec 12 10:37:20 2012
@@ -0,0 +1,77 @@
+/**
+ * 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.handlers;
+
+import java.net.SocketAddress;
+
+import org.apache.camel.CamelExchangeException;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.netty.NettyConstants;
+import org.apache.camel.component.netty.NettyConsumer;
+import org.apache.camel.component.netty.NettyHelper;
+import org.jboss.netty.channel.ChannelFuture;
+import org.jboss.netty.channel.ChannelFutureListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A {@link org.jboss.netty.channel.ChannelFutureListener} that performs the 
disconnect logic when
+ * sending the response is complete.
+ */
+public class ServerResponseFutureListener implements ChannelFutureListener {
+
+    // use NettyConsumer as logger to make it easier to read the logs as this 
is part of the consumer
+    private static final transient Logger LOG = 
LoggerFactory.getLogger(NettyConsumer.class);
+    private final NettyConsumer consumer;
+    private final Exchange exchange;
+    private final SocketAddress remoteAddress;
+
+    public ServerResponseFutureListener(NettyConsumer consumer, Exchange 
exchange, SocketAddress remoteAddress) {
+        this.consumer = consumer;
+        this.exchange = exchange;
+        this.remoteAddress = remoteAddress;
+    }
+
+    @Override
+    public void operationComplete(ChannelFuture future) throws Exception {
+        // if it was not a success then thrown an exception
+        if (!future.isSuccess()) {
+            Exception e = new CamelExchangeException("Cannot write response to 
" + remoteAddress, exchange, future.getCause());
+            consumer.getExceptionHandler().handleException(e);
+        }
+
+        // should channel be closed after complete?
+        Boolean close;
+        if (exchange.hasOut()) {
+            close = 
exchange.getOut().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, 
Boolean.class);
+        } else {
+            close = 
exchange.getIn().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, 
Boolean.class);
+        }
+
+        // should we disconnect, the header can override the configuration
+        boolean disconnect = consumer.getConfiguration().isDisconnect();
+        if (close != null) {
+            disconnect = close;
+        }
+        if (disconnect) {
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Closing channel when complete at address: {}", 
remoteAddress);
+            }
+            NettyHelper.close(future.getChannel());
+        }
+    }
+}

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

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


Reply via email to