发送请求(camel-http)后如何处理错误?

时间:2019-02-11 11:14:28

标签: java apache-camel apache-servicemix blueprint-osgi camel-http

我想根据http代码响应来处理错误。

我还想知道如何在我的路线上启用*throwExceptionOnFailure*。例如,如果响应为code is 500x,则将消息发送到队列“ redmine_errors”

更新4:

从答案@ fg78nc添加异常后的我的蓝图(不起作用)

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
        http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
        http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd
        http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
       ">

<bean id="tfsToRedmineMapper"
    class="com.stackabuse.example.TfsToRedmineMapper" />

<bean id="myBean" class="com.stackabuse.example.MyBean" />

<camelContext
    xmlns="http://camel.apache.org/schema/blueprint">

<onException>
    <exception>org.apache.camel.http.common.HttpOperationFailedException
    </exception>
    <onWhen>
        <method ref="myBean" method="parseException" />
    </onWhen>
    <handled>
        <constant>true</constant>
    </handled>
    <to uri="log:redmine_errors" />
</onException>
    <route>
        <from uri="jetty:http://0.0.0.0:8082/test" />
        <inOnly uri="activemq://from_tfs" />
    </route>
    <route>
        <from uri="activemq://from_tfs" />
        <process ref="tfsToRedmineMapper" />
        <to uri="activemq://for_redmine" />
    </route>
    <route>
        <from uri="activemq://for_redmine" />
        <setHeader headerName="Content-Type">
            <constant>application/json; charset=utf-8</constant>
        </setHeader>
        <setHeader headerName="X-Redmine-API-Key">
            <constant>my_redmine_api_token</constant>
        </setHeader>
        <toD uri="${header.url}" />
    </route>

错误:     2019-02-15 09:35:12,103 | ERROR | mix-7.0.1/deploy | BlueprintCamelContext | 40 - org.apache.camel.camel-blueprint - 2.16.5 | Error occurred during starting Camel: CamelContext(camel-32) due Failed to create route route48 at: >>> OnException[null When[bean{} -> []] -> [To[activemq://redmine_errors]]] <<< in route: Route(route48)[[From[jetty:http://0.0.0.0:8082/test]] -> [On... because of org.apache.camel.http.common.HttpOperationFailedException org.apache.camel.FailedToCreateRouteException: Failed to create route route48 at: >>> OnException[null When[bean{} -> []] -> [To[activemq://redmine_errors]]] <<< in route: Route(route48)[[From[jetty:http://0.0.0.0:8082/test]] -> [On... because of org.apache.camel.http.common.HttpOperationFailedException

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

不幸的是,骆驼没有正确设置Http状态代码。 下面的解决方案有点令人费解,但它可以工作。 也可以使用简单的语言谓词在XML中解决该问题,但是由于某种原因它对我不起作用,因此我将Java用作谓词。

蓝图:

 <bean id="myBean" class="com.example.MyBean" />

 <onException>
     <exception>org.apache.camel.http.common.HttpOperationFailedException</exception>
      <onWhen>
         <method ref="myBean" method="parseException" />
      </onWhen>
      <handled>
         <constant>true</constant>
      </handled>
      <to uri="jms:redmine_errors"/>
 </onException>

Java:

       package com.example;

       public class MyBean {

       public boolean parseException(Exchange exchange){
              return exchange.getProperty("CamelExceptionCaught")
                             .toString().contains("statusCode: 500");
            }
       }