我正在处理一个应用程序,如果发生任何异常,它会正确打印异常消息。这是我的自定义过滤器
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>
以下是nullpointer异常
时的错误消息错误2017-07-25 17:35:49,595 [[muleexceptionhandling] .HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: ************************************************** ******************************消息:在有效负载中找不到数据{price = 1000, productname = Shampoo}(java.lang.NullPointerException)。有效载荷
:{price = 1000,productname = Shampoo}有效载荷类型: java.util.HashMap过滤器: filter.ExceptionClass@584d4f19元素: / muleexceptionhandlingFlow / processors / 1 @ muleexceptionhandling:NULL:NULL。 -------------------------------------------------- ------------------------------ Root异常堆栈跟踪:java.lang.NullPointerException:无数据 在有效负载中找到{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 ..................................
为什么异常没有被异常处理程序
捕获请指导!
答案 0 :(得分: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组件,如果您想要执行输入验证,那就是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>