我不确定我的问题是否存在于 Spring Integration 或 JCraft 的 Jsch 中。我在服务器上有一个应用程序,它使用 SFTP 和 session 池接收文件并将它们放置在另一台服务器上。我经常收到以下开头的权益痕迹:
java.lang.IllegalStateException: failed to create SFTP Session
Caused by: java.lang.IllegalStateException: failed to create SFTP Session
Caused by: java.lang.IllegalStateException: failed to connect...
并以下列异常(exception)之一结束:
Caused by: com.jcraft.jsch.JSchException: session is down
at com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:762)
或
Caused by: com.jcraft.jsch.JSchException: SSH_MSG_DISCONNECT: 11 This session would exceed your concurrent session limit. Please disconnect one or more of your existing sessions and try again.
at com.jcraft.jsch.Session.read(Session.java:996)
at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:198)
at com.jcraft.jsch.Session.connect(Session.java:463)
at com.jcraft.jsch.Session.connect(Session.java:183)
at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:263)
或
Caused by: com.jcraft.jsch.JSchException: channel is not opened.
at com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:765)
或
Caused by: com.jcraft.jsch.JSchException: SSH_MSG_DISCONNECT: 2 FlowSshPacketDecoder: received a higher-level packet before first key exchange is done
at com.jcraft.jsch.Session.read(Session.java:996)
at com.jcraft.jsch.Session.connect(Session.java:323)
at com.jcraft.jsch.Session.connect(Session.java:183)
at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:263)
... 45 more
这些异常通常成组发生。例如,我可能会发现 session 在一分钟内连续关闭三到四次。它们有时也是混合的,所以也许我会得到 channel 未打开且 session 已关闭的信息。有时,它可以完美运行,不会引发异常。
我的 bean :
<bean id="cachingSessionFactory"
class="org.springframework.integration.file.remote.session.CachingSessionFactory">
<constructor-arg ref="sftpSessionFactory" />
</bean>
<bean id="sftpSessionFactory"
class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<constructor-arg name="isSharedSession" value="true"/>
<property name="host" value="${SERVER}" />
<property name="port" value="22" />
<property name="user" value="${USER}" />
<property name="privateKey" value="${KEYPATH}" />
<property name="sessionConfig" ref="props"/>
</bean>
它连接到的服务器的 maxSessions 设置为 30,连接超时设置为 6 分钟。过去几天我一直在尝试调试这些错误,但似乎无法重现它们。
最佳答案
多线程是我的问题所在。我的应用程序将收到一次推送多个文件的请求(比如 20 个以上)。问题是 session 会过时,但现在有 20 多个线程正在尝试创建新 session 。它所连接的服务器一次只允许 6 个 sftp 连接。
我的解决方案是通过添加来防止 session 断开
<property name="serverAliveInterval" value="60000" />
给我的 bean 。这可以防止连接变得陈旧,缺点是它始终处于连接状态(占用资源)。我想如果我想解决线程问题,我必须删除 Spring 并直接使用 Jsch。
关于java - 使用 Spring Integration 时抛出不同的 JSchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32788255/