异常未被 mulesoft 捕获
Exception not being caught in mulesoft
我正在开发一个应用程序,如果发生任何异常,它会打印异常消息 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;
}
}
这里是app的配置
<?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-07-25 17:35:49,595
[[muleexceptionhandling].HTTP_Listener_Configuration.worker.01]
org.mule.exception.DefaultMessagingExceptionStrategy:
******************************************************************************** Message : no data found in payload {price=1000,
productname=Shampoo} (java.lang.NullPointerException). Payload
: {price=1000, productname=Shampoo} Payload Type :
java.util.HashMap Filter :
filter.ExceptionClass@584d4f19 Element :
/muleexceptionhandlingFlow/processors/1 @
muleexceptionhandling:null:null
-------------------------------------------------------------------------------- Root Exception stack trace: java.lang.NullPointerException: no data
found in payload {price=1000, productname=Shampoo} at
filter.ExceptionClass.accept(ExceptionClass.java:23) at
org.mule.routing.MessageFilter.accept(MessageFilter.java:93) at
org.mule.processor.AbstractFilteringMessageProcessor.process(AbstractFilteringMe
..................................
为什么异常没有被异常处理程序捕获
请多指教!
你的异常策略没有被调用的原因是你的异常策略在你的块之外。更新如下:
<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>
我刚刚阅读了有关您的用例的博客,令我震惊的是,即使您的代码可以正常工作并按预期提供结果,过滤器也被设计为在 returns false 并且按照设计它不会抛出任何异常。所以你实际上有两个更简洁的选项来处理这种情况。
(i) 使用 Validation 组件,如果您想执行输入验证,mule 打算让您使用它。
因此对于您的示例,这将是验证规则 -
<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("productid"))<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("productid")]" doc:name="Expression"/>
</message-filter>
我正在开发一个应用程序,如果发生任何异常,它会打印异常消息 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;
}
}
这里是app的配置
<?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-07-25 17:35:49,595 [[muleexceptionhandling].HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: ******************************************************************************** Message : no data found in payload {price=1000, productname=Shampoo} (java.lang.NullPointerException). Payload
: {price=1000, productname=Shampoo} Payload Type : java.util.HashMap Filter : filter.ExceptionClass@584d4f19 Element : /muleexceptionhandlingFlow/processors/1 @ muleexceptionhandling:null:null -------------------------------------------------------------------------------- Root Exception stack trace: java.lang.NullPointerException: no data found in payload {price=1000, productname=Shampoo} at filter.ExceptionClass.accept(ExceptionClass.java:23) at org.mule.routing.MessageFilter.accept(MessageFilter.java:93) at org.mule.processor.AbstractFilteringMessageProcessor.process(AbstractFilteringMe ..................................
为什么异常没有被异常处理程序捕获
请多指教!
你的异常策略没有被调用的原因是你的异常策略在你的块之外。更新如下:
<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>
我刚刚阅读了有关您的用例的博客,令我震惊的是,即使您的代码可以正常工作并按预期提供结果,过滤器也被设计为在 returns false 并且按照设计它不会抛出任何异常。所以你实际上有两个更简洁的选项来处理这种情况。 (i) 使用 Validation 组件,如果您想执行输入验证,mule 打算让您使用它。
因此对于您的示例,这将是验证规则 -
<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("productid"))<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("productid")]" doc:name="Expression"/>
</message-filter>