弹簧整合后的移动文件处理



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

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

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

相关内容

  • 没有找到相关文章

最新更新