设为首页 收藏本站
查看: 1361|回复: 0

[经验分享] FTP连接池

[复制链接]

尚未签到

发表于 2016-6-7 11:59:28 | 显示全部楼层 |阅读模式
  开发目的:使用FTP连接池来管理FTP连接,以避免不断的连接FTP造成性能下降。
  
  1、FTP连接池,建立一个java类FtpConnectionPooling:
  

package com.dripstone.ftp;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
/**
* <br>Title:FTP连接池
* <br>Description:FTP连接池及FTP连接池相应的操作
* <br>Author:张智研(zhangzhiyan@neusoft.com)
* <br>Date:2013-7-4
*/
public abstract class FtpConnectionPooling {
private static BlockingQueue<FtpClient> fqueue;
private static FtpClientInfo ftpClientInfo;
/**
* <br>Description:初始化连接池
* <br>Author:张智研(zhangzhiyan@neusoft.com)
* <br>Date:2013-7-4
* @param ftpClientInfo
*/
public static void init(FtpClientInfo info) {
ftpClientInfo = info;
fqueue = new PriorityBlockingQueue<FtpClient>(ftpClientInfo.getMaxConnects(),
new FtpClientComparator());// 初始化队列容量
FtpClient ftpClient;
for (int i = 0; i < ftpClientInfo.getMaxConnects(); i++) {
ftpClient = new FtpClient();
ftpClient.order = i;
fqueue.add(ftpClient);
}
}
public static FtpClientInfo getFtpClientInfo() {
return ftpClientInfo;
}
/**
* <br>Description:向线程池中添加FTPClient
* <br>Author:张智研(zhangzhiyan@neusoft.com)
* <br>Date:2013-7-4
* @param ftpClient
*/
public static boolean add(FtpClient ftpClient) {
boolean b = fqueue.contains(ftpClient);
if (!b)
return fqueue.add(ftpClient);
return true;
}
/**
* <br>Description:获取FTPClient,如果线程池为空,则等待到FtpClientInfo中所设置的超时时间
* <br>Author:张智研(zhangzhiyan@neusoft.com)
* <br>Date:2013-7-4
* @return
* @throws InterruptedException
*/
public static FtpClient poll() throws InterruptedException {
return fqueue.poll(ftpClientInfo.getTimeout(), ftpClientInfo.getTimeUnit());
}
/**
* <br>Description:获取FTPClient,如果线程池为空,则一直等待。
* <br>Author:张智研(zhangzhiyan@neusoft.com)
* <br>Date:2013-7-4
* @return
* @throws InterruptedException
*/
public static FtpClient take() throws InterruptedException {
return fqueue.take();
}
}

  2、FtpClientProxy类

package com.dripstone.ftp;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
/**
* <br>Title:FTPClient代理类
* <br>Description:负责FTPClient功能的代理
* <br>Author:张智研(zhangzhiyan@neusoft.com)
* <br>Date:2013-7-4
*/
public class FtpClientProxy {
private FtpClient ftpClient;
public FtpClientProxy() throws InterruptedException, SocketException, IOException {
ftpClient = FtpConnectionPooling.poll();
if (!ftpClient.isConnected()) {
FtpClientInfo info = FtpConnectionPooling.getFtpClientInfo();// 获取ftpClient信息
ftpClient.connect(info.getFtpIp(), info.getFtpPort());// 连接
ftpClient.login(info.getFtpUserName(), info.getFtpPassword());// 登陆
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);// 设置为二进制
}
}
/**
* <br>Description:释放ftpClient
* <br>Author:张智研(zhangzhiyan@neusoft.com)
* <br>Date:2013-7-4
* @return
*/
public boolean release() {
if (ftpClient == null)
return true;
boolean b = FtpConnectionPooling.add(ftpClient);
if (b)
ftpClient = null;
return b;
}
/**
* <br>Description:下载文件
* <br>Author:张智研(zhangzhiyan@neusoft.com)
* <br>Date:2013-7-4
* @param fileName
* @return
* @throws IOException
*/
public InputStream retrieveFileStream(String remote) throws IOException {
return ftpClient.retrieveFileStream(remote);
}
/**
* <br>Description:上传文件
* <br>Author:张智研(zhangzhiyan@neusoft.com)
* <br>Date:2013-7-4
* @param remote
* @param local
* @return
* @throws IOException
*/
public boolean storeFile(String remote, InputStream local) throws IOException {
return ftpClient.storeFile(remote, local);
}
/**
* <br>Description:获取本地端口
* <br>Author:张智研(zhangzhiyan@neusoft.com)
* <br>Date:2013-7-4
* @return
*/
public int getLocalPort() {
return ftpClient.getLocalPort();
}
}

  

