成功频道后删除文件

时间:2019-06-26 12:26:55

标签: spring-integration

我正在从FTP服务器 Foo.csv 中提取文件,创建新文件 bar.csv ,并将新文件发送回FTP。然后,我使用建议发送到成功频道,并删除成功建议中的有效负载,这是从本地删除 bar.csv ,然后传递给成功的通道带有一个头表达式,该头表达式带有我从一开始就从FTP服务器提取的原始文件( foo.csv ),然后将其发送回FTP历史记录文件夹以进行存档。

现在,在将其发送回FTP历史记录后,我也想从本地删除它,但找不到成功流程中的方法。有什么我可以实施的解决方案吗?

这是我到目前为止所拥有的:

public IntegrationFlow localToFtpFlow(Branch myBranch) {

    return IntegrationFlows.from(Files.inboundAdapter(new File(myBranch.getBranchCode()))
                    .filter(new ChainFileListFilter<File>()
                            .addFilter(new RegexPatternFileListFilter("final" + myBranch.getBranchCode() + ".csv"))
                            .addFilter(new FileSystemPersistentAcceptOnceFileListFilter(metadataStore(dataSource), "foo"))),//FileSystemPersistentAcceptOnceFileListFilter
            e -> e.poller(Pollers.fixedDelay(10_000)))
            .enrichHeaders(h ->h.headerExpression("file_originalFile", "new java.io.File('"+ myBranch.getBranchCode() +"/FEFOexport" + myBranch.getBranchCode() + ".csv')",true))
            .transform(p -> {
                LOG1.info("Sending file " + p + " to FTP branch " + myBranch.getBranchCode());
                return p;
            })

            .log()
            .transform(m -> {
                        this.defaultSessionFactoryLocator.addSessionFactory(myBranch.getBranchCode(),createNewFtpSessionFactory(myBranch));
                        LOG1.info("Adding factory to delegation");
                        return m;
            })
            .handle(Ftp.outboundAdapter(createNewFtpSessionFactory(myBranch), FileExistsMode.REPLACE)
                    .useTemporaryFileName(true)
                    .autoCreateDirectory(false)
                    .remoteDirectory(myBranch.getFolderPath()), e -> e.advice(expressionAdvice()))

            .get();
}

/**
* Creating the advice for routing the payload of the outbound message on different expressions (success, failure)
* @return Advice
*/

@Bean
public Advice expressionAdvice() {
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setSuccessChannelName("success.input");
    advice.setOnSuccessExpressionString("payload.delete() + ' was successful'");
    //advice.setOnSuccessExpressionString("inputMessage.headers['file_originalFile'].renameTo(new java.io.File(payload.absolutePath + '.success.to.send'))");
    //advice.setFailureChannelName("failure.input");
    advice.setOnFailureExpressionString("payload + ' was bad, with reason: ' + #exception.cause.message");
    advice.setTrapException(true);
    return advice;
}

/**
 * Creating FTP connection based on the branch ftp data entered.
 * @return ftpSessionFactory
 */

public DefaultFtpSessionFactory createNewFtpSessionFactory(Branch branch) {
    final DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
    factory.setHost(branch.getHost());
    factory.setUsername(branch.getUsern());
    factory.setPort(branch.getFtpPort());
    factory.setPassword(branch.getPassword());
    return factory;
}

/**
 * Creating a default FTP connection.
 * @return ftpSessionFactory
 */
@Bean
public SessionFactory<FTPFile> createNewFtpSessionFactory() {
    final DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
    factory.setHost("xxxxxx");
    factory.setUsername("xxx");
    factory.setPort(21);
    factory.setPassword("xxxxx");
    return factory;
}

/**
 * Creating a metadata store to be used across the application flows to prevent reprocessing the file if it is already processed.
 * This will save the new file in a metadata table in the DB with the state of the report, so when a new copy comes with different date it will be processed only.
 * @return metadataStore
 * */
@Bean
public ConcurrentMetadataStore metadataStore(final DataSource dataSource) {
    return new JdbcMetadataStore(dataSource);
}

/*
* Success channel that will handle the AdviceMessage from the outbound adapter and sends the inputMessage file_originalFile to FTP destination folder specified.
*
* */

@Bean
public IntegrationFlow success(){
    return f -> f.transform("inputMessage.headers['file_originalFile']")
            .transform(e -> {
                //getting the Branch code from the Input message and calling the correct factory based on it
                delegatingSessionFactoryAuto().setThreadKey(e.toString().substring(0,3));
                return e;
            })
            .handle(Ftp.outboundAdapter(delegatingSessionFactoryAuto(), FileExistsMode.REPLACE)
                    .useTemporaryFileName(true)
                    .autoCreateDirectory(true)
                    .fileNameGenerator(new FileNameGenerator() {
                        @Override
                        public String generateFileName(Message<?> message) {
                            return new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date()) + ".csv";
                        }
                    })
                    .remoteDirectory("/ftp/erbranch/EDMS/FEFO/History/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date())) // + dateFormat.format(date)
                    .get(),e -> e.advice(expressionAdviceForSuccess()));

}

@Bean
public Advice expressionAdviceForSuccess() {
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    //advice.setSuccessChannelName("success.input");
    advice.setOnSuccessExpressionString("payload.delete() + ' was successful'");
    //advice.setOnSuccessExpressionString("inputMessage.headers['file_originalFile'].renameTo(new java.io.File(payload.absolutePath + '.success.to.send'))");
    //advice.setFailureChannelName("failure.input");
    advice.setOnFailureExpressionString("payload + ' was bad, with reason: ' + #exception.cause.message");
    advice.setTrapException(true);
    return advice;
}

@Bean
public DelegatingSessionFactory<FTPFile> delegatingSessionFactoryAuto(){

    SessionFactoryLocator<FTPFile> sff = createNewFtpSessionFactoryAndAddItToTheLocator();
    return new DelegatingSessionFactory<FTPFile>(sff);
}

@Bean
public SessionFactoryLocator<FTPFile> createNewFtpSessionFactoryAndAddItToTheLocator(){

    this.defaultSessionFactoryLocator.addSessionFactory("BEY",createNewFtpSessionFactory());
    return this.defaultSessionFactoryLocator;
}

1 个答案:

答案 0 :(得分:0)

只需向另一个FTP通道适配器添加另一个,即可添加另一个建议。