本文介绍了Apache Camel:如何将值从configure方法传递到from()&to()组件?- [解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我必须以一定间隔从该位置读取文件,提取文件名&文件路径,点击2个其余的服务,即Get&使用这些输入进行通话后&将文件放在适当的位置.我已经按如下方式管理了伪代码.

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.

想知道是否有使用Camel实现此目标的更好方法.感谢您的帮助!

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

流为-

  1. 提取文件名
  2. 使用该文件名作为输入来命中Get端点('getAPIDetails'),以检查该文件名是否存在于注册表中.
    • 如果响应成功(状态码200)

  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)

  • 使用fileName&filePath作为RequestBody
  • 将文件移动到C:/output文件夹(在下面的代码中,移动文件仍然是TODO).

如果找不到该文件(状态码404)

If the file is not found (status code 404)

  • 将文件移动到C:/error文件夹.

下面的"FileDetails"是由fileName&filePath,将其用作RequestBody传递以发布服务调用.

'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");
}

推荐答案

通过以下方法设法解决此用例.结束循环.谢谢!

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

P.S .:此实现还有更多内容.只是想提出这种方法.

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();
}

这篇关于Apache Camel:如何将值从configure方法传递到from()&to()组件?- [解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 04:05