FTP Pool Tools ftp连接池

详细使用方法

依赖库

```
    <!-- ftpclient -->
       <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
    </dependency>
        
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.4.2</version>
    </dependency>
```

已经编译后的库

https://gitee.com/lliaoliao/ftppool/raw/master/bin/ftppool-1.0.jar

FTPPool  类里面有详细的调用情况

eg

```
public static void main(String[] args) throws Exception {
        FTPPoolConfig config = new FTPPoolConfig() ;
        config.setHost("192.168.1.20");
        config.setPort(21);
        config.setUsername("elen");
        config.setPassword("Elen@123");
        config.setPassiveMode("true");
        config.setClientTimeout(300000);
        config.setTransferFileType(2);
        //config.setMaxTotal(10);
        config.setMaxIdle(5);
        config.setMinIdle(1);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        config.setTestWhileIdle(true);
        config.setNumTestsPerEvictionRun(10);
        config.setTimeBetweenEvictionRunsMillis(60000);
        
        FTPClientFactory f = new FTPClientFactory() ;
        f.setConfig(config);
        final FTPPool pool = new FTPPool(config, f) ;
        for(int i = 20 ;i>0 ; i--){
            Thread t = new Thread(new Runnable() {
                
                @Override
                public void run() {
                   
                    try {
                        FTPClient ftp = pool.getPool().borrowObject() ;
                         System.out.println(Thread.currentThread().getName()+"--获得一个");
                        Thread.sleep(60000);
                        pool.getPool().returnObject(ftp);
                         System.out.println(Thread.currentThread().getName()+"取消一个");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                }
            },"name--"+i) ;
            t.start();
        }
       
        
        System.out.println("执行完毕");
        
        Thread.sleep(60000*20);
    
        
    }
```

spring 调用方式


```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd"
    default-autowire="byName">
    <bean id="ftpConfig" class="com.hatchsolution.ftp.pool.FTPPoolConfig" >
        <property name="host" value="192.168.1.20"/>
        <property name="port" value="21"/>
        <property name="username" value="elen"/>
        <property name="password" value="Elen@123"/>
        <property name="passiveMode" value="true"/>
        <property name="clientTimeout" value="300000"/>
        <property name="transferFileType" value="2"/>
        <property name="maxTotal" value="10"/>
        <property name="maxIdle" value="5"/>
        <property name="minIdle" value="2"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="numTestsPerEvictionRun" value="10"/>
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    </bean>    
     
    <bean id="ftpClientFactory" class="com.hatchsolution.ftp.factory.FTPClientFactory" >
        <constructor-arg name="config" ref="ftpConfig"></constructor-arg>
    </bean>
    
    <bean id = "ftpPool" class="com.hatchsolution.ftp.pool.impl.FTPPool" scope="singleton">
        <constructor-arg name="factory" ref="ftpClientFactory"></constructor-arg>    
        
    </bean>
    
    <bean id = "ftpUtils" class="com.hatchsolution.ftp.FTPUtils" scope="singleton">
        <property name="pools" ref="ftpPool"/>
    </bean>
    
    
    
    
    
    
</beans>
```

软件主页https://gitee.com/lliaoliao/ftppool.git
文档地址https://gitee.com/lliaoliao/ftppool.wiki.git
下载地址https://gitee.com/lliaoliao/ftppool/repository/archive/master.zip
授权协议 GPL         LGPL         AGPL         Apache         MIT         BSD         EPL         MPL         其它 

 

本链接池由济南汉驰信息科技有限公司编写本着开源原则欢迎探讨。

转载于:https://my.oschina.net/u/263414/blog/1579509

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经过几天的琢磨,去看了csdn上一位大牛的数据库的连接池实现方案,从中感悟很多,感谢这位大神。 让我才能有信心去坚持下去。也不知道写的好不好··不好的话,大家指出。但是我是努力去做了,这一个过程,很享受,大家互相学习吧~ 其实ftp连接池跟数据库连接池的原理是差不多的,不同的是ftp连接池有个连接时间的限制,如果你没设置的话,它的默认连接服务器的时间是0,所以我们要合理的设置它的服务器的时间,ftp.setConnectTimeout(5000);在这里设置了它的时间是5s。 写ftp连接池的目的就是合理的利用资源,本文的目的是在初始的时候,创建10个Ftp连接,放到一个队列中去,当多个用户同时去下载ftp上的文件的时候,就会从队列中取,若当前的队列中存在着空闲的连接,就获取该ftp的连接,并设置此连接为忙的状态,否则就在创建新的连接到连接池中去(有最大的连接池数的限制,不能超过这个连接数,超过的话,就会进入等待状态,直到其它连接释放连接),在执行下载操作的前对登录ftp时间进行判断。看是否超时,超时的话,就重新连接到ftp服务器,在这里我所做的操作就是,在开始创建ftp连接池的时候,记录下系统的当前时间,例如为:long beginTime=System.currentTimeMillis(),在取文件之前获得 当前系统的时间 long endTime=System.currentTimeMillis(),此时我们就可以获得系统登录ftp的时间time=endTime-beginTime,在此我们可以用time与ftp最大登录服务器时间(ftpPool.getConnection();)进行比较。 当然了,在操作完之后我们需要将所操作的连接池中的ftp设置为空闲状态。代码在文件中,为了测试,我本地自己创建了一个ftp服务器,创建ftp的方法,大家可以到网上查资料,我用的是Serv-U工具。傻瓜式的。所用到的jar包是commons-net2.0.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值