java - 使用 apache camel 调用外部 API 时处理异常?
问题描述
我需要从我的服务Service A调用下游服务Service B(给定 api 合同),上游系统正在调用该服务。
现在我正在使用 apache camel 服务实例来调用Service B,如下所示:
请求和响应:
public abstract class CamelRequest {
private String route
}
public abstract class CamelResponse {
}
public class ServiceBRequest extends CamelRequest {
// some fields are here
}
public class ServiceBResponse extends CamelResponse {
// some fields are here
}
骆驼服务.java:
import org.apache.camel.CamelExecutionException;
import org.apache.camel.ProducerTemplate;
public abstract class CamelService {
@Autowired
private ProducerTemplate template;
protected abstract void preProcessor(CamelRequest req);
protected abstract void postProcessor(CamelResponse res);
public CamelResponse process(CamelRequest req, Map<String, Object> headers) throws CamelExecutionException {
CamelResponse response = template.requestBodyAndHeaders(getRoute(req), req, headers, CamelResponse.class);
log.debug(req.getRoute()+":: "+returnMetricsMessageHistoryService());
return response;
}
private String getRoute(CamelRequest req) {
return req.getRoute();
}
}
服务BImpl.java:
public class ServiceBImpl extends CamelService {
@Override
public void preProcessor(CamelRequest req) {
//Do nothing because no validation exist on downstream request object
}
@Override
public void postProcessor(CamelResponse res) {
//Do nothing because no validation exist on downstream response object
}
}
调用下游服务service B:
public ServiceBResponse callServiceB(ServiceBRequest req, Map<String, Object> headers)
throws CamelExecutionException{
CamelService serviceBImpl = new ServiceBImpl();
ServiceBResponse resp = (ServiceBResponse) serviceBImpl.process(req, headers);
return resp;
}
现在,当我们将 serviceB 称为:
template.requestBodyAndHeaders(getRoute(req), req, headers, CamelResponse.class);
此方法抛出CamelExecutionException。
我需要弄清楚这个 CamelExecutionException 中可以包含哪些可能的异常,以便我可以单独处理它们或者为不同的异常执行不同的自定义日志记录。
For instance, for one exception:
if(exception.getCause() instanceof java.util.concurrent.TimeoutException)
{//do something }
在调用 API 时找出可能包含在CamelExecutionException中的异常的正确方法是什么?
解决方案
catch(CamelExecutionException camelException){
Exception exchangeException = camelException.getExchange().getException();
if (exchangeException != null && exchangeException.getCause() instanceof GoogleJsonResponseException) {
GoogleJsonResponseException originalException = (GoogleJsonResponseException) exchangeException.getCause();
return Response.status(originalException.getStatusCode()).build();
}
throw e;
}
或者
catch(CamelExecutionException camelException){
final Throwable cause = camelException.getCause();
if (cause instanceof HttpOperationFailedException) {
final HttpOperationFailedException httpError = (HttpOperationFailedException) cause;
}
throw camelError;
}
推荐阅读
- f# - F# 模式匹配字符串
- python - 如何“剪断”/提取C char [](内存地址)的一部分作为python字符串
- symfony - symfony mailer 无法发送邮件
- javascript - 我如何__scrape__一个使用谷歌应用脚本延迟加载的网站?
- assembly - 如何对不同类型的变量进行汇编算术?
- flutter - Dart - 如果值不是分数,则 int.toDouble() 仍将 runTimeType 设为 int
- python - 通过 Python 套接字从客户端使用对象方法
- ionic-framework - Ionic V1 项目中 Gulp/SASS 的问题
- node.js - 如何在执行之前等待异步函数中的回复.then
- jenkins - 是否可以根据控制台输出重播 Jenkins 管道作业?