java 多ftp连接池,实现一个ftp连接池

背景

在此之前,本人曾经写过两篇总结文章,《封装一个FTP工具类》和《使用commons-pool管理FTP连接》,相应地造了两个小项目轮子。两个项目之间有相同点,也有不同的侧重点:都是使用commons-net和commons-pool库构建的,前者最大的特点是使用了模板方法设计模式,借鉴了JdbcTemplate的实现,造了上传下载工具类方法;后者最大的特点是使用AutoCloseable的方式去设计FTP连接(FTPConnection),借鉴的是BasicDataSource的实现,重点是连接的管理重用。

第一个项目的问题是和Spring绑定了,且底层使用了KeyObjectPool模式,实现上比较简陋,不利于扩展;第二个项目倒不是存在很大的问题,只是不够简洁,还有改进的空间,但是由于一开始设计时上考虑太多,束缚了自己,项目无法演进。不难看出,本人关心的是项目的扩展性,在项目上能不能实践一些知识点,是一种练笔的心态;因此,姑且将这个新轮子取名ftpcp2;

重构

本次重构的想法酝酿了很久了,总结过去的经验教训,这次重构本着一种alpha的心态,保持代码思路简洁,一点点地加功能,没有考虑API是否稳定,将近一个月的时间收获不少,一些是关于代码架构设计的,一些是关于工具链的;

架构思路

设计一个Manager类,底层是一个连接池,能够返回一个封装后的Client对象,这个Client对象close时并不是真正的关闭连接,而是回收到连接池,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用SpringBoot实现FTP连接池的步骤: 1.在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency> ``` 2.创建FTP连接池配置类FtpClientPoolConfig,配置连接池的最大、最小连接数、连接超时时间等参数。 ```java @Configuration @ConfigurationProperties(prefix = "ftp.pool") @Data public class FtpClientPoolConfig { private int maxTotal; private int maxIdle; private int minIdle; private long maxWaitMillis; private boolean testOnBorrow; private boolean testOnReturn; private boolean testWhileIdle; private long timeBetweenEvictionRunsMillis; private int numTestsPerEvictionRun; private long minEvictableIdleTimeMillis; } ``` 3.创建FTP连接池FtpClientPool,使用Apache Commons Pool2实现连接池。 ```java @Component public class FtpClientPool extends GenericObjectPool<FtpClient> { public FtpClientPool(FtpClientFactory factory, FtpClientPoolConfig config) { super(factory, new GenericObjectPoolConfig()); this.setMaxTotal(config.getMaxTotal()); this.setMaxIdle(config.getMaxIdle()); this.setMinIdle(config.getMinIdle()); this.setMaxWaitMillis(config.getMaxWaitMillis()); this.setTestOnBorrow(config.isTestOnBorrow()); this.setTestOnReturn(config.isTestOnReturn()); this.setTestWhileIdle(config.isTestWhileIdle()); this.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis()); this.setNumTestsPerEvictionRun(config.getNumTestsPerEvictionRun()); this.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis()); } } ``` 4.创建FTP连接池工厂类FtpClientFactory,用于创建FTP连接对象。 ```java @Component public class FtpClientFactory extends BasePooledObjectFactory<FtpClient> { private FtpClientProperties properties; public FtpClientFactory(FtpClientProperties properties) { this.properties = properties; } @Override public FtpClient create() throws Exception { FtpClient ftpClient = new FtpClient(); ftpClient.connect(properties.getHost(), properties.getPort()); ftpClient.login(properties.getUsername(), properties.getPassword()); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.setBufferSize(properties.getBufferSize()); ftpClient.setControlEncoding(properties.getEncoding()); ftpClient.enterLocalPassiveMode(); return ftpClient; } @Override public PooledObject<FtpClient> wrap(FtpClient ftpClient) { return new DefaultPooledObject<>(ftpClient); } @Override public void destroyObject(PooledObject<FtpClient> p) throws Exception { FtpClient ftpClient = p.getObject(); if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } @Override public boolean validateObject(PooledObject<FtpClient> p) { FtpClient ftpClient = p.getObject(); try { return ftpClient.sendNoOp(); } catch (IOException e) { return false; } } } ``` 5.创建FTP连接池属性类FtpClientProperties,用于配置FTP连接的相关参数。 ```java @ConfigurationProperties(prefix = "ftp") @Data public class FtpClientProperties { private String host; private int port; private String username; private String password; private int bufferSize; private String encoding; } ``` 6.在application.yml文件中配置FTP连接池的相关参数。 ```yaml ftp: host: ftp.example.com port: 21 username: username password: password bufferSize: 1024 encoding: UTF-8 ftp: pool: maxTotal: 10 maxIdle: 5 minIdle: 1 maxWaitMillis: 3000 testOnBorrow: true testOnReturn: false testWhileIdle: true timeBetweenEvictionRunsMillis: 60000 numTestsPerEvictionRun: -1 minEvictableIdleTimeMillis: 1800000 ``` 7.在需要使用FTP连接的地方,注入FtpClientPool对象,从连接池中获取FTP连接对象。 ```java @Service public class FtpService { @Autowired private FtpClientPool ftpClientPool; public void uploadFile(String remotePath, String fileName, InputStream inputStream) throws Exception { FtpClient ftpClient = ftpClientPool.borrowObject(); try { ftpClient.changeWorkingDirectory(remotePath); ftpClient.storeFile(fileName, inputStream); } finally { ftpClientPool.returnObject(ftpClient); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值