I need to call dowstream service Service B (given api contract) from my service, Service A , which is being called by the upstream systems.
Now I am using apache camel service instance for making a call to Service B , as given below:
Request and Responses :
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
}
CamelService.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();
}
}
ServiceBImpl.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
}
}
Call to downstream service 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;
}
Now when we call serviceB as:
template.requestBodyAndHeaders(getRoute(req), req, headers, CamelResponse.class);
this method throws CamelExecutionException .
I need to figure out what possible exceptions can be wrapped inside this CamelExecutionException, so that I can handle them separately OR do different custom logging for different exceptions.
For instance, for one exception:
if(exception.getCause() instanceof java.util.concurrent.TimeoutException)
{//do something }
What is the correct way to find out possible exceptions wrapped inside CamelExecutionException while calling an API?
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;
}
OR
catch(CamelExecutionException camelException){
final Throwable cause = camelException.getCause();
if (cause instanceof HttpOperationFailedException) {
final HttpOperationFailedException httpError = (HttpOperationFailedException) cause;
}
throw camelError;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.