使用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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值