Spring文件入站适配器-具有单个通道的目录的优先级

时间:2018-12-20 11:49:17

标签: spring spring-integration

使用单个入站通道,我需要处理两个目录lowpriority和highprioiry,但是lowpriority文件是在highpriority之后选择的。 有谁知道如何通过单个通道处理文件入站适配器中的多个目录?

@Bean
public IntegrationFlow processFileFlow() {
    return pushFileForProcess(lowDirectory
            ,"processFile"
            , "fileInputChannel");
}

private IntegrationFlow pushFileForProcess(String processDir, String methodName, String adapterName) {
    String fileProcessor = "fileProcessor";
    return IntegrationFlows
            .from(Files.inboundAdapter(Paths.get(processDir).toFile())
                            .regexFilter(FILE_PATTERN_REGEX)
                            .preventDuplicates(false),
                    e -> e.poller(Pollers.fixedDelay(j.getPollerIntervalMs())
                            .maxMessagesPerPoll(j.getPollerMaxMsgs())
                            .errorChannel("errorChannel")) // moves processed files
                            .id(adapterName))
            .handle(fileProcessor, methodName).get();
}

1 个答案:

答案 0 :(得分:1)

当民意测验未为高优先级目录返回任何文件时,请使用Smart Poller Advice重新配置FileReadingMessageSource

大概您应该在每次低优先级轮询(无论成功与否)时重新配置它。

编辑

示例:

@SpringBootApplication
public class So53868122Application {

    private static final File HIGH = new File("/tmp/high");

    private static final File LOW = new File("/tmp/low");

    public static void main(String[] args) {
        HIGH.mkdir();
        LOW.mkdir();
        SpringApplication.run(So53868122Application.class, args);
    }

    @Bean
    public IntegrationFlow flow() {
        return IntegrationFlows.from(Files.inboundAdapter(HIGH),
                    e -> e.poller(Pollers.fixedDelay(5_000)
                            .advice(dirSwitcher())))
                .handle(System.out::println)
                .get();
    }

    @Bean
    public Advice dirSwitcher() {
        return new HighLowPollerAdvice();
    }

    public static class HighLowPollerAdvice extends AbstractMessageSourceAdvice {

        private boolean isHigh = true;

        @Override
        public Message<?> afterReceive(Message<?> result, MessageSource<?> source) {
            if (this.isHigh && result == null) {
                System.out.println("No results from " + HIGH + ", switching to " + LOW);
                this.isHigh = false;
                ((FileReadingMessageSource) source).setDirectory(LOW);
            }
            else if (!this.isHigh) {
                System.out.println("After one poll of " + LOW + " that returned "
                        + (result == null ? "no file" : result.getPayload()) + ", switching to " + HIGH);
                this.isHigh = true;
                ((FileReadingMessageSource) source).setDirectory(HIGH);
            }
            return result;
        }

    }

}