You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2015/11/04 21:30:54 UTC

camel git commit: CAMEL-9290 - reconnect netty4 consumer on channel close as well

Repository: camel
Updated Branches:
  refs/heads/master 62d6fa52a -> 79396207c


CAMEL-9290 - reconnect netty4 consumer on channel close as well


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

Branch: refs/heads/master
Commit: 79396207c59ac7f52d7f4abf6673c02825494a3f
Parents: 62d6fa5
Author: Jonathan Anstey <ja...@gmail.com>
Authored: Wed Nov 4 16:53:45 2015 -0330
Committer: Jonathan Anstey <ja...@gmail.com>
Committed: Wed Nov 4 16:53:57 2015 -0330

----------------------------------------------------------------------
 ...lientModeTCPNettyServerBootstrapFactory.java | 45 ++++++++++++++------
 1 file changed, 31 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/79396207/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/ClientModeTCPNettyServerBootstrapFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/ClientModeTCPNettyServerBootstrapFactory.java b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/ClientModeTCPNettyServerBootstrapFactory.java
index 202e9dd..6305156 100644
--- a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/ClientModeTCPNettyServerBootstrapFactory.java
+++ b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/ClientModeTCPNettyServerBootstrapFactory.java
@@ -31,6 +31,7 @@ import io.netty.channel.ChannelOption;
 import io.netty.channel.EventLoop;
 import io.netty.channel.EventLoopGroup;
 import io.netty.channel.socket.nio.NioSocketChannel;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelException;
 import org.apache.camel.support.ServiceSupport;
@@ -185,21 +186,10 @@ public class ClientModeTCPNettyServerBootstrapFactory extends ServiceSupport imp
         }
 
         if (!channelFuture.isDone() || !channelFuture.isSuccess()) {
-            //check if reconnect is enabled and schedule a reconnect, if from handler then dont schedule a reconnect
+            //check if reconnect is enabled and schedule a reconnect, if from handler then don't schedule a reconnect
             if (configuration.isReconnect()) {
-                final EventLoop loop = channelFuture.channel().eventLoop();
-                loop.schedule(new Runnable() {
-                    @Override
-                    public void run() {
-                        try {
-                            LOG.trace("Re-connecting to {} if needed", configuration.getAddress());
-                            doReconnectIfNeeded();
-                        } catch (Exception e) {
-                            LOG.warn("Error during re-connect to " + configuration.getAddress() + ". Will attempt again in "
-                                    + configuration.getReconnectInterval() + " millis. This exception is ignored.", e);
-                        }
-                    }
-                }, configuration.getReconnectInterval(), TimeUnit.MILLISECONDS);
+                scheduleReconnect(channelFuture);
+                return null;
             } else {
                 ConnectException cause = new ConnectException("Cannot connect to " + configuration.getAddress());
                 if (channelFuture.cause() != null) {
@@ -213,7 +203,34 @@ public class ClientModeTCPNettyServerBootstrapFactory extends ServiceSupport imp
         if (LOG.isDebugEnabled()) {
             LOG.debug("Creating connector to address: {}", configuration.getAddress());
         }
+        
+        // schedule a reconnect to happen when the channel closes
+        if (configuration.isReconnect()) {
+            answer.closeFuture().addListener(new ChannelFutureListener() {
+                @Override
+                public void operationComplete(ChannelFuture future) throws Exception {
+                    scheduleReconnect(channelFuture);
+                };
+            });
+        }
+        
         return answer;
     }
 
+    private void scheduleReconnect(final ChannelFuture channelFuture) {
+        final EventLoop loop = channelFuture.channel().eventLoop();
+        loop.schedule(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    LOG.trace("Re-connecting to {} if needed", configuration.getAddress());
+                    doReconnectIfNeeded();
+                } catch (Exception e) {
+                    LOG.warn("Error during re-connect to " + configuration.getAddress() + ". Will attempt again in "
+                            + configuration.getReconnectInterval() + " millis. This exception is ignored.", e);
+                }
+            }
+        }, configuration.getReconnectInterval(), TimeUnit.MILLISECONDS);
+    }
+
 }