你好!
这是有关Apache Camel的第二篇文章,将来还会有更多。 本文的目的是向人们展示Camel的真正功能,也许有些人会发现一个用例,可以在其中使用此框架,而不必一遍又一遍地编写样板代码。
今天,我们将使用Camel调用API端点并处理某些异常。
依存关系
路线
排程路线
If you remember from introduction, this is a scheduler route, it will trigger a message every 120 seconds and send it to "direct:httpRoute" route. I am using synchronous "direct" component, there is also asynchronous "seda" component.
@Override” 公共无效的configure(){
来自(“ timer:scheduler?period = 120000”) .log(“预定作业!”) .to(“ direct:httpRoute”);
}
Http路由
这是一条HTTP客户端路由,它将调用bittrex rest API,并获取加密货币并记录响应。
@Override 公共无效的configure(){
from("direct:httpRoute")
.log("Http Route started")
.setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
.to("https://api.bittrex.com/api/v1.1/public/getcurrencies")
.log("Response : ${body}");
}
错误处理程序
如果您想涵盖一些例外情况,则有两个选择。
- 默认错误处理程序onException子句处理特定的类异常。
@Override 公共无效的configure(){
errorHandler(deadLetterChannel(“ mock:errorHandler”));
onException(HttpOperationFailedException.class) .log(“ $ {exception}”) .to(“ mock:errorHandler”);
from("direct:httpRoute")
.log("Http Route started")
.setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
.to("https://api.bittrex.com/api/v1.1/public/getcurrencies")
.log("Response : ${body}");
}
结果
这是本教程的全部内容,如果不清楚,请随时与我联系,我将很乐意回答任何问题。
谢谢!
from: https://dev.to//djoleb/apache-camel-calling-rest-api-25en