【问题标题】:Use Spring Integration to move File Object使用 Spring Integration 移动文件对象
【发布时间】:2016-10-14 19:16:26
【问题描述】:

我们正在使用 Spring 4.3.3.Release 和 Spring Integration 4.3.4.Release,这两个版本都应该是本文发布时的最新版本。我们正在应用程序中创建一个 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"/>

“sftp.remote.outbound.dir”指向 sftp 服务器上的真实目录: /home/test/tom/出站

我的班级看起来像:

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

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

【问题讨论】:

    标签: java spring spring-integration sftp


    【解决方案1】:

    在一般情况下,配置正确的入站和出站通道和适配器即可完成。类似以下的东西

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

    【讨论】:

    • 在这种情况下不需要网桥,您可以在入站和出站适配器上使用相同的通道。如果要添加错误处理,请将error-channel 添加到轮询器,您将收到错误消息,其中包含失败消息和原因的有效负载。另请参阅新的 file-split-ftp sample app 以获得更复杂的版本,使用 Java DSL。
    • 好的,添加 sftpOutBoundChannel,现在我需要如何在我的代码中使用它。我将看一下 Spring Integration 示例,看看需要如何定义代码。
    猜你喜欢
    • 2018-09-17
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 2018-06-09
    • 2015-04-26
    • 1970-01-01
    相关资源
    最近更新 更多