首页 > 解决方案 > 使用 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中的异常的正确方法是什么?

标签: javaapache-camel

解决方案


     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;

    }

推荐阅读