Apache camel apachecamel:如何将值从configure方法传递到from();到()组件?--[已解决]
我有一个场景,在这个场景中,我必须在特定的时间间隔从该位置读取一个文件,提取文件名和文件路径,使用这些输入点击2个rest服务,这是一个Get&Post调用,并将文件放置在适当的位置。我管理了一个伪代码,如下所示 想知道是否有更好的方法来实现这一点使用骆驼。谢谢你的帮助 流程是-Apache camel apachecamel:如何将值从configure方法传递到from();到()组件?--[已解决],apache-camel,dsl,spring-camel,Apache Camel,Dsl,Spring Camel,我有一个场景,在这个场景中,我必须在特定的时间间隔从该位置读取一个文件,提取文件名和文件路径,使用这些输入点击2个rest服务,这是一个Get&Post调用,并将文件放置在适当的位置。我管理了一个伪代码,如下所示 想知道是否有更好的方法来实现这一点使用骆驼。谢谢你的帮助 流程是- 提取文件名 点击使用该文件名作为输入的Get端点(“getAPIDetails”),检查该文件名是否存在于该注册表中。 如果响应成功(状态代码200) 调用文件名和文件路径为RequestBody的Post端点('
- 如果响应成功(状态代码200)
- 调用文件名和文件路径为RequestBody的Post端点('registerFile')
- 将文件移动到C:/output文件夹(在下面的代码中,移动文件仍然是要做的事情)
- 如果找不到文件(状态代码404)
- 将文件移动到C:/error文件夹
@覆盖
public void configure()引发异常{
restConfiguration().component(“servlet”).port(“8080”).host(“localhost”)
.bindingMode(RestBindingMode.json);
from(“文件:C://input?noop=true&scheduler=quartz2&scheduler.cron=0 0/1*1/1*?*”)
.进程(新处理器(){
公共作废流程(交换消息){
字符串文件名=msg.getIn().getHeader(“CamelFileName”).toString();
System.out.println(“CamelFileName:+fileName”);
FileDetails FileDetails=FileDetails.builder().build();
setFileName(文件名);
setFilePath(exchange.getIn().getBody());
}
})
//检查注册表中是否存在此文件。
//问题:下面URL中的“文件名”是否会从process()方法中选取?
.to(“rest:get:getAPIDetails/fileName”)
.choice()
//如果API返回true,则使用fileName和filePath作为输入参数调用post端点
.when(头(Exchange.HTTP_响应_代码).isEqualTo(常量(200)))
//问题:下面URL中的“fileDetails”是否会作为requestbody传递,并在process()方法中设置所需的值?
//TODO:在Post调用后将文件移动到C:/output位置
.to(“rest:post:registerFile?type=fileDetails”)
.否则()
.to(“文件:C://错误”);
}
通过以下方法解决了此用例。结束循环。谢谢大家!
附言:这个实现还有很多。我只是想把这个方法付诸实践
@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();
}
通过以下方法解决了这个用例。结束循环。谢谢大家! 附言:这个实现还有很多。我只是想把这个方法付诸实践
@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();
}