2013-05-15 47 views
0

我使用的是Mule Community Edition 3.4。 我有一個與UntilSuccessful組件有關的問題。該場景現已公開: 我有一個由UntilSuccessful組件構成的流程,其中有一個SOAP組件向Web服務發出請求。在這個流程中也有一個ExcpetionStrategy。我遇到的問題是,當UntilSuccessful內部發生異常(即在SOAP組件中)時,ExcpetionStrategy無法處理它,因爲它(Exception thrown)由UntilSuccessful組件內的某個機制處理。 因爲我需要在ExcpetionStrategy中處理異常,所以我想構建一個攔截SOAP響應的自定義出站攔截器(如果引發了異常,那麼它將引發異常以便觸發) ExcpetionStrategy。 任何人都可以幫助我解決這個問題嗎?我試圖閱讀文檔,但它很稀疏,並不能很好地解釋如何構建自定義出站異常。 我想要做的是保存某個地方的拋出異常的名稱(即如果服務器拋出一個NumberFormatException,我會保存它的名字以便在ExceptionStrategy中使用它)用SOAP Web Service構建自定義攔截器MULE

下面你可以看到一段代碼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服務的實現。怎麼可以看到,如果字符串作爲參數中引用代碼()函數是錯誤時,拋出一個異常,我想,這是由客戶端定義的例外策略

@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; 
     } 
} 

回答

1

我會改變的辦法管理通過而不是使用攔截器。如果您需要調用異常策略,而不是首先觸發直到成功的路由器,我會將您的cxf:jaxws-client等移動到私有流。引用Mule in Action第二版專有流程:

此解耦允許定義處理和錯誤處理 私有流程的本地策略。

<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 =「異常型: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> 
+0

首先感謝你爲重sponse。如果getCode()返回像NumberFormatException這樣的異常,那麼如何在異常策略中的消息中將此異常名稱設置爲負載?我的目標是發送帶有例外的郵件。我不想獲取SOAPFaultException,但NumberFormatException –

+0

更新了答案,以及如何設置有效負載或異常等。 –

+0

對不起Ryan,更新在哪裏? –