異常

2017-07-25 52 views
0

我上的應用程序,並在任何情況下發生異常它打印異常信息工作appropriately.Here是我的自定義過濾器異常

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> 

,這裏是在空指針異常的情況下,該錯誤消息

ERROR 2017年7月25日17:35:49595 [[muleexceptionhandling] .HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: **************************** ************************************************** **消息:在有效負載中找不到數據{price = 1000, productname = Shampoo}(java.lang.NullPointerException)。有效載荷
:{價格= 1000,產品名=洗髮}有效載荷類型: java.util.HashMap中篩選: [email protected]元素: /muleexceptionhandlingFlow /處理器/ 1 @ muleexceptionhandling:空:空 --- -------------------------------------------------- ---------------------------根異常堆棧跟蹤:java.lang.NullPointerException:在有效負載中找不到數據 {price = 1000,productname =洗髮}在 filter.ExceptionClass.accept(ExceptionClass.java:23)處 org.mule.processor.AbstractFilteringMessageProcessor.process org.mule.routing.MessageFilter.accept(MessageFilter.java:93)(AbstractFilteringMe 。 ..... ............................

爲什麼例外沒有得到例外處理程序捕獲

請指導!

回答

3

你的異常策略沒有被調用的原因是因爲你在你的塊之外有你的異常策略。如下更新:

 <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,停止流動,並且設計它不會拋出任何異常。所以你實際上有兩個清理選項來處理這種情況。 (i)使用驗證組件,如果您想要執行輸入驗證,這是您希望使用的驗證組件。

因此,對於你的榜樣,這將是驗證規則 -

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

更有趣的是,你可以使用所有選項將多個驗證在同一組件: - 像

<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> 
+0

非常感謝您的回答。其實我昨天就開始研究它,所以不太瞭解它:) –

+0

歡迎您:) – jvas