如何设置在groovy中从FTP服务器检索文件的时间限制?

时间:2018-04-16 09:40:44

标签: groovy ftp filemaker

如何设置从FTP服务器检索文件的时间限制。如果文件无法在时间内获取,则通过         一个名为超时异常的错误,带有错误代码。如果文件在timelimit内,则获得成功的消息。

      import org.apache.commons.net.ftp.FTPClient
      import org.apache.commons.net.ftp.FTP;
        import org.apache.commons.net.ftp.FTPFile;
       import groovy.io.FileType
       start()
       def start(){

        def store;
        int TENSECONDS  = 10*1000
        int THIRTYSECONDS = 30*1000
        def ftpClient = new FTPClient()
        try{

            ftp.setSoTimeout(THIRTYSECONDS)     
            ftp.setConnectTimeout(TENSECONDS)
            ftpClient.connect(server)
            ftp.setDefaultTimeout(TENSECONDS)
            ftpClient.login(user,pass)
            ftpClient.enterLocalPassiveMode()
            FTPFile[] fileslist = ftpClient.listFiles("/")
            FTPFile[] folderfileslist = ftpClient.listFiles("/sample")
            FTPFile[] folderfileslist1 = ftpClient.listFiles("/sample1")

            def allFiles = []; 
            for(int i=0; i<fileslist.length; i++){  
            String file_name = fileslist[i].getName()
            String file_timestamp = fileslist[i].getTimestamp().getTime()     
            String s = '|' + file_name+ '|' + '/' +file_name+'|'  +file_timestamp
            allFiles << s       
          }               
        return allFiles.join('\n')
  }
  catch?(Exception e){
    return e.printStackTrace(); 
  }
  finally{

       ftpClient.disconnect()
  }
}

1 个答案:

答案 0 :(得分:0)

你可以在groovy中尝试AST:

导入groovy.transform.TimedInterrupt

//timeout of 10 seconds
        @TimedInterrupt(value = 10L, unit = java.util.concurrent.TimeUnit.SECONDS) 
        def start(){
           […]
           try{
              //block that eventually may hang
           }catch(TimeOutException e){
              //handle the exception
           }
           […]
    }