4

我在 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 策略应该将消息记录到控制台中,但同样,我什么也看不到...放置在句柄方法中的断点也根本不会触发。

提前感谢您的帮助。

4

1 回答 1

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();
    }
}
于 2012-05-22T15:41:32.273 回答