处理后的Spring集成移动文件 [英] Spring integration move file after processing

查看:86
本文介绍了处理后的Spring集成移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理文件..之后如何在Spring集成中处理后移动文件? 我已关注 http://xpadro.blogspot.com/2016 /07/spring-integration-polling-file.html 来实现文件轮询,但是我需要添加onSuccess和OnError跨国事件(无XML配置)

How to move a file after processing in spring integration after processing file ..? I have followed http://xpadro.blogspot.com/2016/07/spring-integration-polling-file.html to implement the file polling , but I need to add onSuccess and OnError transnational events (with out XML configurations)

推荐答案

我不确定您所说的事务"是什么意思,文件系统通常不是事务性的,但是您可以向流程中的最终使用者添加建议. ..

I am not sure what you mean by "transaction", file systems are generally not transactional, but you can add an advice to the final consumer in the flow...

@SpringBootApplication
public class So40625031Application {

    public static void main(String[] args) {
        SpringApplication.run(So40625031Application.class, args);
    }

    @Bean
    public IntegrationFlow flow() {
        return IntegrationFlows.from(
                    Files.inboundAdapter(new File("/tmp/foo")), e -> e.poller(Pollers.fixedDelay(1000)))
                .transform(Transformers.fileToString())
                .handle("processor", "process", e -> e.advice(advice()))
                .get();
    }

    @Bean
    public Processor processor() {
        return new Processor();
    }

    @Bean
    public AbstractRequestHandlerAdvice advice() {
        return new AbstractRequestHandlerAdvice() {

            @Override
            protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
                File file = message.getHeaders().get(FileHeaders.ORIGINAL_FILE, File.class);
                try {
                    Object result = callback.execute();
                    file.renameTo(new File("/tmp/bar", file.getName()));
                    System.out.println("File renamed after success");
                    return result;
                }
                catch (Exception e) {
                    file.renameTo(new File("/tmp/baz", file.getName()));
                    System.out.println("File renamed after failure");
                    throw e;
                }
            }
        };
    }

    public static class Processor {

        public void process(String in) {
            System.out.println(in);
        }

    }

}

这篇关于处理后的Spring集成移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