Apache Camel: How to pass values from configure method to from() & to() components? — [RESOLVED]

谁都会走 提交于 2020-12-15 00:50:58

问题


I have a scenario where I have to read a file from the location on certain interval, extract the file name & file path, hit 2 rest services which is a Get & Post call using those inputs & place the file in appropriate location. I have managed a pseudo code as follows.

Wanted to know if there's a better way of achieving this using Camel. Appreciate your help!

The flow is -

  1. Extract the fileName
  2. Hit a Get endpoint ('getAPIDetails') using that fileName as an input to check if that fileName exists in that registry.
    • If the response is successful (status code 200)

      • Call a Post endpoint ('registerFile') with fileName & filePath as RequestBody
      • Move the file to C:/output folder (moving the file is still TODO in the code below).
    • If the file is not found (status code 404)

      • Move the file to C:/error folder.

'FileDetails' below is a POJO consisting of fileName & filePath which will be used for passing as a RequestBody to post service call.

@Override
public void configure() throws Exception {

    restConfiguration().component("servlet").port("8080")).host("localhost")
        .bindingMode(RestBindingMode.json);

    from("file:C://input?noop=true&scheduler=quartz2&scheduler.cron=0 0/1 * 1/1 * ? *")
        .process(new Processor() {
            public void process(Exchange msg) {
                String fileName = msg.getIn().getHeader("CamelFileName").toString();
                System.out.println("CamelFileName: " + fileName);
                FileDetails fileDetails = FileDetails.builder().build();
                fileDetails.setFileName(fileName);
                fileDetails.setFilePath(exchange.getIn().getBody());
            }
        })
        // Check if this file exists in the registry. 
        // Question: Will the 'fileName' in URL below be picked from process() method?
        .to("rest:get:getAPIDetails/fileName")
        .choice()
            // If the API returns true, call the post endpoint with fileName & filePath as input params
            .when(header(Exchange.HTTP_RESPONSE_CODE).isEqualTo(constant(200)))
                // Question: Will 'fileDetails' in URL below be passed as a requestbody with desired values set in process() method?
                // TODO: Move the file to C:/output location after Post call
                .to("rest:post:registerFile?type=fileDetails")
            .otherwise()
                .to("file:C://error");
}

回答1:


Managed to resolve this use case with below approach. Closing the loop. Thank you!

P.S.: There's more to this implementation. Just wanted to put across the approach.

@Override
public void configure() throws Exception {
    
    // Actively listen to the inbound folder for an incoming file
    from("file:C://input?noop=true&scheduler=quartz2&scheduler.cron=0 0/1 * 1/1 * ? *"")
      .doTry()
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setHeader("fileName",
                        exchange.getIn().getHeader("CamelFileName").toString());
            }
        })
        // Call the Get endpoint with fileName as input parameter
        .setHeader(Exchange.HTTP_METHOD, simple("GET"))
        .log("Consuming the GET service")
        .toD("http://localhost:8090/getAPIDetails?fileName=${header.fileName}")
        .choice()
            // if the API returns true, move the file to the processing folder 
            .when(header(Exchange.HTTP_RESPONSE_CODE).isEqualTo(constant(200)))
                .to("file:C:/output")
                .endChoice()
            // If the API's response code is other than 200, move the file to error folder
            .otherwise()
                .log("Moving the file to error folder")
                .to("file:C:/error")
      .endDoTry()
      .doCatch(IOException.class)
        .log("Exception handled")
      .end();
    
    // Listen to the processing folder for file arrival after it gets moved in the above step
    from("file:C:/output")
        .doTry()
            .process(new FileDetailsProcessor())
            .marshal(jsonDataFormat)
            .setHeader(Exchange.HTTP_METHOD, simple("POST"))
            .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
            .log("Consuming the POST service")
            // Call the Rest endpoint with fileName & filePath as RequestBody which is set in the FileDetailsProcessor class
            .to("http://localhost:8090/registerFile")
            .process(new MyProcessor())
            .endDoTry()
        .doCatch(Exception.class)
            .log("Exception handled")
        .end();
}


来源:https://stackoverflow.com/questions/65154708/apache-camel-how-to-pass-values-from-configure-method-to-from-to-componen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!