Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 92023 invoked from network); 23 Jul 2007 23:44:26 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 Jul 2007 23:44:26 -0000 Received: (qmail 79068 invoked by uid 500); 23 Jul 2007 23:44:24 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 79020 invoked by uid 500); 23 Jul 2007 23:44:23 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 79009 invoked by uid 99); 23 Jul 2007 23:44:23 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jul 2007 16:44:23 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (herse.apache.org: local policy includes SPF record at spf.trusted-forwarder.org) Received: from [205.158.62.67] (HELO webmail-outgoing.us4.outblaze.com) (205.158.62.67) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jul 2007 16:44:20 -0700 Received: from unknown (unknown [192.168.8.90]) by webmail-outgoing.us4.outblaze.com (Postfix) with QMQP id 9943D1800129 for ; Mon, 23 Jul 2007 23:41:48 +0000 (GMT) X-OB-Received: from unknown (205.158.62.49) by wfilter2.us4.outblaze.com; 23 Jul 2007 23:41:48 -0000 Received: by ws1-1.us4.outblaze.com (Postfix, from userid 1001) id 192F71BF28D; Mon, 23 Jul 2007 23:41:43 +0000 (GMT) Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" MIME-Version: 1.0 From: "Oscar Usifer" To: commons-user@jakarta.apache.org Date: Mon, 23 Jul 2007 18:41:43 -0500 Subject: [commons-net] How to add timeouts to FTP client connections? Received: from [66.17.52.1] by ws1-1.us4.outblaze.com with http for oscaruser@programmer.net; Mon, 23 Jul 2007 18:41:43 -0500 X-Originating-Ip: 66.17.52.1 X-Originating-Server: ws1-1.us4.outblaze.com Message-Id: <20070723234143.192F71BF28D@ws1-1.us4.outblaze.com> X-Virus-Checked: Checked by ClamAV on apache.org Folks, I am writing an ftp client that uploads a file to more than one (many) ftp = servers simultaneously. I want to add some fault tolerance so that if one s= erver goes offline during the transfer, the upload is completed to the runn= ing servers. I've tried to capture this situation by blocking packets (via = iptables) on one ftp server, and seeing if it triggers a timeout condition/= exception (from setting setSoTimeout) in the ftp client, but unfortuantley = the connection only hangs. Note I am using rwinston's commons-net-ftp-2.0.0= -SNAPSHOT.jar. The code below is a command line example of the ftp client. = Any suggestions? Thanks, OSC import java.io.OutputStream; import org.apache.commons.net.ftp.FTPClient; public class FTPServer { =A0=A0 boolean online =3D true; =A0=A0 FTPClient client =3D null; =A0=A0 String hostname =3D null; =A0=A0 OutputStream os =3D null; } import java.io.FileInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Random; import org.apache.commons.net.PrintCommandListener; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPConnectionClosedException; import org.apache.commons.net.ftp.FTPReply; public class Fdb { public static final String USAGE =3D "Usage: Fdb \n" + "\nUploads local file to remote ftp servers in binary transfer mode.\= n"; public static void main(String[] args) { String[] ftphost =3D { "filedb1.test", "filedb2.test"}; FTPServer[] ftpsvr =3D new FTPServer[ftphost.length]; File fl =3D null; FileInputStream is =3D null; if (args.length =3D=3D 0) { System.out.println(USAGE); System.exit(-1); } try { fl =3D new File(args[0]); is =3D new FileInputStream(fl); } catch (Exception e) { System.exit(-1); } // data type initialization for (int i =3D 0; i < ftpsvr.length; ++i) { ftpsvr[i] =3D new FTPServer(); ftpsvr[i].client =3D new FTPClient(); ftpsvr[i].client.setConnectTimeout(1000); // one sec timeout try { ftpsvr[i].client.setSoTimeout(1000); } catch(Exception e) {} ftpsvr[i].client.addProtocolCommandListener(new PrintCommandListen= er( new PrintWriter(System.out))); ftpsvr[i].hostname =3D ftphost[i]; } // ftp connections for (int i =3D 0; i < ftpsvr.length; i++) { try { ftpsvr[i].client.connect(ftpsvr[i].hostname); if (FTPReply.isPositiveCompletion(ftpsvr[i].client.getReplyCode= ())) { System.out.println(ftpsvr[i].hostname + " ftp connection"); ftpsvr[i].online =3D true; System.out.println(ftpsvr[i].hostname + " marked server onli= ne"); } else { System.out.println(ftpsvr[i].hostname + " no ftp connection"= ); System.out.println(ftpsvr[i].hostname + " marked server offl= ine"); } } catch (Exception e) { ftpsvr[i].online =3D false; } } // connection negotiation for (int i =3D 0; i < ftpsvr.length; i++) { if (ftpsvr[i].online) { ftpsvr[i].client.enterLocalPassiveMode(); try { ftpsvr[i].client.login("ftp", ""); ftpsvr[i].client.setFileType(FTPClient.BINARY_FILE_TYPE); ftpsvr[i].client.changeWorkingDirectory("/pub/incoming"); // added random number for testing purposes ftpsvr[i].os =3D ftpsvr[i].client.storeFileStream((new Rando= m()).nextInt(1000) + args[0]); } catch (java.io.IOException e) { ftpsvr[i].online =3D false; } } } // Writing input byte streams to ftp server destinations byte buf[] =3D new byte[8192]; int bytesRead =3D 0; try { bytesRead =3D is.read(buf); } catch(Exception e) {} System.out.println("writing data"); while (bytesRead !=3D -1) { // write data out to ftp servers for (int i =3D 0; i < ftpsvr.length; i++) { try { if (ftpsvr[i].online) { if (FTPReply.isPositivePreliminary(ftpsvr[i].client.getRe= plyCode()) && ftpsvr[i].os !=3D null) { System.out.print("."); ftpsvr[i].os.write(buf, 0, bytesRead); } else { System.out.println("non - positive reply on " + ftpsvr= [i].hostname); } } } catch (Exception e1) { // here we're assuming any exception was generated by // an I/O fault related to writing to the ftp server, // thus the server is offline System.out.println("**FAULT writing to " + ftpsvr[i].hostnam= e); ftpsvr[i].online =3D false; } } try { bytesRead =3D is.read(buf); } catch (Exception e) {} } System.out.println("done."); try { is.close(); } catch (Exception e) {} for (int i =3D 0; i < ftpsvr.length; i++) { if (ftpsvr[i].online) { System.out.println("closing output stream " + ftpsvr[i].hostname= ); try { ftpsvr[i].os.close(); ftpsvr[i].client.completePendingCommand(); ftpsvr[i].client.logout(); ftpsvr[i].client.disconnect(); } catch (Exception e) {} } } } } --=20 We've Got Your Name at http://www.mail.com! Get a FREE E-mail Account Today - Choose From 100+ Domains --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org