Reputation: 1998
We're using Spring 4.3.3.Release and Spring Integration 4.3.4.Release which should both be the latest versions as of the time of this post. We're creating a File object in the application, and we don't need to write this file out to disk.
We want to take the File object and using Spring Integration, move this file to a remote SFTP directory.
I have some familiarity with Spring Integration, and I have been looking at some of the examples, and I am not sure which example makes the most sense right now.
If I could be referred to the right place, then I will take the examples and see how I can apply them to my code.
Thanks!
UPDATE 1:
Here is my new application context XML file:
<int-sftp:outbound-channel-adapter id="sftpOutbondAdapter"
channel="ftpOutboundChannel"
remote-directory="${sftp.remote.outbound.dir}"
session-factory="sftpSessionFactory" >
<int-sftp:request-handler-advice-chain>
<int:retry-advice />
</int-sftp:request-handler-advice-chain>
</int-sftp:outbound-channel-adapter>
<int:channel id="ftpOutboundChannel"/>
The "sftp.remote.outbound.dir" is to a real directory on the sftp server: /home/test/tom/outbound
And my class looks something like:
@Transactional
@Service("reportService")
public class ReportServiceImpl implements ReportService
{
@Autowired
private MessageChannel ftpOutboundChannel;
@Scheduled(cron = "${sftp.timerTaskCron}", zone = "UTC")
public void runReports() throws Exception
{
File file = new File(path to some local filename);
// yes, this file really exists
Message<File> message = MessageBuilder.withPayload(file).build();
ftpOutboundChannel.send(message);
// I presume this file is output to that local directory
}
}
This seems to compile ok, but I just want to make sure the mechanism to move this is configured correctly.
I have a unit test that is very similar. It's Transactional, and maybe it shouldn't be?
public class SftpOutboundReceiveSample extends BaseServiceTests
{
@Autowired
private MessageChannel ftpOutboundChannel;
private String _filename = "/src/test/resources/attachment/DemoTrialFormv6_1.pdf";
@Test
public void runDemo()
{
File file = new File(_filename);
assertNotNull(file);
Message<File> message = MessageBuilder.withPayload(file).build();
ftpOutboundChannel.send(message);
}
}
Does the fact that this is transactional means that it rolls the file out of this directory once we are done with the test? Or, is this not a great test? I am trying to just make sure that the file gets to the right location.
Upvotes: 1
Views: 1953
Reputation: 28519
In a general case it boils done to configuring proper inbound and outban channels and adapters. Something along the lines of following
<int:channel id="fileInboundChannel"/>
<int:channel id="ftpOutboundChannel"/>
<int:bridge input-channel="fileInboundChannel" output-channel="ftpOutboundChannel"/>
<file:inbound-channel-adapter id="fileSource"
directory="/path/to/file" channel="fileInboundChannel" prevent-duplicates="true">
<int:poller fixed-rate="1000"/>
</file:inbound-channel-adapter>
<ftp:outbound-channel-adapter channel="ftpOutboundChannel"
remote-directory="/path/to/storage/" session-factory="ftpSessionFactory"/>
<bean name="sessionFactory"
class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="localhost"/>
<property name="port" value="587"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
</bean>
Upvotes: 2