我在Mule 3.2.1中有一个简单的流程,它只能重新发布一个web服务。目标是了解异常处理在Mule中的工作原理。为了测试这个,我使用一个Web服务,它总是返回一个异常(即永远不正确或错误响应),我想捕获这个异常并处理它(登录到初学者的文件)。
问题在于,似乎根本没有调用异常策略......
我尝试了两个版本 - 第一个版本使用默认异常策略,第二个版本使用自定义异常策略。流程如下:
<flow name="LoginFlow">
<http:inbound-endpoint address="http://localhost:8081/WsFaultResponseMule" exchange-pattern="request-response">
<cxf:jaxws-service serviceClass="aaa.bbb.Soap">
<cxf:features>
<spring:bean class="sandbox.StackTraceFeature" id="stackTraceService"/>
</cxf:features>
</cxf:jaxws-service>
</http:inbound-endpoint>
<http:outbound-endpoint
host="localhost"
port="8080"
path="WsFaultResponse/services/Soap"
exchange-pattern="request-response">
<cxf:jaxws-client
clientClass="aaa.bbb.SforceService"
port="Soap"
wsdlLocation="classpath:wsdl/partner.wsdl"
operation="login"
soapVersion="1.1"
enableMuleSoapHeaders="false">
</cxf:jaxws-client>
</http:outbound-endpoint>
<default-exception-strategy>
<processor-chain>
<object-to-string-transformer/>
<file:outbound-endpoint path="data/exception" outputPattern="#[function:datestamp:yyyyMMddHHmmss].err"/>
</processor-chain>
</default-exception-strategy>
<!-- <custom-exception-strategy class="exception.TestExceptionListener"/> -->
</flow>
流程注释掉部分中提到的自定义策略很简单(基本上与文档中的相同):
public class TestExceptionListener extends DefaultMessagingExceptionStrategy {
public TestExceptionListener(MuleContext muleContext) {
super(muleContext);
}
private MuleEvent handle(Exception ex, MuleEvent event, RollbackSourceCallback rollbackMethod) {
logger.warn("Exception: " + ex.getMessage());
Object payloadBefore = event.getMessage().getPayload();
MuleEvent result = super.handleException(ex, event, rollbackMethod);
result.getMessage().setPayload(payloadBefore);
return result;
}
@Override
public MuleEvent handleException(Exception ex, MuleEvent event) {
return handle(ex, event, null);
}
@Override
public MuleEvent handleException(Exception ex, MuleEvent event, RollbackSourceCallback rollbackMethod) {
return handle(ex, event, rollbackMethod);
}
}
当我使用上述配置运行Mule服务器时,我没有得到我期望的输出。我的期望是default-exception-strategy应该在异常目录中生成一个文件,但这不会发生。 Java策略应该将消息记录到控制台中,但同样,我看不到任何东西......置于句柄方法中的断点根本不会触发。
提前感谢您的帮助。
答案 0 :(得分:1)
问题在于Mule版本。流程在3.3.0-R3中正常工作。
流程:
<default-exception-strategy>
<processor-chain>
<custom-transformer class="exception.StackTraceTransformer"/>
<file:outbound-endpoint path="data/exception" outputPattern="#[function:datestamp:yyyyMMddHHmmss].txt"/>
</processor-chain>
</default-exception-strategy>
自定义变压器:
public class StackTraceTransformer extends AbstractMessageTransformer {
public StackTraceTransformer() {
setName("StackTraceToText");
}
@Override
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
StringWriter writer = new StringWriter();
PrintWriter stream = new PrintWriter(writer);
Throwable t = ((ExceptionMessage) message.getPayload()).getException();
t.printStackTrace(stream);
return writer.toString();
}
}