Apache Camel:如何将值从配置方法传递到 from() &to() 组件?- [解决] [英] Apache Camel: How to pass values from configure method to from() & to() components? -- [RESOLVED]
问题描述
我有一个场景,我必须以一定的时间间隔从该位置读取文件,提取文件名 &文件路径,点击 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!
流程是-
- 提取文件名
- 使用该文件名作为输入点击 Get 端点 ('getAPIDetails') 以检查该文件名是否存在于该注册表中.
如果响应成功(状态码 200)
- Extract the fileName
- 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 & 调用 Post 端点 ('registerFile')文件路径作为请求体
- 将文件移动到 C:/output 文件夹(在下面的代码中移动文件仍然是 TODO).
如果找不到文件(状态码 404)
If the file is not found (status code 404)
- 将文件移动到 C:/error 文件夹.
下面的'FileDetails'是一个由fileName &组成的POJO.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:如何将值从配置方法传递到 from() &to() 组件?- [解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!