如果sftp未激活而没有关闭它们,则Spring Integration会创建新会话

时间:2014-02-19 15:27:07

标签: java spring spring-integration

我正在开发一个使用Spring Integration连接到多个SFTP服务器的项目。

要建立连接并获取所需文件,请使用以下代码:

<!-- Session factory for new SSH sessions for the server of *this* context -->
<bean id="sftpFactory"
    class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    <property name="host" value="${sftp.remote.host}" />
    <property name="port" value="${sftp.remote.port}" />
    <property name="user" value="${sftp.remote.username}" />
    <property name="password" value="${sftp.remote.password}" />
    <property name="privateKey"
        value="${sftp.remote.privatekey.path}"></property>
    <property name="privateKeyPassphrase"
        value="${sftp.remote.privatekey.passphrase}"></property>
</bean>

    <int-sftp:inbound-channel-adapter id="sftpInboundAdapter"
    session-factory="sftpFactory" remote-directory="${sftp.remote.directory}"
    charset="UTF-8"
    local-directory="file:${sftp.local.directory}/${sftp.remote.host}_${sftp.remote.port}"
    auto-create-local-directory="true" delete-remote-files="${sftp.remote.deleteSourceFiles}"
    channel="channelDelayAfterSftpDownload" filename-pattern="*.xml" local-filename-generator-expression="#this + '.' + T(class.to.generate.filename).createRandomUuid()">
    <si:poller fixed-rate="${sftp.remote.pollerRate}" time-unit="MILLISECONDS"
        max-messages-per-poll="100" />
</int-sftp:inbound-channel-adapter>

如果一切配置正确,代码可以正常工作。

我的问题如下: 如果建立了与SSH服务器的连接但在服务器上未启用SFTP,则会为每个轮询创建一个新会话。但是连接没有关闭,最终阻止服务器工作。

尝试连接时抛出的异常是:

ERROR o.s.i.handler.LoggingHandler - org.springframework.integration.MessagingException: Failed to obtain pooled item
at org.springframework.integration.util.SimplePool.getItem(SimplePool.java:170)
at org.springframework.integration.file.remote.session.CachingSessionFactory.getSession(CachingSessionFactory.java:83)
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:143)
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.receive(AbstractInboundFileSynchronizingMessageSource.java:146)
at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:111)
at org.springframework.integration.endpoint.AbstractTransactionSynchronizingPollingEndpoint.doPoll(AbstractTransactionSynchronizingPollingEndpoint.java:67)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:146)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:144)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:236)
at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:52)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:49)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:231)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:266)
at org.springframework.integration.file.remote.session.CachingSessionFactory$1.createForPool(CachingSessionFactory.java:55)
at org.springframework.integration.file.remote.session.CachingSessionFactory$1.createForPool(CachingSessionFactory.java:53)
at org.springframework.integration.util.SimplePool.doGetItem(SimplePool.java:180)
at org.springframework.integration.util.SimplePool.getItem(SimplePool.java:160)
... 22 more
Caused by: java.lang.IllegalStateException: failed to connect
at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:204)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:262)
... 26 more
Caused by: com.jcraft.jsch.JSchException: Session.connect: java.io.IOException: End of IO Stream Read
at com.jcraft.jsch.Session.connect(Session.java:507)
at com.jcraft.jsch.Session.connect(Session.java:158)
at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:196)
... 27 more

是否有必须设置的属性或我只是缺少某些东西?

提前致谢

2 个答案:

答案 0 :(得分:0)

请参阅以下Jira以供参考:

https://jira.springsource.org/browse/INT-1921

因此,如果您升级到最新的Snapshot版本,您的问题就会消失。

此外,现有2.0.4版本还有其他几种简单的解决方法可以解决您的问题。在我看来,最好的解决方案是在“int-sftp:inbound-channel-adapter”元素上明确指定“remote-file-separator”属性,例如:

远程文件的隔膜= “/”

或者,只要您确保“remote-directory”具有尾随路径分隔符,您也应该没问题(例如remote-directory =“/ poll /”)

如果这可以解决您的问题,请告诉我们。

答案 1 :(得分:0)

这看起来像是JSch客户端中的一个错误(如果session.connect()失败,他不会关闭套接字);但即使disconnect()失败,我们也可以添加一种解决方法来调用connect()

随意打开JIRA问题here