Sending data with Spring Batch can be done in several ways. The most used case with Spring Batch is to send the data as XML files, but this framework offers you a lot of other ways too.
One of these ways is to send the data to a secure FTP server. For this use case the Spring Integration framework has the correct interface.
Firstly you need, like in all cases to create files with Spring Batch, a marshaller with a “Reader” and a “Writer”.
The Reader:
The RowMapper which is used by the Reader:
The DomainObject which is used by the Reader:
The Writer:
The HeaderCallback is only needed, if you want to give the file, in our case a csv file, a headline, which is not created by the Reader. Then you need a small java class which creates the line for the header.
For sending the files to a SFTP server you need a SftpSessionFactory, that includes the access data of the receiving server.
Then you need a channel. This channel is used to send the data.
For knowing the kind of sending, you have to tell Spring Batch the way to do. This is done by an “outbound-channel-adapter”, which includes the kind of sending way, links the channel to the SftpSessionFactory and tells the framework how the file should be named on the target server (remote-filename-generator).
To make it easy, the target server should store the file with the same name like it is stored on the sending server. To do this, you need only the DefaultFilenameGenerator of Spring Integration.
To really send the file now, you need at least a Tasklet and a BatchJob.
For the Tasklet you need a small java class where you give the filename and the channel as parameters.
The javacode for sending is also really simple:
At least the xml code for the BatchJob:
This BatchJob can be started by a CronJob on the server.
At last part I will shortly post the header of the xml file called “applicationContext.xml”, which includes all the described beans:
Blog author
Carsten Fröhlich
Do you still have questions? Just send me a message.
Do you still have questions? Just send me a message.