Spring integration - reading remote file line by line
I am trying to read a remote file line by line using spring integration. Using the spring documentation I found here I set up my project to poll for a file and pipe it through sftp when it was found. I am stuck on how to go about reading the contents of a file one line at a time.
Here is my inbound adapter setup currently working to pull 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 get one line at a time from a remote file and then process the contents of that line and then get the next line. Likewise creating java.io.inputstream for a local file and reading it line by line.
Any help is greatly appreciated. Thank!
source to share
You can use <file-to-string-transformer>
after receiving the file and <splitter>
to delimit the content payload
into a list of strings.
UPDATE
I would like to get one line at a time from a remote file and then process the contents of that line and then get the next line. Likewise creating java.io.inputstream for a local file and reading it line by line.
Well, unfortunately we don't provide a high level component for this, but you can try using functions from 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);
}
});
You can use this code for your POJO service and connect the latter using <service-activator>
.
source to share