camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r791486 - in /camel/trunk/components/camel-ftp/src: main/java/org/apache/camel/component/file/remote/ test/java/org/apache/camel/component/file/remote/
Date Mon, 06 Jul 2009 14:16:42 GMT
Author: davsclaus
Date: Mon Jul  6 14:16:42 2009
New Revision: 791486

URL: http://svn.apache.org/viewvc?rev=791486&view=rev
Log:
CAMEL-1801: ftp component will now automatic try to reconnect in case the connection could
not be established. Added options to control this behavior. Will try 3 times waiting 1 sec
by default.

Added:
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtoReconnectAttemptServerStoppedTest.java
  (with props)
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpReconnectAttemptUnknownHostTest.java
  (contents, props changed)
      - copied, changed from r791478, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFileToFtpTest.java
Modified:
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java?rev=791486&r1=791485&r2=791486&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
(original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
Mon Jul  6 14:16:42 2009
@@ -45,7 +45,7 @@
 public class FtpOperations implements RemoteFileOperations<FTPFile> {
     private static final transient Log LOG = LogFactory.getLog(FtpOperations.class);
     private final FTPClient client;
-    private GenericFileEndpoint endpoint;
+    private RemoteFileEndpoint endpoint;
 
     public FtpOperations() {
         this.client = new FTPClient();
@@ -56,19 +56,19 @@
     }
 
     public void setEndpoint(GenericFileEndpoint endpoint) {
-        this.endpoint = endpoint;
+        this.endpoint = (RemoteFileEndpoint) endpoint;
     }
 
-    public boolean connect(RemoteFileConfiguration config) throws GenericFileOperationFailedException
{
+    public boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException
{
         if (LOG.isTraceEnabled()) {
             LOG.trace("Connecting using FTPClient: " + client);
         }
 
-        String host = config.getHost();
-        int port = config.getPort();
-        String username = config.getUsername();
+        String host = configuration.getHost();
+        int port = configuration.getPort();
+        String username = configuration.getUsername();
 
-        FtpConfiguration ftpConfig = (FtpConfiguration) config;
+        FtpConfiguration ftpConfig = (FtpConfiguration) configuration;
 
         if (ftpConfig.getFtpClientConfig() != null) {
             LOG.trace("Configuring FTPClient with config: " + ftpConfig.getFtpClientConfig());
@@ -76,16 +76,41 @@
         }
 
         if (LOG.isTraceEnabled()) {
-            LOG.trace("Connecting to " + config.remoteServerInformation());
+            LOG.trace("Connecting to " + configuration.remoteServerInformation());
         }
-        try {
-            client.connect(host, port);
-        } catch (IOException e) {
-            throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(),
e.getMessage(), e);
+
+        boolean connected = false;
+        int attempt = 0;
+
+        while (!connected) {
+            try {
+                if (LOG.isTraceEnabled() && attempt > 0) {
+                    LOG.trace("Reconnect attempt #" + attempt + " connecting to + " + configuration.remoteServerInformation());
+                }
+                client.connect(host, port);
+                // yes we could connect
+                connected = true;
+            } catch (Exception e) {
+                GenericFileOperationFailedException failed = new GenericFileOperationFailedException(client.getReplyCode(),
client.getReplyString(), e.getMessage(), e);
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("Could not connect due: " + failed.getMessage());
+                }
+                attempt++;
+                if (attempt > endpoint.getMaximumReconnectAttempts()) {
+                    throw failed;
+                }
+                if (endpoint.getReconnectDelay() > 0) {
+                    try {
+                        Thread.sleep(endpoint.getReconnectDelay());
+                    } catch (InterruptedException e1) {
+                        // ignore
+                    }
+                }
+            }
         }
 
         // must enter passive mode directly after connect
-        if (config.isPassiveMode()) {
+        if (configuration.isPassiveMode()) {
             LOG.trace("Using passive mode connections");
             client.enterLocalPassiveMode();
         }
@@ -94,9 +119,9 @@
             boolean login;
             if (username != null) {
                 if (LOG.isTraceEnabled()) {
-                    LOG.trace("Attempting to login user: " + username + " using password:
" + config.getPassword());
+                    LOG.trace("Attempting to login user: " + username + " using password:
" + configuration.getPassword());
                 }
-                login = client.login(username, config.getPassword());
+                login = client.login(username, configuration.getPassword());
             } else {
                 LOG.trace("Attempting to login anonymous");
                 login = client.login("anonymous", null);
@@ -107,7 +132,7 @@
             if (!login) {
                 throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString());
             }
-            client.setFileType(config.isBinary() ? FTPClient.BINARY_FILE_TYPE : FTPClient.ASCII_FILE_TYPE);
+            client.setFileType(configuration.isBinary() ? FTPClient.BINARY_FILE_TYPE : FTPClient.ASCII_FILE_TYPE);
         } catch (IOException e) {
             throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(),
e.getMessage(), e);
         }

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java?rev=791486&r1=791485&r2=791486&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
(original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
Mon Jul  6 14:16:42 2009
@@ -30,6 +30,9 @@
  */
 public abstract class RemoteFileEndpoint<T> extends GenericFileEndpoint<T> {
 
+    private int maximumReconnectAttempts = 3;
+    private long reconnectDelay = 1000;
+
     public RemoteFileEndpoint() {
         // no args constructor for spring bean endpoint configuration
     }
@@ -139,4 +142,19 @@
         return name.startsWith("/");
     }
 
+    public int getMaximumReconnectAttempts() {
+        return maximumReconnectAttempts;
+    }
+
+    public void setMaximumReconnectAttempts(int maximumReconnectAttempts) {
+        this.maximumReconnectAttempts = maximumReconnectAttempts;
+    }
+
+    public long getReconnectDelay() {
+        return reconnectDelay;
+    }
+
+    public void setReconnectDelay(long reconnectDelay) {
+        this.reconnectDelay = reconnectDelay;
+    }
 }

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java?rev=791486&r1=791485&r2=791486&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
(original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
Mon Jul  6 14:16:42 2009
@@ -52,37 +52,63 @@
  */
 public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry> {
     private static final transient Log LOG = LogFactory.getLog(SftpOperations.class);
-    private GenericFileEndpoint endpoint;
+    private RemoteFileEndpoint endpoint;
     private ChannelSftp channel;
     private Session session;
 
     public void setEndpoint(GenericFileEndpoint endpoint) {
-        this.endpoint = endpoint;
+        this.endpoint = (RemoteFileEndpoint) endpoint;
     }
 
     public boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException
{
-        try {
-            if (isConnected()) {
-                // already connected
-                return true;
-            }
-            if (channel == null || !channel.isConnected()) {
-                if (session == null || !session.isConnected()) {
-                    LOG.trace("Session isn't connected, trying to recreate and connect.");
-                    session = createSession(configuration);
-                    session.connect();
-                }
-                LOG.trace("Channel isn't connected, trying to recreate and connect.");
-                channel = (ChannelSftp) session.openChannel("sftp");
-                channel.connect();
-                LOG.info("Connected to " + configuration.remoteServerInformation());
-            }
-
+        if (isConnected()) {
+            // already connected
             return true;
+        }
+
+        boolean connected = false;
+        int attempt = 0;
+
+        while (!connected) {
+            try {
+                if (LOG.isTraceEnabled() && attempt > 0) {
+                    LOG.trace("Reconnect attempt #" + attempt + " connecting to + " + configuration.remoteServerInformation());
+                }
 
-        } catch (JSchException e) {
-            throw new GenericFileOperationFailedException("Cannot connect to " + configuration.remoteServerInformation(),
e);
+                if (channel == null || !channel.isConnected()) {
+                    if (session == null || !session.isConnected()) {
+                        LOG.trace("Session isn't connected, trying to recreate and connect.");
+                        session = createSession(configuration);
+                        session.connect();
+                    }
+                    LOG.trace("Channel isn't connected, trying to recreate and connect.");
+                    channel = (ChannelSftp) session.openChannel("sftp");
+                    channel.connect();
+                    LOG.info("Connected to " + configuration.remoteServerInformation());
+                }
+
+                // yes we could connect
+                connected = true;
+            } catch (Exception e) {
+                GenericFileOperationFailedException failed = new GenericFileOperationFailedException("Cannot
connect to " + configuration.remoteServerInformation(), e);
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("Could not connect due: " + failed.getMessage());
+                }
+                attempt++;
+                if (attempt > endpoint.getMaximumReconnectAttempts()) {
+                    throw failed;
+                }
+                if (endpoint.getReconnectDelay() > 0) {
+                    try {
+                        Thread.sleep(endpoint.getReconnectDelay());
+                    } catch (InterruptedException e1) {
+                        // ignore
+                    }
+                }
+            }
         }
+
+        return true;
     }
 
     protected Session createSession(final RemoteFileConfiguration configuration) throws JSchException
{

Added: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtoReconnectAttemptServerStoppedTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtoReconnectAttemptServerStoppedTest.java?rev=791486&view=auto
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtoReconnectAttemptServerStoppedTest.java
(added)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtoReconnectAttemptServerStoppedTest.java
Mon Jul  6 14:16:42 2009
@@ -0,0 +1,65 @@
+/**
+ * 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.file.remote;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class FtoReconnectAttemptServerStoppedTest extends FtpServerTestSupport {
+
+    private String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/reconnect?password=admin&maximumReconnectAttempts=2&reconnectDelay=500";
+    }
+
+    @Test
+    public void testFromFileToFtp() throws Exception {
+        // put a file in the folder (do not use ftp as we then will connect)
+        template.sendBodyAndHeader("file://./res/home/reconnect", "Hello World", Exchange.FILE_NAME,
"hello.txt");
+        // suspect serve so we cannot connect
+        ftpServer.suspend();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(0);
+
+        // let it run a little
+        Thread.sleep(3000);
+
+        assertMockEndpointsSatisfied();
+
+        // resume the server so we can connect
+        ftpServer.resume();
+
+        mock.reset();
+        mock.expectedMessageCount(1);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from(getFtpUrl()).to("mock:result");
+            }
+        };
+    }
+
+}

Propchange: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtoReconnectAttemptServerStoppedTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtoReconnectAttemptServerStoppedTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpReconnectAttemptUnknownHostTest.java
(from r791478, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFileToFtpTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpReconnectAttemptUnknownHostTest.java?p2=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpReconnectAttemptUnknownHostTest.java&p1=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFileToFtpTest.java&r1=791478&r2=791486&rev=791486&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFileToFtpTest.java
(original)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpReconnectAttemptUnknownHostTest.java
Mon Jul  6 14:16:42 2009
@@ -23,16 +23,19 @@
 /**
  * @version $Revision$
  */
-public class FromFileToFtpTest extends FtpServerTestSupport {
+public class FtpReconnectAttemptUnknownHostTest extends FtpServerTestSupport {
 
     private String getFtpUrl() {
-        return "ftp://admin@localhost:" + getPort() + "/tmp2/camel?password=admin&consumer.initialDelay=5000";
+        return "ftp://admin@doesnotexisthost:" + getPort() + "/reconnect?password=admin";
     }
 
     @Test
     public void testFromFileToFtp() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMessageCount(2);
+        mock.expectedMessageCount(0);
+
+        // let it run a little
+        Thread.sleep(3000);
 
         assertMockEndpointsSatisfied();
     }
@@ -40,11 +43,9 @@
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                from("file:src/main/data?noop=true&consumer.delay=5000").to(getFtpUrl());
-
                 from(getFtpUrl()).to("mock:result");
             }
         };
     }
 
-}
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpReconnectAttemptUnknownHostTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpReconnectAttemptUnknownHostTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message