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

标签 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

最佳答案

不幸的是,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");
            }
       }

关于java - 发送请求(camel-http)后如何处理错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54629343/

相关文章:

ssl - 使用 apache camel 连接到 https 服务器

java - 不同型号的TableView

java - CloudResourceManager$Builder 上的 ClassNotFoundException

超过 MSS 时的 Java 套接字发送延迟

java - 矩阵 Choco3 中列的约束

java - 通过添加camel功能扩展现有的spring应用程序

java - Camel bean方法在另一个线程上运行

Java 消息传递 : Difference between ActiveMQ, Mule、ServiceMix 和 Camel

java - 从 java List 对象创建 XML 的最有效方法

osgi - 如何添加自定义 osgi 包作为新的依赖项?