使用 SFTP 出站网关获取输入流 [英] Use SFTP Outbound Gateway to Obtain Input Stream
问题描述
我正试图像这个问题一样处理一个文件:逐行读取远程文件
I am attempting to process a file exactly like this question: read a remote file line by line
这个问题的一个答案建议使用 RemoteFileTemplate 但我是尝试按照最后一条评论中的建议使用 -stream 选项回答.此外,这似乎是 -stream 选项的重点;获取流.
The one answer to this question suggests to use RemoteFileTemplate but I am attempting to use the -stream option as suggested in the last comment to the answer. Also, this kinda seems like the point of the -stream option; to get a stream.
我的实现成功获取了 InputStream 并启动了一个单独的线程在 BufferedReader 中处理.
My implementation successfully obtains the InputStream and kick off a separate thread to process in a BufferedReader.
这在 Windows 笔记本电脑上运行良好,但在 linux 机器上部署我有时会在尝试读取时捕获Write end dead"异常BufferedReader 在我的处理线程中.
This works happily on windows laptop but deployment on a linux machine I sometimes get a "Write end dead" exception caught when trying to read the BufferedReader in my processing thread.
对此的研究表明作者没有正确关闭流:使用 PipedInputStream 编写结束死异常
Research into this suggests the writer is not closing the stream properly: Write end dead exception using PipedInputStream
所以,这要么是 spring-integration 中的错误,要么缺少某些东西在我的配置中.我希望是后者并且可以使用反馈我获取 InputStream 的方式.如果我得到 InputStream正确,那么如何让作者在写入后关闭输入流?
So, either this is a bug in spring-integration or there is something missing in my configuration. I am hoping it is the latter and could use feedback on the way I am obtaining the InputStream. If I am getting the InputStream correctly, then how can I get the writer to close the input stream after writing?
谢谢!
这里是出站网关配置:
<int-ftp:outbound-gateway session-factory="ftpClientFactory"
request-channel="inboundGetStream" command="get" command-options="-stream"
expression="payload" remote-directory="/" reply-channel="stream">
</int-ftp:outbound-gateway>
<int:channel id="stream">
<int:queue/>
</int:channel>
这里是我获取 InputStream 的地方:
Here is where I obtain the InputStream:
public InputStream openFileStream(final String filename, final String directory) throws Exception {
if (inboundGetStream.send(MessageBuilder.withPayload(directory + "/" + filename).build(), ftpTimeout)) {
return getInputStream();
}
return null;
}
private InputStream getInputStream() {
Message<?> msgs = stream.receive(ftpTimeout);
if (msgs == null) {
return null;
}
InputStream is = (InputStream) msgs.getPayload();
return is;
}
推荐答案
如果您分享更多 StackTrace 以进行调查会更好.
Would be better if you share more StackTrace to investigate.
另外,我没有看到您按照解决方案的建议关闭了会话:
Plus, I don't see that you close the session as it is recommended by the solution:
当以流的形式消费远程文件时,用户负责在消费完流后关闭Session.为方便起见,file_remoteSession
标头中提供了 Session
.
When consuming remote files as streams, the user is responsible for closing the Session after the stream is consumed. For convenience, the
Session
is provided in thefile_remoteSession
header.
<int:service-activator input-channel="markers"
expression="payload.mark.toString().equals('END') ? headers['file_remoteSession'].close() : null"/>
作为参考手册的示例.
另一方面,最好在获得 InputStream
的同一线程中使用它.不会将如此低级别的会话绑定资源转移到队列.
On the other hand it would be better to consume the InputStream
in the same thread as it has been obtained. Not shifting such a low level, session tied resource to the Queue.
还请检查此错误:https://bugs.eclipse.org/bugs/show_bug.cgi?id=359184.也许你真的应该升级到更新鲜的东西:http://search.maven.org/#search|ga|1|g%3A%22com.jcraft%22
Also check, please, this bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=359184 . Maybe you really should upgrade to something more fresh: http://search.maven.org/#search|ga|1|g%3A%22com.jcraft%22
这篇关于使用 SFTP 出站网关获取输入流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!