0

I am trying to read a remote file line by line, using spring integration. Using the spring documentation found here I have set up my project to poll for the file and transfer it via sftp when it is found. I am stuck on how to go about reading the file contents one line at a time.

Here is my inbound channel adapter setup that currently works to pull in files.

<int-sftp:inbound-channel-adapter id="sftpAdapterAutoCreate"
        session-factory="sftpSessionFactory"
        channel="receiveChannel"
        filename-pattern="*.txt"
        remote-directory="/home/springftp"
        preserve-timestamp="true"
        local-directory="file:C:\sprintftp"
        auto-create-local-directory="true"
        temporary-file-suffix=".writing"
        delete-remote-files="false">
    <int:poller fixed-rate="1000" max-messages-per-poll="1"/>
</int-sftp:inbound-channel-adapter>

<int:channel id="receiveChannel"> 
    <int:queue/> 
</int:channel> 

Edit: To clarify, I would like to retrieve one line at a time from the remote file, then process the contents of that line, then retrieve the next line. Similar to creating a java.io.inputstream for a local file and reading it line by line.

Any help is much appreciated. Thank you!

Community
  • 1
  • 1
Chris
  • 1
  • 1
  • 2

1 Answers1

2

You can use <file-to-string-transformer> after the receiving File and <splitter> to delimit the content of payload to the list of lines.

UPDATE

I would like to retrieve one line at a time from the remote file, then process the contents of that line, then retrieve the next line. Similar to creating a java.io.inputstream for a local file and reading it line by line.

Well, Unfortunatelly we don't provide high-level component for that, but you can try to use the features from the RemoteFileTemplate:

RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<FTPFile>(this.ftpSessionFactory);
template.setFileNameExpression(new SpelExpressionParser().parseExpression("payload"));
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
final ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
template.get(new GenericMessage<String>("ftpSource/ftpSource1.txt"), new InputStreamCallback() {

    @Override
    public void doWithInputStream(InputStream stream) throws IOException {
        FileCopyUtils.copy(stream, baos1);
    }
});

This code you can to some your POJO service and wire the last one with <service-activator>.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Sorry, I should clarify my intentions. I would like to retrieve one line at a time from the remote file, then process the contents of that line, then retrieve the next line. Similar to creating a java.io.inputstream for a local file and reading it line by line. – Chris Sep 19 '14 at 13:01
  • @ArtemBilan can't we use SFTP outbound gateway `get` command with `-stream` option to read the file as stream? http://docs.spring.io/spring-integration/reference/html/sftp.html#sftp-outbound-gateway – Chacko Jan 06 '16 at 06:19