代码之家  ›  专栏  ›  技术社区  ›  Vikram Saini

mulesoft中未捕获异常

  •  0
  • Vikram Saini  · 技术社区  · 8 年前

    我正在开发一个应用程序,如果出现任何异常,它会适当地打印异常消息。这是我的自定义过滤器

        package filter;
    import java.io.IOException;
    import java.util.Map;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import org.mule.api.MuleMessage;
    public class ExceptionClass implements org.mule.api.routing.filter.Filter {
    
        @Override
        public boolean accept(MuleMessage message) {
            Map<String,Object> payload=(Map<String,Object>)message.getPayload();
    
            if(!payload.containsKey("productid"))
            {
    
                throw new java.lang.NullPointerException("no data found in payload "+payload.toString());
    
            }
            if(Integer.parseInt((String) payload.get("productid"))<5)
            {
            throw new IllegalArgumentException("invalid input to payload " +payload.toString());
    
            }
            return true;
        }
    
    }
    

    以下是应用程序的配置

        <?xml version="1.0" encoding="UTF-8"?>
    
    <mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
        xmlns:spring="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
    http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
        <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
        <flow name="muleexceptionhandlingFlow">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP" >
                <http:response-builder statusCode="#[flowVars['statuscode']]" reasonPhrase="#[flowVars['reason']]"/>
            </http:listener>
            <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
            <custom-filter doc:name="Custom" class="filter.ExceptionClass"/>
            <logger message="payload:#[payload]" level="INFO" doc:name="Logger"/>
            <logger message="#[message]" level="INFO" doc:name="Logger"/>
            <set-payload value="payload:#[payload]" doc:name="Set Payload"/>
        </flow>
        <choice-exception-strategy name="muleexceptionhandlingChoice_Exception_Strategy">
            <catch-exception-strategy doc:name="Catch Missing Data Exception Strategy" logException="false" when="#[exception.causedBy(java.lang.NullPointerException)]">
                <set-payload value="Missing data:#[payload]" doc:name="Set Payload"/>
                <set-variable variableName="reason" value="missing input data" doc:name="Variable"/>
                <set-variable variableName="statuscode" value="400" doc:name="Variable"/>
            </catch-exception-strategy>
            <catch-exception-strategy doc:name="Catch Invalid data Exception Strategy" logException="false" when="#[exception.causedBy(java.lang.IllegalArgumentException)}">
                <set-payload value="Invalid Data:#[payload]" doc:name="Set Payload"/>
                <set-variable variableName="reason" value="invalid input" doc:name="Variable"/>
                <set-variable variableName="statuscode" value="400" doc:name="Variable"/>
            </catch-exception-strategy>
        </choice-exception-strategy>
    </mule>
    

    错误2017-07-25 17:35:49595 [[muleeexceptionhandling].HTTP\u Listener\u配置.worker.01] ********************************************************************************消息:在有效载荷{price=1000,
    :{price=1000,productname=Shappoom}负载类型: java.util。HashMap筛选器: filter.ExceptionClass@584d4f19元素: 多异常处理:null:null --------------------------------------------------------------------------------根异常堆栈跟踪:java.lang.NullPointerException:无数据 在以下位置的有效负载{price=1000,productname=Shappoom}中找到 接受(ExceptionClass.java:23) accept(MessageFilter.java:93)位于 processor.AbstractFilteringMessageProcessor.process(AbstractFilteringMe ..................................

    1 回复  |  直到 8 年前
        1
  •  3
  •   jvas    8 年前

    您的异常策略没有被调用的原因是您的异常策略在您的块之外。更新如下:

          <flow name="muleexceptionhandlingFlow">
                <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP" >
                    <http:response-builder statusCode="#[flowVars['statuscode']]" reasonPhrase="#[flowVars['reason']]"/>
                </http:listener>
                <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
                <custom-filter doc:name="Custom" class="filter.ExceptionClass"/>
                <logger message="payload:#[payload]" level="INFO" doc:name="Logger"/>
                <logger message="#[message]" level="INFO" doc:name="Logger"/>
                <set-payload value="payload:#[payload]" doc:name="Set Payload"/>
    <choice-exception-strategy name="muleexceptionhandlingChoice_Exception_Strategy">
                <catch-exception-strategy doc:name="Catch Missing Data Exception Strategy" logException="false" when="#[exception.causedBy(java.lang.NullPointerException)]">
                    <set-payload value="Missing data:#[payload]" doc:name="Set Payload"/>
                    <set-variable variableName="reason" value="missing input data" doc:name="Variable"/>
                    <set-variable variableName="statuscode" value="400" doc:name="Variable"/>
                </catch-exception-strategy>
                <catch-exception-strategy doc:name="Catch Invalid data Exception Strategy" logException="false" when="#[exception.causedBy(java.lang.IllegalArgumentException)}">
                    <set-payload value="Invalid Data:#[payload]" doc:name="Set Payload"/>
                    <set-variable variableName="reason" value="invalid input" doc:name="Variable"/>
                    <set-variable variableName="statuscode" value="400" doc:name="Variable"/>
                </catch-exception-strategy>
            </choice-exception-strategy>
            </flow>
    

    我刚刚在博客中阅读了你的用例,让我印象深刻的是,即使你的代码可以正常工作,并按预期给出结果,但过滤器的设计是,如果它返回false,则只会停止流,并且在设计上不会引发任何异常。因此,实际上有两个更简洁的选项来处理这个场景。

    对于您的示例,这将是验证规则-

    <validation:is-true config-ref="Validation_Configuration" expression="#[payload.containsKey('productid')]" message="payload doesnt have productid" doc:name="Validation"/>
    

    更有趣的是,您可以使用All选项在同一个组件中进行多个验证

    <validation:all config-ref="Validation_Configuration"   doc:name="Validation">
                <validation:validations>
                    <validation:is-true expression="#[Integer.parseInt((String) payload.get(&quot;productid&quot;))&lt;5]" message="prod id less than 5"/>
                    <validation:is-true expression="#[payload.containsKey('productid')]" message="no product id"/>
                </validation:validations>
            </validation:all>
    

    (ii)使用您已经使用过的过滤器,但将其围绕在消息过滤器组件周围,如以下博客中所述: https://www.ricston.com/blog/playing-with-mule-filters/ 在您的示例中,可以使用以下表达式过滤器实现异常:

    <message-filter doc:name="Message" throwOnUnaccepted="true">
            <expression-filter expression="#[payload.containsKey(&quot;productid&quot;)]" doc:name="Expression"/>
            </message-filter>