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 2015/07/09 11:32:27 UTC

[1/3] camel git commit: CAMEL-8894: Support to reconnect the server in ClientMode. Thanks to Darshan Sundaresh for the patch.

Repository: camel
Updated Branches:
  refs/heads/master af0486a0b -> 9ccce32ec


CAMEL-8894: Support to reconnect the server in ClientMode. Thanks to Darshan Sundaresh for the patch.


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

Branch: refs/heads/master
Commit: 68ee51cbcb8b9da48dc5916d1b8dbaf5c16241ca
Parents: af0486a
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Jul 9 11:12:12 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Jul 9 11:33:29 2015 +0200

----------------------------------------------------------------------
 ...lientModeTCPNettyServerBootstrapFactory.java |  46 +++--
 .../NettyServerBootstrapConfiguration.java      |  34 +++-
 .../NettyConsumerClientModeReconnectTest.java   | 170 +++++++++++++++++++
 .../src/test/resources/log4j.properties         |   7 +-
 4 files changed, 240 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/68ee51cb/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 d674340..202e9dd 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
@@ -28,6 +28,7 @@ import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelFutureListener;
 import io.netty.channel.ChannelInitializer;
 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;
@@ -143,7 +144,6 @@ public class ClientModeTCPNettyServerBootstrapFactory extends ServiceSupport imp
         }
         LOG.info("ClientModeServerBootstrap binding to {}:{}", configuration.getHost(), configuration.getPort());
         channel = openChannel(channelFuture);
-       
     }
 
     protected void stopServerBootstrap() {
@@ -155,8 +155,16 @@ public class ClientModeTCPNettyServerBootstrapFactory extends ServiceSupport imp
             workerGroup = null;
         }
     }
