CapitalDecay
CapitalDecay

Reputation: 21

Different JSchException thrown when using Spring Integration

I'm unsure if my issue resides in Spring Integration or JCraft's Jsch. I have an application on a server that takes in files and places them on another server using SFTP and session pools. Ever so often I receive stake traces that start with:

     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...

and ends with one of the following exceptions:

    Caused by: com.jcraft.jsch.JSchException: session is down
    at com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:762)

or

    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)

or

Caused by: com.jcraft.jsch.JSchException: channel is not opened.
    at com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:765)

or

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 

These exceptions usually happen in groups. For example, I might get Session is down three or four times in a row within a minute. They sometimes are also mixed, so maybe I'll get channel is not open with a session is down. Sometime, it works perfectly without throwing an exception.

My 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>

The server it is connecting to has maxSessions set to 30 and a connection timeout of 6 minutes. I've spent the past couple of days trying to debug the errors, but can't seem to reproduce them.

Upvotes: 1

Views: 2193

Answers (1)

CapitalDecay
CapitalDecay

Reputation: 21

Multi-threading was the issue to my problem. My application would receive request to push multiple files at once (say 20+). The issue is that the session would be stale, but now 20+ threads are trying to create a new session. The server it was reaching out to only allowed 6 sftp connections at once.

My solution was to keep the session from disconnecting by adding

<property name="serverAliveInterval" value="60000" />

to my bean. This stops the connection from becoming stale, the down side is that it is always connected (taking up resources). I think if I want to resolve the threading issue, I would have to remove Spring and work with Jsch directly.

Upvotes: 1

Related Questions