当前位置: 首页 > 知识库问答 >
问题:

如何在Mule中处理SOAP Web Service中的异常策略

司空鸣
2023-03-14

我有一个关于Mule 3.3.1 CE中Web服务的问题。我有一个Web服务,它公开了三个操作和一个实现这些操作类。这些操作可以返回结果(积极)或异常(AuthExeception,ValidateExeception,等等)。多亏了SOAP Mule组件,当我提出一个Java异常时,框架能够将Java异常编组在一个SOAP故障中,但是如果我想既返回一个SOAP故障给客户机,又用Mule中的异常策略处理该异常(即发送一封电子邮件),那么Mule行为就不是我能期望的了。换句话说,例如,当我提出AuthException时,流控制传递给定义的异常策略,并且我不能再将SOAP错误(AuthException)发送回客户端。

下面是mule 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> 

我读过一些关于处理策略的东西,但我不知道这是不是正确的方法。非常感谢你的帮助。

共有1个答案

丁曦
2023-03-14

下面是我的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;
    }
}
 类似资料:
  • 我的骡子流有点像下面这样:- 现在我的问题出现在SOAP请求中,ID和AGE属性是整数,如果我放入任何字符串值,如,它会抛出错误,如org.apache.CXF.interceptor.Fault:Unmarshalling error:不是一个很自然的数字...现在我如何处理这个并发送自定义消息作为响应...我尝试使用Mule中的catch exception block但我无法处理这个CXF

  • 我目前正在尝试为spring boot实现一个自定义的错误处理程序,我已经用以下方法实现了它: 不知为什么这不起作用,并且异常仍然被抛给客户端,是否有某种方法捕获方法抛出的异常并忽略它。

  • 我试图抓住无效的json,而解析它与jiffy在牛仔web套接字处理程序。如果json是有效的/无效的,我想转发一个适当的消息到,它将回复客户端。这是我的代码。 这会导致运行时异常。 12:07:48.406[错误]牧场侦听器http已连接到进程 那我该怎么做呢?

  • 问题内容: 我知道callable的调用可能会将异常抛出给调用它的父方法,而runnable则不是这种情况。 我不知道如何,因为它是线程方法,并且是线程堆栈的最底层方法。 问题答案: 的要点是将异常抛出到调用线程,例如,当您获得提交的结果时。

  • 这个问题要求解释在各种语言中如何在后台实现异常处理,但没有收到Python的任何回应。 我对Python特别感兴趣,因为Python通过EAFP原则“鼓励”异常抛出和捕捉。 我从其他SO答案中了解到,如果预计很少引发异常,try/catch块比if/etc语句更便宜,并且调用深度很重要,因为填充stacktrac很昂贵。这可能主要适用于所有编程语言。 python的特殊之处在于EAFP原则的高优先

  • 我们正在将Oracle Weblogic server 8.1升级到Weblogic server 12c,并将java 1.4升级到1.8 我的任务是确保应用程序功能保持不变。一些应用程序自2007年以来就没有碰过。 前面的代码是: 搜索“weblogic.jar”(在“\Oracle\Middleware\Oracle\u Home\wlserver\server\lib”中找到),我看到它没