Spring integration move file after processing

懵懂的女人 提交于 2019-12-04 18:12:14

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

    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!