【问题标题】:How to handle errors after sending request(camel-http)?发送请求(camel-http)后如何处理错误?
【发布时间】:2019-02-11 11:14:28
【问题描述】:

我想根据 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

【问题讨论】:

  • 您应该能够将?throwExceptionOnFailure=false 附加到要调用的URI 的末尾。这将防止 Camel 引发异常,但您可以通过 .when(header(Exchange.HTTP_RESPONSE_CODE)).isGreaterThanOrEqualTo(400))... 检查标头中的响应代码。实际的响应正文将在消息正文中提供
  • 您应该将 onException 移到路由之外,即将它保持在 CamelContext 级别,而不是路由级别,这样它将适用于所有路由。
  • 我按照你说的做了,但现在我得到了错误:org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to validate xml ... Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'onException'. One of '{"http://camel.apache.org/schema/blueprint":route}' is expected.

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


【解决方案1】:

很遗憾,Camel 没有正确设置 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");
            }
       }

【讨论】:

  • 2019-02-12 09:41:36,704 |错误 |混合 7.0.1/部署 |蓝图CamelContext | 40 - org.apache.camel.camel-蓝图 - 2.16.5 |启动 Camel 时发生错误:CamelContext(camel-40) 由于无法在以下位置创建路由 route117:>>> OnException[null When[simple{${header.CamelHttpResponseCode} == 500} -> [To[jms:redmine_errors]] ] -> []] [OnExcepti... 因为 org.apache.camel.http.common.HttpOperationFailedException
  • @D.Batmanov 我已经更新了我的答案,请检查它是否适合你。它对我有用。
  • 如果我添加 onException 块我有错误(bundle 没有启动):原因:org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: 发现无效的内容开始元素'onException'。 '{"camel.apache.org/schema/blueprint":route}' 之一是预期的。我将在上面添加我的完整蓝图(我的开发机器是 Intranet(也许这很重要?))
  • 你是在 CamelContext 元素中添加了它,但在路由元素之外添加了吗?
  • 请将 onException 块放在路由块上方。
猜你喜欢
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-06
  • 1970-01-01
相关资源
最近更新 更多