连接池实现方式
背景:
一次线上问题,发现句柄数非常高,经过定位,发现其实是有方法创建会话,没有关闭导致的。
基于此,在代码里面及时关闭会话,后来想了一下,还是要做一个连接池做管理比较稳妥。
以下是记录了一个方式
实现方式
package com.file.service.utils; import com.jcraft.jsch.ChannelSftp; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.integration.file.remote.session.Session; import org.springframework.integration.file.remote.session.SessionFactory; import org.springframework.stereotype.Component; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicInteger; /** * Sftp会话连接池 * * @author HuangXiaoFeng * @version $id:SftpSessionPool.java, v 1 2022/3/11 09:58:33, HuangXiaoFeng Exp $ */ @Component public class SftpSessionPool { private SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory; @Autowired public void setSftpSessionFactory(SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory) { this.sftpSessionFactory = sftpSessionFactory; } private ConcurrentLinkedQueue<Session> sessionPool = new ConcurrentLinkedQueue<>(); // sftp 连接池大小 @Value("${sftp.poolSize:30}") private int poolSize; private AtomicInteger currentSize = new AtomicInteger(0); /** * 初如化session连接池 */ private synchronized void createSession() { Session session = sftpSessionFactory.getSession(); sessionPool.add(session); currentSize.incrementAndGet(); } /** * 获取一个session */ public synchronized Session getSession() { if (sessionPool.size() > 0) { Session session = sessionPool.poll(); Object proxyInstance = Proxy.newProxyInstance(SftpSessionPool.class.getClassLoader(), session.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (!"close".equals(method.getName())) { return method.invoke(session, args); } else { sessionPool.add(session); } return null; } }); return (Session) proxyInstance; } else { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } if (sessionPool.size() == 0 && currentSize.intValue() < poolSize) { createSession(); } return getSession(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南