1

Mule 3.3.1 CE の Web サービスについて質問があります。3 つの操作を公開する Web サービスと、これらの操作を実装するクラスがあります。これらの操作は、結果 (正) または例外 (AuthException、ValidateExeception など) を返すことができます。Java 例外を発生させたときに SOAP Mule コンポーネントのおかげで、フレームワークは SOAP Fault で Java 例外をマーシャリングできますが、SOAP Fault をクライアントに返し、Mule の例外戦略で例外を処理したい場合 (つまり、電子メールを送信するなど)、Mule の動作は期待できません。つまり、たとえば AuthException を発生させると、フロー制御が定義された例外戦略に渡され、クライアントに SOAP エラー (AuthException) を返すことができなくなります。

問題は、SOAP 応答を送り返し、例外戦略を処理するにはどうすればよいかということです。

以下に示すのは、Logger コンポーネントを使用して単純に例外戦略を実装したミュール xml ファイルのスニペットです。

<flow name="esb_consignmentFlow1" doc:name="esb_consignmentFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="${conn.host}" port="8081" doc:name="HTTP" path="sgb/consignment" />
        <cxf:jaxws-service doc:name="Process SOAP Request" serviceClass="com.suzuki.sales.webservice.ProcessTriggerPortType"/>
        <component class="it.aizoon.suzuki.service.implementation.TriggerConsignmentOperationsImplementation" doc:name="triggerConsignmentOperationsImpl"/>
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <flow-ref name="ErrorHandling" doc:name="Flow Reference"/>
        </catch-exception-strategy>
</flow>

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

Processing Strategy について読んだことがありますが、それが正しい方法かどうかわかりません。ご助力ありがとうございます。

4

2 に答える 2

2

ここに私のflow.xmlがあります

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf"   xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:http="http://www.mulesoft.org/schema/mule/http" 
        xmlns="http://www.mulesoft.org/schema/mule/core" 
        xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd 
http://www.mulesoft.org/schema/mule/smtp http://www.mulesoft.org/schema/mule/smtp/current/mule-smtp.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd ">
    <spring:beans>
        <spring:bean id="consignmentProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <!-- In questo modo riesco a definire un file di property esterno che non è in una locazione hard-coded -->
            <spring:property name="ignoreUnresolvablePlaceholders" value="true"/>
            <spring:property name="locations">
                <spring:list>
                    <spring:value>classpath:esb_consignment.properties</spring:value>
                    <spring:value>classpath:connections.properties</spring:value>
                    <spring:value>classpath:email.properties</spring:value>
                    <!-- L'ultimo nella lista è il più generico perché sovrascrive le properties -->
                </spring:list>
            </spring:property>
        </spring:bean>
        <spring:bean id="outfaultInterceptor" class="it.aizoon.suzuki.service.interceptors.CustomSoapFaultOutInterceptor">
            <spring:property name="outputQueue" value="ErrorHandler"/>
        </spring:bean>
    </spring:beans>

    <flow name="TriggerConsignmentInterceptorService" doc:name="TriggerConsignmentInterceptorService">
        <http:inbound-endpoint exchange-pattern="request-response" host="${conn.host}" port="${conn.port}" doc:name="HTTP" path="sgb/consignment" />
        <cxf:jaxws-service doc:name="Process SOAP Request" serviceClass="com.suzuki.sales.webservice.ProcessTriggerPortType">
            <cxf:outFaultInterceptors>
                <spring:ref bean="outfaultInterceptor"/>
            </cxf:outFaultInterceptors>        
        </cxf:jaxws-service>
        <component class="it.aizoon.suzuki.service.implementation.TriggerConsignmentOperationsImplementation" doc:name="triggerConsignmentOperationsImpl"/>
    </flow>
    <flow name="ErrorHandler" doc:name="ErrorHandler">
        <vm:inbound-endpoint exchange-pattern="one-way" path="ErrorHandler" doc:name="Error Handler"/>
        <logger message="PAYLOAD: #[message.payload]" level="INFO" doc:name="Payload"/>
        <set-payload value="Tipo di Eccezione: #[message.payload]" doc:name="Set Payload"/>
        <smtp:outbound-endpoint host="${smtp.host}"
                                from="${email.fromAddress}"
                                to="${email.toAddress}" 
                                subject="${email.subject}" 
                                responseTimeout="10000" 
                                doc:name="Email Notification"/>
        <logger message="EMAIL SENT" level="INFO" doc:name="Result"/>
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <logger message="ERRORE INVIO EMAIL" level="INFO" doc:name="Logger"/>
        </catch-exception-strategy>
    </flow>

</mule>

以下は、SOAP 応答と例外戦略を処理するインターセプターです。

package it.aizoon.suzuki.service.interceptors;

import javax.xml.bind.UnmarshalException;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.mule.DefaultMuleMessage;
import org.mule.api.MuleContext;
import org.mule.api.MuleEvent;
import org.mule.api.MuleException;
import org.mule.api.client.MuleClient;
import org.mule.api.context.MuleContextAware;
import org.mule.module.cxf.CxfConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suzuki.sales.webservice.AuthenticationFailedException;
import com.suzuki.sales.webservice.ValidationFailedException;

public class CustomSoapFaultOutInterceptor extends AbstractPhaseInterceptor implements MuleContextAware{

    private static final Logger logger = LoggerFactory.getLogger(CustomSoapFaultOutInterceptor.class);

    private String outputQueue;
    private MuleContext context;


    public CustomSoapFaultOutInterceptor() {
        // TODO Auto-generated constructor stub
        super(Phase.MARSHAL);
    }

    @Override
    public void setMuleContext(MuleContext context) {
        // TODO Auto-generated method stub
        this.context = context;
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        // TODO Auto-generated method stub
        MuleClient client = context.getClient();
        MuleEvent event = (MuleEvent) message.getExchange().get(CxfConstants.MULE_EVENT);
        DefaultMuleMessage muleMessage = (DefaultMuleMessage) event.getMessage();

        Throwable genericExec = message.getContent(Exception.class).getCause();
        Throwable exception = null;

        if(genericExec instanceof ValidationFailedException){
            exception = (ValidationFailedException) genericExec;

        }else if(genericExec instanceof AuthenticationFailedException){
            exception = (AuthenticationFailedException) genericExec;

        }else if(genericExec instanceof UnmarshalException){
            exception = (UnmarshalException) genericExec;
        }


        try {

            muleMessage.setPayload(exception);
            client.send("vm://" + getOutputQueue(), muleMessage);

        } catch (MuleException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String getOutputQueue() {
        return outputQueue;
    }

    public void setOutputQueue(String outputQueue) {
        this.outputQueue = outputQueue;
    }

    public MuleContext getContext() {
        return context;
    }

    public void setContext(MuleContext context) {
        this.context = context;
    }
}
于 2013-06-06T16:11:17.043 に答える