1

I am attempting to process a file exactly like this question: read a remote file line by line

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.

My implementation successfully obtains the InputStream and kick off a separate thread to process in a 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.

Research into this suggests the writer is not closing the stream properly: Write end dead exception using PipedInputStream

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?

Thanks!

Here is outbound gateway configuration:

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

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;
}
Community
  • 1
  • 1
feblock352
  • 81
  • 1
  • 8

1 Answers1

0

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:

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 the file_remoteSession header.

<int:service-activator input-channel="markers"
           expression="payload.mark.toString().equals('END') ? headers['file_remoteSession'].close() : null"/>

As a sample from the Reference Manual.

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.

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

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Brilliant insight and super thanks for such quick answer! Not closing the session seems to be the problem. I had saw that in the reference manual but for some reason thought it only applied when using the splitter but after reading again I see my original misunderstanding. My exact solution for my config: `` – feblock352 Jun 08 '16 at 22:52
  • I have follow up question somewhat related: http://stackoverflow.com/questions/37798170/no-messages-when-obtaining-input-stream-from-sftp-outbound-gateway – feblock352 Jun 13 '16 at 20:00