0
votes

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!

1

1 Answers

2
votes

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