Spring integration Java - how to use @InboundChannelAdapter to check a directory for files?

Ocelot picture Ocelot · Aug 5, 2015 · Viewed 9k times · Source

How can i get @InboundChannelAdapter to work with files? something like this:

<int-file:inbound-channel-adapter id="executionMessageFileInputChannel"
                                      directory="file:${fpml.messages.input}"
                                      prevent-duplicates="false" filename-pattern="*.xml">
        <int:poller fixed-delay="20000" max-messages-per-poll="1" />
</int-file:inbound-channel-adapter>

but in java?

Answer

Artem Bilan picture Artem Bilan · Aug 5, 2015

Something like this:

@Bean
@InboundChannelAdapter(value = "executionMessageFileInputChannel",
        poller = @Poller(fixedDelay = "20000", maxMessagesPerPoll = "1"))
public MessageSource<File> fileMessageSource(@Value("${fpml.messages.input}") File directory) {
    FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
    fileReadingMessageSource.setDirectory(directory);
    fileReadingMessageSource.setFilter(new SimplePatternFileListFilter("*.xml"));
    return fileReadingMessageSource;
}

From other side pay attention, please, to the Spring Integration Java DSL project, using which the same may look like:

    @Bean
    public IntegrationFlow fileReadingFlow(@Value("${fpml.messages.input}") File directory) {
        return IntegrationFlows
                .from(s -> s.file(directory).patternFilter("*.xml"),
                        e -> e.poller(Pollers.fixedDelay(20000)))
        .....................
                .get();
    }