-    
-    protected Channel openChannel(ChannelFuture channelFuture) throws Exception {
+
+    protected void doReconnectIfNeeded() throws Exception {
+        if (channel == null || !channel.isActive()) {
+            LOG.debug("ClientModeServerBootstrap re-connect to {}:{}", configuration.getHost(), configuration.getPort());
+            ChannelFuture connectFuture = clientBootstrap.connect(new InetSocketAddress(configuration.getHost(), configuration.getPort()));
+            channel = openChannel(connectFuture);
+        }
+    }
+
+    protected Channel openChannel(final ChannelFuture channelFuture) throws Exception {
         // blocking for channel to be done
         if (LOG.isTraceEnabled()) {
             LOG.trace("Waiting for operation to complete {} for {} millis", channelFuture, configuration.getConnectTimeout());
@@ -173,27 +181,39 @@ public class ClientModeTCPNettyServerBootstrapFactory extends ServiceSupport imp
         try {
             channelLatch.await(configuration.getConnectTimeout(), TimeUnit.MILLISECONDS);
         } catch (InterruptedException ex) {
-            throw new CamelException("Interrupted while waiting for " + "connection to "
-                                     + configuration.getAddress());
+            throw new CamelException("Interrupted while waiting for " + "connection to " + configuration.getAddress());
         }
 
-
         if (!channelFuture.isDone() || !channelFuture.isSuccess()) {
-            ConnectException cause = new ConnectException("Cannot connect to " + configuration.getAddress());
-            if (channelFuture.cause() != null) {
-                cause.initCause(channelFuture.cause());
+            //check if reconnect is enabled and schedule a reconnect, if from handler then dont 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);
+            } else {
+                ConnectException cause = new ConnectException("Cannot connect to " + configuration.getAddress());
+                if (channelFuture.cause() != null) {
+                    cause.initCause(channelFuture.cause());
+                }
+                throw cause;
             }
-            throw cause;
         }
         Channel answer = channelFuture.channel();
-       
 
         if (LOG.isDebugEnabled()) {
             LOG.debug("Creating connector to address: {}", configuration.getAddress());
         }
-        
         return answer;
-        
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/68ee51cb/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyServerBootstrapConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyServerBootstrapConfiguration.java b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyServerBootstrapConfiguration.java
index 0791408..8fa9621 100644
--- a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyServerBootstrapConfiguration.java
+++ b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyServerBootstrapConfiguration.java
@@ -97,7 +97,11 @@ public class NettyServerBootstrapConfiguration implements Cloneable {
     protected EventLoopGroup workerGroup;
     @UriParam(label = "consumer")
     protected String networkInterface;
-    
+    @UriParam(label = "consumer")
+    private boolean reconnect;
+    @UriParam(label = "consumer", defaultValue = "10000")
+    private int reconnectInterval = 10000;
+
     public String getAddress() {
         return host + ":" + port;
     }
@@ -503,6 +507,28 @@ public class NettyServerBootstrapConfiguration implements Cloneable {
     }
 
     /**
+     * Used only in clientMode in consumer, the consumer will attempt to reconnect on disconnection if this is enabled
+     */
+    public boolean isReconnect() {
+        return reconnect;
+    }
+
+    public void setReconnect(boolean reconnect) {
+        this.reconnect = reconnect;
+    }
+
+    /**
+     * Used if reconnect and clientMode is enabled. The interval in milli seconds to attempt reconnection
+     */
+    public int getReconnectInterval() {
+        return reconnectInterval;
+    }
+
+    public void setReconnectInterval(int reconnectInterval) {
+        this.reconnectInterval = reconnectInterval;
+    }
+
+    /**
      * Checks if the other {@link NettyServerBootstrapConfiguration} is compatible
      * with this, as a Netty listener bound on port X shares the same common
      * {@link NettyServerBootstrapConfiguration}, which must be identical.
@@ -581,6 +607,10 @@ public class NettyServerBootstrapConfiguration implements Cloneable {
             isCompatible = false;
         } else if (networkInterface != null && !networkInterface.equals(other.networkInterface)) {
             isCompatible = false;
+        } else if (reconnect != other.reconnect) {
+            isCompatible = false;
+        } else if (reconnectInterval != other.reconnectInterval) {
+            isCompatible = false;
         }
 
         return isCompatible;
@@ -620,6 +650,8 @@ public class NettyServerBootstrapConfiguration implements Cloneable {
                 + ", bossGroup=" + bossGroup
                 + ", workerGroup=" + workerGroup
                 + ", networkInterface='" + networkInterface + '\''
+                + ", reconnect='" + reconnect + '\''
+                + ", reconnectInterval='" + reconnectInterval + '\''
                 + '}';
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/68ee51cb/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConsumerClientModeReconnectTest.java
----------------------------------------------------------------------
diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConsumerClientModeReconnectTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConsumerClientModeReconnectTest.java
new file mode 100644
index 0000000..6db9780
--- /dev/null
+++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConsumerClientModeReconnectTest.java
@@ -0,0 +1,170 @@
+/**
+ * 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.netty4;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.SimpleChannelInboundHandler;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.codec.DelimiterBasedFrameDecoder;
+import io.netty.handler.codec.Delimiters;
+import io.netty.handler.codec.string.StringDecoder;
+import io.netty.handler.codec.string.StringEncoder;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class NettyConsumerClientModeReconnectTest extends BaseNettyTest {
+
+    private MyServer server;
+
+    public void startNettyServer() throws Exception {
+        server = new MyServer(getPort());
+        server.start();
+    }
+
+    public void shutdownServer() {
+        if (server != null) {
+            server.shutdown();
+        }
+    }
+
+    @Test
+    public void testNettyRouteServerNotStarted() throws Exception {
+        try {
+            MockEndpoint receive = context.getEndpoint("mock:receive", MockEndpoint.class);
+            receive.expectedBodiesReceived("Bye Willem");
+
+            log.info(">>> starting Camel route while Netty server is not ready");
+            context.startRoute("client");
+
+            Thread.sleep(5000);
+
+            log.info(">>> starting Netty server");
+            startNettyServer();
+
+            assertMockEndpointsSatisfied();
+            log.info(">>> routing done");
+
+            Thread.sleep(500);
+        } finally {
+            log.info(">>> shutting down Netty server");
+            shutdownServer();
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("netty4:tcp://localhost:{{port}}?textline=true&clientMode=true&reconnect=true&reconnectInterval=2000").id("client")
+                        .process(new Processor() {
+                            public void process(final Exchange exchange) {
+                                log.info("Processing exchange in Netty server {}", exchange);
+                                String body = exchange.getIn().getBody(String.class);
+                                exchange.getOut().setBody("Bye " + body);
+                            }
+                        }).to("log:receive").to("mock:receive").noAutoStartup();
+            }
+        };
+    }
+
+    private static class MyServer {
+        private int port;
+        private ServerBootstrap bootstrap;
+        private Channel channel;
+        private EventLoopGroup bossGroup;
+        private EventLoopGroup workerGroup;
+
+        public MyServer(int port) {
+            this.port = port;
+        }
+
+        public void start() throws Exception {
+            bossGroup = new NioEventLoopGroup(1);
+            workerGroup = new NioEventLoopGroup();
+
+            bootstrap = new ServerBootstrap();
+            bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
+                    .childHandler(new ServerInitializer());
+
+            ChannelFuture cf = bootstrap.bind(port).sync();
+            channel = cf.channel();
+        }
+
+        public void shutdown() {
+            channel.disconnect();
+            bossGroup.shutdownGracefully();
+            workerGroup.shutdownGracefully();
+        }
+    }
+
+    private static class ServerHandler extends SimpleChannelInboundHandler<String> {
+
+        public void channelActive(ChannelHandlerContext ctx) throws Exception {
+            ctx.write("Willem\r\n");
+            ctx.flush();
+        }
+
+        @Override
+        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+            cause.printStackTrace();
+            ctx.close();
+        }
+
+        @Override
+        protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
+            // Do nothing here
+        }
+
+        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
+            ctx.flush();
+        }
+    }
+
+    private static class ServerInitializer extends ChannelInitializer<SocketChannel> {
+        private static final StringDecoder DECODER = new StringDecoder();
+        private static final StringEncoder ENCODER = new StringEncoder();
+        private static final ServerHandler SERVERHANDLER = new ServerHandler();
+
+        @Override
+        public void initChannel(SocketChannel ch) throws Exception {
+            ChannelPipeline pipeline = ch.pipeline();
+
+            // Add the text line codec combination first,
+            pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
+                    8192, Delimiters.lineDelimiter()));
+            // the encoder and decoder are static as these are sharable
+            pipeline.addLast("decoder", DECODER);
+            pipeline.addLast("encoder", ENCODER);
+
+            // and then business logic.
+            pipeline.addLast("handler", SERVERHANDLER);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/68ee51cb/components/camel-netty4/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/components/camel-netty4/src/test/resources/log4j.properties b/components/camel-netty4/src/test/resources/log4j.properties
index fed9ec9..d5da94c 100644
--- a/components/camel-netty4/src/test/resources/log4j.properties
+++ b/components/camel-netty4/src/test/resources/log4j.properties
@@ -18,12 +18,13 @@
 #
 # The logging properties used for eclipse testing, We want to see debug output on the console.
 #
-log4j.rootLogger=INFO, file
+log4j.rootLogger=INFO, out
 
 # uncomment the following to enable camel debugging
-#log4j.logger.org.apache.camel.component.netty=TRACE
+#log4j.logger.org.apache.camel.component.netty4=TRACE
+#log4j.logger.org.apache.camel.component.netty4.ClientModeTCPNettyServerBootstrapFactory=DEBUG
 #log4j.logger.org.apache.camel=DEBUG
-#log4j.logger.org.jboss.netty=TRACE
+#log4j.logger.io.netty=TRACE
 
 # CONSOLE appender not used by default
 log4j.appender.out=org.apache.log4j.ConsoleAppender


[2/3] camel git commit: CAMEL-8894: Lets reconnect by default in client mode.

Posted by da...@apache.org.
CAMEL-8894: Lets reconnect by default in client mode.


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

Branch: refs/heads/master
Commit: 463b9b75b073d3362a5a4ab46a7003c48fc9ab3e
Parents: 68ee51c
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Jul 9 11:36:15 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Jul 9 11:36:15 2015 +0200

----------------------------------------------------------------------
 .../component/netty4/NettyServerBootstrapConfiguration.java      | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/463b9b75/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyServerBootstrapConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyServerBootstrapConfiguration.java b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyServerBootstrapConfiguration.java
index 8fa9621..0e9af39 100644
--- a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyServerBootstrapConfiguration.java
+++ b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyServerBootstrapConfiguration.java
@@ -97,8 +97,8 @@ public class NettyServerBootstrapConfiguration implements Cloneable {
     protected EventLoopGroup workerGroup;
     @UriParam(label = "consumer")
     protected String networkInterface;
-    @UriParam(label = "consumer")
-    private boolean reconnect;
+    @UriParam(label = "consumer", defaultValue = "true")
+    private boolean reconnect = true;
     @UriParam(label = "consumer", defaultValue = "10000")
     private int reconnectInterval = 10000;
 


[3/3] camel git commit: Log

Posted by da...@apache.org.
Log


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

Branch: refs/heads/master
Commit: 9ccce32ecef253bbfeda2c86059d3691e6dbf1a5
Parents: 463b9b7
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Jul 9 11:36:56 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Jul 9 11:36:56 2015 +0200

----------------------------------------------------------------------
 components/camel-netty4/src/test/resources/log4j.properties | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/9ccce32e/components/camel-netty4/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/components/camel-netty4/src/test/resources/log4j.properties b/components/camel-netty4/src/test/resources/log4j.properties
index d5da94c..bda89ad 100644
--- a/components/camel-netty4/src/test/resources/log4j.properties
+++ b/components/camel-netty4/src/test/resources/log4j.properties
@@ -18,7 +18,7 @@
 #
 # The logging properties used for eclipse testing, We want to see debug output on the console.
 #
-log4j.rootLogger=INFO, out
+log4j.rootLogger=INFO, file
 
 # uncomment the following to enable camel debugging
 #log4j.logger.org.apache.camel.component.netty4=TRACE