使用Apache commons-pool2实现高效的FTPClient连接池的方法

一. 连接池概述

​       频繁的建立和关闭连接,会极大的降低系统的性能,而连接池会在初始化的时候会创建一定数量的连接,每次访问只需从连接池里获取连接,使用完毕后再放回连接池,并不是直接关闭连接,这样可以保证程序重复使用同一个连接而不需要每次访问都建立和关闭连接, 从而提高系统性能。有些对象的创建开销是比较大的,比如数据库连接等。为了减少频繁创建、销毁对象带来的性能消耗,我们可以利用对象池的技术来实现对象的复用。对象池提供了一种机制,它可以管理对象池中对象的生命周期,提供了获取和释放对象的方法,可以让客户端很方便的使用对象池中的对象。

二. commons-pool2介绍

2.1 pool2的引入

<dependency>
	<groupId>commons-lang</groupId>
	<artifactId>commons-lang</artifactId>
	<version>2.5</version>
</dependency>
	<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
	<version>2.8.0</version>
</dependency>
<dependency>
	<groupId>commons-net</groupId>
	<artifactId>commons-net</artifactId>
	<version>3.6</version>
</dependency>
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.6</version>
</dependency>

2.2 pool2的组成

PooledObject(池化对象) PooledObjectFactory(对象工厂) ObjectPool (对象池)

对应为: FTPClient(池化对象) FTPClientFactory(对象工厂) FTPClientPool(对象池)

关系图:

关系图

三. 实现连接池

3.1 配置FtpClient

我们已经有现成的池化对象(FtpClient)了,只需要添加配置即可,FTPClientConfig【FTP连接配置类】


/** 
 * 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 com.tompai.ftp.pool;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

/**
 * @desc: demo
 * @name: FTPClientConfig.java
 * @author: tompai
 * @email:liinux@qq.com
 * @createTime: 2019年12月31日 下午12:53:48
 * @history:
 * @version: v1.0
 */

public class FTPClientConfig extends GenericObjectPoolConfig<FTPClient> {

	private String host;// 主机名
	private int port = 21;// 端口
	private String username;// 用户名
	private String password;// 密码

	private int connectTimeOut = 5000;// ftp 连接超时时间 毫秒
	private String controlEncoding = "utf-8";
	private int bufferSize = 1024;// 缓冲区大小
	private int fileType = 2;// 传输数据格式 2表binary二进制数据
	private int dataTimeout = 120 * 1000;
	private boolean useEPSVwithIPv4 = false;
	private boolean passiveMode = true;// 是否启用被动模式
	private int threadNum=1;//开启线程数
	private int transferFileType=FTPClient.BINARY_FILE_TYPE;//传输文件类型
	private boolean renameUploaded=false;//是否上传文件重命名;
	private int retryTimes=3;//重试次数

	public String getHost() {

		return host;
	}

	public void setHost(String host) {

		this.host = host;
	}

	public int getPort() {

		return port;
	}

	public void setPort(int port) {

		this.port = port;
	}

	public String getUsername() {

		return username;
	}

	public void setUsername(String username) {

		this.username = username;
	}

	public String getPassword() {

		return password;
	}

	public void setPassword(String password) {

		this.password = password;
	}

	public int getConnectTimeOut() {

		return connectTimeOut;
	}

	public void setConnectTimeOut(int connectTimeOut) {

		this.connectTimeOut = connectTimeOut;
	}

	public String getControlEncoding() {

		return controlEncoding;
	}

	public void setControlEncoding(String controlEncoding) {

		this.controlEncod
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
经过几天的琢磨,去看了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.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值