3、FtpClient类

package com.dripstone.ftp;
import org.apache.commons.net.ftp.FTPClient;
public class FtpClient extends FTPClient {
public int order;
}

 4、FtpClientComparator类

package com.dripstone.ftp;
import java.util.Comparator;
public class FtpClientComparator implements Comparator<FtpClient> {
@Override
public int compare(FtpClient arg0, FtpClient arg1) {
return arg0.order - arg1.order;
}
}

 5、FtpClientInfo类

package com.dripstone.ftp;
import java.util.concurrent.TimeUnit;
public class FtpClientInfo {
private String ftpIp; // ftp的IP地址
private int ftpPort; // ftp的端口
private String ftpUserName; // ftp的用户名
private String ftpPassword; // ftp的密码
private int maxConnects; // 最大连接数
private long timeout; // 超时时间 ,默认60
private TimeUnit timeUnit;// 超时时间单位,默认为秒
public FtpClientInfo() {
timeout = 60;
timeUnit = TimeUnit.SECONDS;
}
public String getFtpIp() {
return ftpIp;
}
public void setFtpIp(String ftpIp) {
this.ftpIp = ftpIp;
}
public int getFtpPort() {
return ftpPort;
}
public void setFtpPort(int ftpPort) {
this.ftpPort = ftpPort;
}
public String getFtpUserName() {
return ftpUserName;
}
public void setFtpUserName(String ftpUserName) {
this.ftpUserName = ftpUserName;
}
public String getFtpPassword() {
return ftpPassword;
}
public void setFtpPassword(String ftpPassword) {
this.ftpPassword = ftpPassword;
}
public int getMaxConnects() {
return maxConnects;
}
public void setMaxConnects(int maxConnects) {
this.maxConnects = maxConnects;
}
public long getTimeout() {
return timeout;
}
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public TimeUnit getTimeUnit() {
return timeUnit;
}
public void setTimeUnit(TimeUnit timeUnit) {
this.timeUnit = timeUnit;
}
}

 6、测试类FtpConnectionPoolingTest

package com.dripstone.ftp;
import java.io.IOException;
public class FtpConnectionPoolingTest extends Thread {
private static int n = 0;
private static int m = 1;
public void run() {
try {
/********************业务代码调用样例*********************/
System.out.println(m++);
FtpClientProxy ftpClientProxy = new FtpClientProxy();
String t = "连接" + ++n;
System.out.println(t + "连接成功,端口号:" + ftpClientProxy.getLocalPort());
sleep(1000);
System.out.println(t + "释放连接");
ftpClientProxy.release();// 释放连接
/***************************************************/
} catch (InterruptedException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
/************需要在服务器启动时进行加载**************/
FtpClientInfo ftpClientInfo = new FtpClientInfo();
ftpClientInfo.setFtpIp("192.168.135.85");
ftpClientInfo.setFtpPassword("test");
ftpClientInfo.setFtpPort(21);
ftpClientInfo.setFtpUserName("test");
ftpClientInfo.setMaxConnects(20);
FtpConnectionPooling.init(ftpClientInfo);
/*******************************************/
/*************************************************************/
try {
FtpClientProxy ftpClientProxy1 = new FtpClientProxy();
System.out.println("本地端口" + ftpClientProxy1.getLocalPort());
ftpClientProxy1.release();
ftpClientProxy1.release();
FtpClientProxy ftpClientProxy2 = new FtpClientProxy();
System.out.println("本地端口" + ftpClientProxy2.getLocalPort());
ftpClientProxy2.release();
} catch (InterruptedException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*************************并发测试*******************************/
for (int i = 1; i <= 40; i++) {
FtpConnectionPoolingTest test = new FtpConnectionPoolingTest();
test.start();
}
}
}

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-227438-1-1.html 上篇帖子: JAVA实现FTP 下篇帖子: python sftp&ftp&ssh2
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表