使用Spring Integration移动文件对象

时间:2016-10-14 19:16:27

标签: java spring spring-integration sftp

我们正在使用Spring 4.3.3.Release和Spring Integration 4.3.4。这两个应该是本文发布时的最新版本。我们在应用程序中创建了一个File对象,我们不需要将此文件写入磁盘。

我们想要获取File对象并使用Spring Integration,将此文件移动到远程SFTP目录。

我对Spring Integration有一定的了解,我一直在研究一些例子,我不确定哪个例子现在最有意义。

如果我可以被引用到正确的位置,那么我将采用这些示例,看看如何将它们应用到我的代码中。

谢谢!

更新1:

这是我的新应用程序上下文XML文件:

<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"/>

&#34; sftp.remote.outbound.dir&#34;是sftp服务器上的真实目录: /家/测试/汤姆/呼出

我的班级看起来像是:

@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
    }
}

这似乎编译好了,但我只是想确保正确配置移动它的机制。

我有一个非常相似的单元测试。这是交易性的,也许它不应该是?

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);
 }
}

这是事务性的事实是否意味着一旦我们完成测试,它就会将文件从这个目录中滚出来?或者,这不是一个很好的测试吗? 我试图确保文件到达正确的位置。

1 个答案:

答案 0 :(得分:2)

在一般情况下,它可以完成配置正确的入站和外部通道和适配器。下面的内容

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