0

我正在使用 Mule 社区版 3.4。我对 UntilSuccessful 组件有疑问。现在该场景已公开:我有一个由 UntilSuccessful 组件组成的流,其中有一个向 Web 服务发出请求的 SOAP 组件。在这个流程中,也有一个 ExcpetionStrategy。我遇到的问题是,当UntilSuccessful 内部(即SOAP 组件中)发生异常时,ExcpetionStrategy 无法处理它,因为它(抛出的异常)是由UntilSuccessful 组件内部的某种机制处理的。因为我需要在 ExcpetionStrategy 中处理异常,所以我想构建一个自定义出站拦截器(在 SOAP 组件内部)来拦截 SOAP 响应(如果它被抛出则异常)并且能够抛出异常以触发例外策略。谁能帮我解决这个问题?我试图阅读文档,但它很少,并且没有很好地解释如何构建自定义出站异常。我要做的是将抛出的异常的名称保存在某处(即,如果服务器抛出 NumberFormatException,我会将其名称保存在某处以便在 ExceptionStrategy 中使用它)

下面你可以看到一段 mule 配置文件:

<flow name="ProvaClient" doc:name="ProvaClient">
        <quartz:inbound-endpoint jobName="TalendJob" repeatInterval="5000" repeatCount="0" responseTimeout="10000" doc:name="Quartz">
            <quartz:event-generator-job>
                <quartz:payload>error</quartz:payload>
            </quartz:event-generator-job>
        </quartz:inbound-endpoint>
        <object-to-string-transformer doc:name="Object to String"/>
        <until-successful objectStore-ref="OS_Bean" maxRetries="2" secondsBetweenRetries="2" doc:name="Until Successful" deadLetterQueue-ref="myQueue">
            <processor-chain doc:name="Processor Chain: Wait For Web Service Response">
                <processor-chain doc:name="Processor Chain: Web Service">
                    <cxf:jaxws-client operation="getCode" clientClass="it.aizoon.prova.client.ProvaService" port="ProvaPort" enableMuleSoapHeaders="true" doc:name="SOAP">
                    </cxf:jaxws-client>
                      <http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="service/prova" method="POST" doc:name="HTTP"/>
                </processor-chain>
                <logger message="PAYLOAD: #[payload]" level="INFO" doc:name="Logger"/>
            </processor-chain>
        </until-successful>
        <catch-exception-strategy doc:name="Catch Exception Strategy">
           <!--   <processor ref="myExceptionHandler_id"/>  -->
            <logger message="EXCEPTION STRATEGY" level="INFO" doc:name="Logger"/>
        </catch-exception-strategy>
</flow>

在这里,您可以看到公开 Web 服务的服务器:

<flow name="provaServer" doc:name="provaServer">
        <http:inbound-endpoint exchange-pattern="request-response" doc:name="HTTP" host="localhost" path="service/prova" port="8081"/>
        <logger message="SERVER" level="INFO" doc:name="Logger"/>
        <cxf:jaxws-service serviceClass="it.aizoon.prova.Prova" doc:name="Process SOAP Request" />
        <component class="it.aizoon.prova.ProvaImpl" doc:name="Java"/>
</flow>

这里有 ProvaImpl.java,Web Service 的实现。如果在 getCode() 函数中作为参数传递的字符串是错误的,那么您如何看到,抛出异常,我希望它由客户端中定义的异常策略管理

@WebService(endpointInterface = "it.aizoon.prova.Prova",
                  serviceName = "Prova")
public class ProvaImpl implements Prova{

      @Override
      public String getCode(String code) throws NumberFormatException{
            // TODO Auto-generated method stub
            if(code.equals("error"))    throw new NumberFormatException();

            String str = "Andato a buon fine!";
            return str;
      }
}
4

1 回答 1

1

我会改变方法而不是使用拦截器。如果您需要调用异常策略而不首先触发直到成功的路由器,我会将您的 cxf:jaxws-client 等移动到私有流。引用关于私人流量的 Mule in Action 第 2 版:

这种解耦允许定义私有流本地的处理和错误处理策略。

<flow name="ProvaClient" doc:name="ProvaClient">
    ...
    <until-successful objectStore-ref="OS_Bean"
        maxRetries="2" secondsBetweenRetries="2" doc:name="Until Successful"
        deadLetterQueue-ref="myQueue">
        <processor-chain doc:name="Processor Chain: Wait For Web Service Response">
            <processor-chain doc:name="Processor Chain: Web Service">
                <flow-ref name="externalCallFlow" />
            </processor-chain>
            <logger message="PAYLOAD: #[payload]" level="INFO" doc:name="Logger" />
        </processor-chain>
    </until-successful>

    ...
</flow>
<flow name="externalCallFlow">
    <cxf:jaxws-client operation="getCode"
        clientClass="it.aizoon.prova.client.ProvaService" port="ProvaPort"
        enableMuleSoapHeaders="true" doc:name="SOAP">
    </cxf:jaxws-client>
    <http:outbound-endpoint exchange-pattern="request-response"
        host="localhost" port="8081" path="service/prova" method="POST"
        doc:name="HTTP" />
    <catch-exception-strategy doc:name="Catch Exception Strategy">
        <!-- Handle exception here locally and return custom exception or error 
            message for the unil-successful router -->
    </catch-exception-strategy>
</flow>

然后,您可以在本地处理异常并返回自定义异常或错误消息,以便直到成功的路由器使用以下属性捕获: failureExpression="exception-type:java.lang.NumberFormatException"

这是一个我敲出来抛出 NumberFormatException 的虚拟示例,在异常策略中记录异常并重试:

<flow name="test" doc:name="test">
        <http:inbound-endpoint address="http://localhost:8081/test"
            doc:name="HTTP" />

        <until-successful objectStore-ref="OS_Bean"
            maxRetries="2" secondsBetweenRetries="2" doc:name="Until Successful">
            <processor-chain doc:name="Processor Chain: Wait For Web Service Response">
                <processor-chain doc:name="Processor Chain: Web Service">
                    <flow-ref name="externalCallFlow" doc:name="Flow Reference" />
                </processor-chain>
            </processor-chain>
        </until-successful>

    </flow>
    <flow name="externalCallFlow" doc:name="externalCallFlow">
        <scripting:component>
            <scripting:script engine="groovy">
                throw new java.lang.NumberFormatException();
                </scripting:script>
        </scripting:component>
        <default-exception-strategy>
            <processor-chain>
                <logger level="ERROR"
                    message="NumberFormatException Occurred : #[message.payload.getException().getCause()]" />
                <scripting:component>
                    <scripting:script engine="groovy">
                        throw message.payload.getException().getCause();
                </scripting:script>
                </scripting:component>
            </processor-chain>
        </default-exception-strategy>
    </flow>
于 2013-05-15T13:35:36.453 回答