How to poll for multiple files at once with Spring Integration with WebFlux?

Viewed 15

I have the following configuration below for file monitoring using Spring Integration and WebFlux.

It works well, but if I drop in 100 files it will pick up one file at a time with a 10 second gap between the "Received a notification of new file" log messages.

How do I poll for multiple files at once, so I don't have to wait 1000 seconds for all my files to finally register?

@Configuration
@EnableIntegration
public class FileMonitoringConfig {

  private static final Logger logger =
      LoggerFactory.getLogger(FileMonitoringConfig.class.getName());

  @Value("${monitoring.folder}")
  private String monitoringFolder;

  @Value("${monitoring.polling-in-seconds:10}")
  private int pollingInSeconds;

  @Bean
  Publisher<Message<Object>> myMessagePublisher() {
    return IntegrationFlows.from(
            Files.inboundAdapter(new File(monitoringFolder))
                .useWatchService(false),
            e -> e.poller(Pollers.fixedDelay(pollingInSeconds, TimeUnit.SECONDS)))
        .channel(myChannel())
        .toReactivePublisher();
  }

  @Bean
  Function<Flux<Message<Object>>, Publisher<Message<Object>>> myReactiveSource() {
    return flux -> myMessagePublisher();
  }

  @Bean
  FluxMessageChannel myChannel() {
    return new FluxMessageChannel();
  }

  @Bean
  @ServiceActivator(
      inputChannel = "myChannel",
      async = "true",
      reactive = @Reactive("myReactiveSource"))
  ReactiveMessageHandler myMessageHandler() {
    return new ReactiveMessageHandler() {
      @Override
      public Mono<Void> handleMessage(Message<?> message) throws MessagingException {
        return Mono.fromFuture(doHandle(message));
      }

      private CompletableFuture<Void> doHandle(Message<?> message) {
        return CompletableFuture.runAsync(
            () -> {
              logger.info("Received a notification of new file: {}", message.getPayload());
              File file = (File) message.getPayload();
            });
      }
    };
  }
}
1 Answers
Related