Home > Back-end >  How to poll a directory and if the any xlsx file copied in the directory call an REST api to load th
How to poll a directory and if the any xlsx file copied in the directory call an REST api to load th

Time:06-02

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.

CodePudding user response:

We have a system that does (almost) the same (just xml instead of xlsx) and we use Apache Camel https://camel.apache.org/

Integration is good with Spring Boot. You just need to define your route from("file:///<path").to("http:<host>:port/<path>) and it will do probably what you need.

Might need to tweek the line of code to get filtering and maybe add some transformation but it is a nice peace of software.

CodePudding user response:

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

  • Related