Search code examples
javaspring-bootspring-integration

How to poll a directory and if the any xlsx file copied in the directory call an REST api to load the feed


I have a use case, i want to poll a directory, if the any .*xlsx file get pasted in that directory, i want to call a post rest API that will load the data. I'm not able to find the my way, please suggest some way to do this.


Solution

  • I believe that you are looking for fully working sample and I doubt that is going to be one since your business task might not be the same what other people are doing.

    Although we won't mind if you contribute back such a sample: https://github.com/spring-projects/spring-integration-samples.

    1. So, to build a logic we need to provide an IntegrationFlow: https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl.

    2. To read files from a dir we need to use a Files.inboundAdapter() with respective polling policy.

    3. You may do some transformation (.transform()) about polled file content or so.

    4. Call the REST service via Http.outboundGateway()

    5. Do the post-process.

    Something like this:

        @Bean
        public IntegrationFlow fileReadingFlow() {
            return IntegrationFlows
                    .from(Files.inboundAdapter(new File("myDir"))
                                    .patternFilter("*.xlsx"),
                            e -> e.poller(Pollers.fixedDelay(1000)))
                    .transform(...)
                    .handle(Http.outboundGateway("")
                            .expectedResponseType(String.class))
                    .transform(...)
                    .get();
        }
    

    (Haven't checked as working since I don't know what is your XSLT content and how you call the REST service.)

    This sample does something with files reading to gather some ideas: https://github.com/spring-projects/spring-integration-samples/tree/main/applications/file-split-ftp