骆驼异步调用获取响应

时间:2018-08-03 13:27:58

标签: apache-camel

我正在使用以下代码使用apache camel API对Web服务进行异步调用:

    Exchange exchange = new DefaultExchange(context);
    Message msg = exchange.getIn();
    msg.setBody(requestStr);
    msg.setHeader("content-type", "application/json");

    template.asyncCallback("direct:invokeAPI", exchange, new Synchronization() {
        @Override
        public void onComplete(Exchange exchange) {
            System.out.println("Success");
            System.out.println(exchange);
            System.out.println(exchange.getIn());
            System.out.println(exchange.getIn().getHeaders());
            System.out.println(exchange.getIn().getBody());
            System.out.println("================================");
            System.out.println(exchange.getOut());
            System.out.println(exchange.getOut().getHeaders());
            System.out.println(exchange.getOut().getBody());
            System.out.println("================================");
            Exception ex = exchange.getException();
            System.out.println(ex.getMessage());
        }

        @Override
        public void onFailure(Exchange exchange) {
            System.out.println("Failure");
        }
    });

现在我的网络服务失败并出现异常,并返回json响应:

Response-Code: 500
Content-Type: application/json
Headers: {Content-Type=[application/json], Date=[Fri, 03 Aug 2018 13:18:19 GMT]}
Payload: {"errorMessage":"Unable to process","errorCode":"500"}

现在如何在template.asyncCallback方法中捕获此信息。 在我上面的代码中,它将转到onComplete回调方法并在下面的消息中打印:

Success
Exchange[ID-xxxx-0-2]
Message[ID-xxxx-0-1]
{breadcrumbId=ID-xxxx-0-1, content-type=application/json}
{
"source":"PDF"
}
================================
Message[]
{}
null
================================

我正在尝试获取状态500代码和具有错误信息的响应有效负载,但是我无法打印任何内容。您能否帮我获取错误信息的正确方法是什么?

0 个答案:

没有答案