3

我正在从 Mule 进行 SOAP 调用。我正在使用默认异常策略来捕获异常。发生异常时,如何发送我自己的故障代码和故障字符串而不是通用的肥皂故障消息?谢谢

这是 mule xml、java 代码、实际输出和预期输出的片段

骡子xml

<flow name="addressSearchFlow1" doc:name="addressSearchFlow1">
    <invoke object-ref="InvoiceManager" method="completeSearchObject"
        methodArguments="#[message.payload]"></invoke>

    <logger level="INFO" doc:name="Logger" />

    <processor-chain doc:name="Search Processor Chain">
        <cxf:jaxws-client operation="doSearch"
            clientClass="com.invoice.web_2007_09.ProWeb" port="InvoicePortType"
            wsdlLocation="proweb.wsdl" enableMuleSoapHeaders="true" doc:name="SOAP">
        </cxf:jaxws-client>
        <http:outbound-endpoint exchange-pattern="request-response"
            method="POST" ref="InvoiceEndPoint" doc:name="HTTP" />
    </processor-chain>

    <default-exception-strategy>
        <processor-chain>
            <logger message="Exception_logger" level="INFO" doc:name="Exception_logger" />
            <custom-transformer class="com.managers.CustomExceptionHandler" />
            <logger message="Exception_logger" level="INFO" doc:name="Exception_logger" />
        </processor-chain>
    </default-exception-strategy>

</flow>

Java 代码

package com.managers;

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.message.ExceptionMessage;
import org.mule.transformer.AbstractMessageTransformer;

public class CustomExceptionHandler extends AbstractMessageTransformer {

public CustomExceptionHandler() {
    setName("CustomExceptionHandler");
}

@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
        throws TransformerException {

    ExceptionMessage exceptionMessage = (ExceptionMessage)message.getPayload();

    String outputMessage = "<soap:Fault xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">        "
            + " <faultcode>soap:Server</faultcode> "
            + "<faultstring>"
            + "An unexpected error has occured. Please contact your service desk and quote this error \""  + exceptionMessage.getException() +  "\""+ "</faultstring>      " + "</soap:Fault>";
    return outputMessage;

}

}

实际输出


<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring> dt-invoice. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: PostMethod</faultstring>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

预期产出


<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring> An unexpected error has occured. Please contact your service desk and quote this error   "org.mule.api.transport.DispatchException: dt-invoice. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: PostMethod"
</faultstring>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>
4

2 回答 2

1

如下更新流程以开始使用捕获异常策略。

<flow name="addressSearchFlow1" doc:name="addressSearchFlow1">
    <invoke object-ref="InvoiceManager" method="completeSearchObject"
        methodArguments="#[message.payload]"></invoke>

    <logger level="INFO" doc:name="Logger" />

    <processor-chain doc:name="Search Processor Chain">
        <cxf:jaxws-client operation="doSearch"
            clientClass="com.invoice.web_2007_09.ProWeb" port="InvoicePortType"
            wsdlLocation="proweb.wsdl" enableMuleSoapHeaders="true" doc:name="SOAP">
        </cxf:jaxws-client>
        <http:outbound-endpoint exchange-pattern="request-response"
            method="POST" ref="InvoiceEndPoint" doc:name="HTTP" />
    </processor-chain>

    <catch-exception-strategy>          
        <logger message="Exception_logger" level="INFO" doc:name="Exception_logger" />
        <custom-transformer class="com.managers.CustomExceptionHandler" />
        <logger message="Exception_logger" level="INFO" doc:name="Exception_logger" />          
    </catch-exception-strategy>
</flow>

同样在自定义转换器中,消息中不存在异常作为有效负载。它在 exceptionPayload 中可用。因此,请使用 message.getExceptionPayload() ,它会为您提供 ExceptionPayload 作为结果。从 exceptionPayload 对象可以获取异常消息和其他详细信息。

希望这可以帮助。

于 2013-08-14T12:06:52.257 回答
1

您需要<catch-exception-strategy' and not '<default-exception-strategy>像这样在流程中:

<flow name="addressSearchFlow1" doc:name="addressSearchFlow1">
    <invoke object-ref="InvoiceManager" method="completeSearchObject"
        methodArguments="#[message.payload]"></invoke>

    <logger level="INFO" doc:name="Logger" />

    <processor-chain doc:name="Search Processor Chain">
        <cxf:jaxws-client operation="doSearch"
            clientClass="com.invoice.web_2007_09.ProWeb" port="InvoicePortType"
            wsdlLocation="proweb.wsdl" enableMuleSoapHeaders="true" doc:name="SOAP">
        </cxf:jaxws-client>
        <http:outbound-endpoint exchange-pattern="request-response"
            method="POST" ref="InvoiceEndPoint" doc:name="HTTP" />
    </processor-chain>

    <catch-exception-strategy>
        <processor-chain>
            <logger message="Exception_logger" level="INFO" doc:name="Exception_logger" />
            <custom-transformer class="com.managers.CustomExceptionHandler" />
            <logger message="Exception_logger" level="INFO" doc:name="Exception_logger" />
        </processor-chain>
    </catch-exception-strategy>

</flow>

catch-exception-strategy:定义一个 catch 异常策略来自定义 Mule 处理任何异常的方式。捕获异常策略会消耗入站消息。

default-exception-strategy:默认定义并隐式应用以处理在 Mule 应用程序中引发的所有消息传递异常

更多信息在mulesoft

于 2013-08-13T14:43:00.710 回答