//池对象
package com.starcoder.beans;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Session;
import com.starcoder.config.SftpClientConfig;
import lombok.Data;
/**
* @ClassName: SftpClient
* @Description:
* @Author wuzegao
* @Date 2022/10/9
* @Version 1.0
*/
@Data
public class SftpClient {
private ChannelSftp channelSftp;
private Session session;
private SftpClientConfig sftpClientConfig;
}
// 对象池
package com.starcoder.beans;
import com.starcoder.config.SftpClientPoolConfig;
import com.starcoder.factory.SftpClientFactory;
import lombok.Data;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
/**
* @ClassName: SftpClientPool
* @Description:
* @Author wuzegao
* @Date 2022/10/9
* @Version 1.0
*/
@Data
public class SftpClientPool implements ObjectPool<SftpClient> {
private SftpClientPoolConfig poolConfig;
private SftpClientFactory factory;
private GenericObjectPool<SftpClient> pool;
public SftpClientPool(SftpClientPoolConfig poolConfig, SftpClientFactory factory) {
this.poolConfig = poolConfig;
this.factory = factory;
this.pool = new GenericObjectPool<SftpClient>(factory,getConfig(poolConfig));
}
private GenericObjectPoolConfig<SftpClient> getConfig(SftpClientPoolConfig poolConfig) {
GenericObjectPoolConfig<SftpClient> config = new GenericObjectPoolConfig<>();
config.setMinIdle(poolConfig.getMinIdle());
config.setMaxIdle(poolConfig.getMaxIdle());
config.setMaxTotal(poolConfig.getMaxTotal());
config.setTestOnBorrow(poolConfig.isTestOnBorrow());
config.setTestOnReturn(poolConfig.isTestOnReturn());
config.setTestWhileIdle(poolConfig.isTestWhileIdle());
return config;
}
@Override
public void addObject() throws Exception {
pool.addObject();
}
@Override
public SftpClient borrowObject() throws Exception {
return pool.borrowObject();
}
@Override
public void clear(){
pool.clear();
}
@Override
public void close() {
pool.close();
}
@Override
public int getNumActive() {
return pool.getNumActive();
}
@Override
public int getNumIdle() {
return pool.getNumIdle();
}
@Override
public void invalidateObject(SftpClient sftpClient) throws Exception {
pool.invalidateObject(sftpClient);
}
@Override
public void returnObject(SftpClient sftpClient) {
pool.returnObject(sftpClient);
}
}
//对象配置
package com.starcoder.config;
import lombok.Data;
/**
* @ClassName: SftpConfig
* @Description:
* @Author wuzegao
* @Date 2022/10/9
* @Version 1.0
*/
@Data
public class SftpClientConfig{
/**
* 主机名
*/
private String sftpname;
/**
* 主机ip
*/
private String host;
/**
* 端口号
*/
private int port;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 路径
*/
private String path;
/**
* 连接sftp服务器,默认超时时间30000
*/
private int timeout = 30000;
}
// 池配置
package com.starcoder.config;
import com.starcoder.beans.SftpClient;
import lombok.Data;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
/**
* @ClassName: SftpClientPoolConfig
* @Description:
* @Author wuzegao
* @Date 2022/10/10
* @Version 1.0
*/
@Data
public class SftpClientPoolConfig extends GenericKeyedObjectPoolConfig<SftpClient> {
private int maxTotal;
private int maxIdle;
private int minIdle;
private boolean lifo;
private boolean fairness;
private long maxWaitMillis;
private long minEvictableIdleTimeMillis;
private long evictorShutdownTimeoutMillis;
private long softMinEvictableIdleTimeMillis;
private int numTestsPerEvictionRun;
private String evictionPolicyClassName;
private boolean testOnCreate;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean testWhileIdle;
private long timeBetweenEvictionRunsMillis;
private boolean blockWhenExhausted;
private boolean jmxEnabled;
private String jmxNamePrefix;
private String jmxNameBase;
/**
* 获取连接重试次数
*/
private int retryCount;
}
// 工厂类
package com.starcoder.factory;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.starcoder.beans.SftpClient;
import com.starcoder.config.SftpClientConfig;
import com.starcoder.config.SftpClientPoolConfig;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import java.util.Properties;
/**
* @ClassName: SftpClientFactory
* @Description:
* @Author wuzegao
* @Date 2022/10/10
* @Version 1.0
*/
@Data
public class SftpClientFactory extends BasePooledObjectFactory<SftpClient> {
private SftpClientConfig clientConfig;
private SftpClientPoolConfig poolConfig;
@Override
public SftpClient create() throws Exception {
SftpClient sftpClient = null;
ChannelSftp channel = null;
// 用户名密码不能为空
if (StringUtils.isBlank(clientConfig.getUsername()) || StringUtils.isBlank(clientConfig.getPassword())) {
return null;
}
JSch jsch = new JSch();
Session sshSession = jsch.getSession(clientConfig.getUsername(), clientConfig.getHost(), clientConfig.getPort());
sshSession.setPassword(clientConfig.getPassword());
Properties sshConfig = new Properties();
// “StrictHostKeyChecking”如果设置成“yes”,ssh就不会自动把计算机的密匙加入“$HOME/.ssh/known_hosts”文件,并且一旦计算机的密匙发生了变化,就拒绝连接。
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
channel = (ChannelSftp) sshSession.openChannel("sftp");
channel.connect();
sftpClient = new SftpClient();
sftpClient.setChannelSftp(channel);
sftpClient.setSession(sshSession);
sftpClient.setSftpClientConfig(clientConfig);
return sftpClient;
}
@Override
public PooledObject<SftpClient> wrap(SftpClient sftpClient) {
return new DefaultPooledObject<>(sftpClient);
}
}
SFTP连接池管理
最新推荐文章于 2024-07-14 00:11:22 发布