【问题标题】:exception handling in apache servicemixapache servicemix中的异常处理
【发布时间】:2018-09-28 05:25:05
【问题描述】:

我是 ServiceMix 的新手,正在尝试在 ServiceMix 中进行异常处理。

当我尝试打印异常时,它还包含请求的正文。有什么方法可以只从异常中提取错误?

<from uri="activemq:topic://topic1"/>
            <!-- Schema validationm for the request received from Biblio -->
            <doTry>
                <to uri="validator:http://localhost/employee.xsd"/>
                <doCatch>                                           
                    <exception>java.lang.Exception</exception>  
                    <log message="${exception.message}"/>           
                </doCatch>                  
            </doTry>

以下是记录的异常:

org.apache.xerces.jaxp.validation.SimpleXMLSchema@a0059b5
errors: [
org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: 'asd' is not a valid value for 'integer'., Line : -1, Column : -1
org.xml.sax.SAXParseException: cvc-type.3.1.3: The value 'asd' of element 'empnumber' is not valid., Line : -1, Column : -1
]. Exchange[ID-GBSMIXDEV01-uk-oup-com-46713-1511957485149-83-2][Message: <empRecord>         
         <employee>
            <empnumber>asd</empnumber>            
            <surname>PM</surname>            
            <firstname>Abhinay</firstname>
         </employee>
      </empRecord>]

我得到了正确的异常,但我不希望异常中的以下部分。

有什么方法可以从异常消息中删除这些?

Exchange[ID-GBSMIXDEV01-uk-oup-com-46713-1511957485149-83-2][Message: <empRecord>         
         <employee>
            <empnumber>asd</empnumber>            
            <surname>PM</surname>            
            <firstname>Abhinay</firstname>
         </employee>
      </empRecord>]

【问题讨论】:

    标签: exception-handling apache-camel blueprint-osgi apache-servicemix


    【解决方案1】:

    该消息来自异常,因此您可以在 Processor 中操作字符串。

    编写一个简单的类来做到这一点:

    private static final Logger logger_ = LoggerFactory
            .getLogger(ExceptionMsgProcessor.class);
    
    public void process(Exchange exchange) {
        Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
        String msg = e.getMessage();
        // manipulate the string here
        log.info("Exception message: {}", msg);
    }
    

    然后将异常发送到这个bean

    <doTry>
        <to uri="validator:http://localhost/employee.xsd"/>
        <doCatch>                                           
            <exception>java.lang.Exception</exception>
            <to uri="bean:exceptionMsgProcessor" />
        </doCatch>                  
    </doTry>
    

    【讨论】:

    • 感谢 Alessandro :) 效果很好。出于好奇,您知道为什么异常包含消息正文吗?
    • @abhipm 创建异常的人正在添加该信息。在验证器组件中设置断点并查看异常是在哪里创建和抛出的 :-)
    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 2010-09-08
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    相关资源
    最近更新 更多