Mule - 如果XML模式验证失败,如何让Mule抛出异常

时间:2014-04-15 10:26:53

标签: xml validation exception xsd mule

我正在使用Mule架构验证过滤器来针对给定方案验证传入的XML文件。下面是我对架构组件的配置:

<mulexml:schema-validation-filter schemaLocations="${app.home}/classes/schemas/Library.xsd" doc:name="Schema Validation"/>

我遇到的问题是,当文件无法对模式进行验证时,Mule会静默阻止消息继续 - 即不会抛出错误/异常。通过一些实验,我发现如果我将以下内容添加到我的log4j.properties文件中,我可以在模式验证中看到“错误”:

log4j.logger.org.mule.module.xml.filters.SchemaValidationFilter=DEBUG

由于这些错误是'DEBUG'级别日志并且没有抛出实际异常,因此我的流程不会恢复为我已配置的异常策略。

所以我的问题是......

如果模式验证中存在错误,我可以在这里做些什么来使Mule恢复为异常策略吗?我在文件无法验证时看到的调试日志下面有一份副本。提前谢谢。

DEBUG 2014-04-15 11:19:20,705 [[processes].orderInputFileConnector.receiver.01] org.mule.module.xml.filters.SchemaValidationFilter: SchemaValidationFilter rejected a message because it apparently failed to validate against the schema.
org.xml.sax.SAXParseException: cvc-pattern-valid: Value 'string' is not facet-valid with respect to pattern '[A-Z]{2}' for type 'countryISOA2'.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.elementLocallyValidType(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.processElementContent(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleEndElement(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.endElement(Unknown Source)
    at org.apache.xerces.jaxp.validation.DOMValidatorHelper.finishNode(Unknown Source)
    at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
    at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
    at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
    at org.mule.module.xml.filters.SchemaValidationFilter.accept(SchemaValidationFilter.java:107)
    at org.mule.routing.MessageFilter.accept(MessageFilter.java:89)
    at org.mule.processor.AbstractFilteringMessageProcessor.process(AbstractFilteringMessageProcessor.java:42)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:61)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:47)
    at org.mule.processor.chain.DefaultMessageProcessorChain.doProcess(DefaultMessageProcessorChain.java:95)
    at org.mule.processor.chain.AbstractMessageProcessorChain.process(AbstractMessageProcessorChain.java:70)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:47)
    at org.mule.processor.AbstractInterceptingMessageProcessorBase.processNext(AbstractInterceptingMessageProcessorBase.java:106)
    at org.mule.interceptor.AbstractEnvelopeInterceptor.process(AbstractEnvelopeInterceptor.java:55)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:61)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:47)
    at org.mule.processor.AbstractInterceptingMessageProcessorBase.processNext(AbstractInterceptingMessageProcessorBase.java:106)
    at org.mule.processor.AbstractFilteringMessageProcessor.process(AbstractFilteringMessageProcessor.java:44)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:61)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:47)
    at org.mule.processor.AbstractInterceptingMessageProcessorBase.processNext(AbstractInterceptingMessageProcessorBase.java:106)
    at org.mule.construct.AbstractPipeline$1.process(AbstractPipeline.java:112)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:61)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:47)
    at org.mule.processor.chain.DefaultMessageProcessorChain.doProcess(DefaultMessageProcessorChain.java:95)
    at org.mule.processor.chain.AbstractMessageProcessorChain.process(AbstractMessageProcessorChain.java:70)
    at org.mule.processor.chain.InterceptingChainLifecycleWrapper.doProcess(InterceptingChainLifecycleWrapper.java:54)
    at org.mule.processor.chain.AbstractMessageProcessorChain.process(AbstractMessageProcessorChain.java:70)
    at org.mule.processor.chain.InterceptingChainLifecycleWrapper.access$001(InterceptingChainLifecycleWrapper.java:26)
    at org.mule.processor.chain.InterceptingChainLifecycleWrapper$1.process(InterceptingChainLifecycleWrapper.java:70)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:61)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:47)
    at org.mule.processor.chain.InterceptingChainLifecycleWrapper.process(InterceptingChainLifecycleWrapper.java:65)
    at org.mule.construct.AbstractPipeline$3.process(AbstractPipeline.java:204)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:61)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:47)
    at org.mule.processor.chain.SimpleMessageProcessorChain.doProcess(SimpleMessageProcessorChain.java:47)
    at org.mule.processor.chain.AbstractMessageProcessorChain.process(AbstractMessageProcessorChain.java:70)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:47)
    at org.mule.processor.AbstractInterceptingMessageProcessorBase.processNext(AbstractInterceptingMessageProcessorBase.java:106)
    at org.mule.processor.AbstractFilteringMessageProcessor.process(AbstractFilteringMessageProcessor.java:44)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:61)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:47)
    at org.mule.processor.chain.SimpleMessageProcessorChain.doProcess(SimpleMessageProcessorChain.java:47)
    at org.mule.processor.chain.AbstractMessageProcessorChain.process(AbstractMessageProcessorChain.java:70)
    at org.mule.processor.chain.InterceptingChainLifecycleWrapper.doProcess(InterceptingChainLifecycleWrapper.java:54)
    at org.mule.processor.chain.AbstractMessageProcessorChain.process(AbstractMessageProcessorChain.java:70)
    at org.mule.processor.chain.InterceptingChainLifecycleWrapper.access$001(InterceptingChainLifecycleWrapper.java:26)
    at org.mule.processor.chain.InterceptingChainLifecycleWrapper$1.process(InterceptingChainLifecycleWrapper.java:70)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:61)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:47)
    at org.mule.processor.chain.InterceptingChainLifecycleWrapper.process(InterceptingChainLifecycleWrapper.java:65)
    at org.mule.transport.AbstractMessageReceiver.routeEvent(AbstractMessageReceiver.java:473)
    at org.mule.transport.AbstractMessageReceiver.routeMessage(AbstractMessageReceiver.java:226)
    at org.mule.transport.AbstractMessageReceiver.routeMessage(AbstractMessageReceiver.java:208)
    at org.mule.transport.AbstractMessageReceiver.routeMessage(AbstractMessageReceiver.java:200)
    at org.mule.transport.AbstractMessageReceiver.routeMessage(AbstractMessageReceiver.java:187)
    at org.mule.transport.file.FileMessageReceiver.moveAndDelete(FileMessageReceiver.java:560)
    at org.mule.transport.file.FileMessageReceiver.access$400(FileMessageReceiver.java:62)
    at org.mule.transport.file.FileMessageReceiver$2.process(FileMessageReceiver.java:410)
    at org.mule.transport.file.FileMessageReceiver$2.process(FileMessageReceiver.java:406)
    at org.mule.execution.ExecuteCallbackInterceptor.execute(ExecuteCallbackInterceptor.java:20)
    at org.mule.execution.HandleExceptionInterceptor.execute(HandleExceptionInterceptor.java:34)
    at org.mule.execution.HandleExceptionInterceptor.execute(HandleExceptionInterceptor.java:18)
    at org.mule.execution.BeginAndResolveTransactionInterceptor.execute(BeginAndResolveTransactionInterceptor.java:58)
    at org.mule.execution.ResolvePreviousTransactionInterceptor.execute(ResolvePreviousTransactionInterceptor.java:48)
    at org.mule.execution.SuspendXaTransactionInterceptor.execute(SuspendXaTransactionInterceptor.java:54)
    at org.mule.execution.ValidateTransactionalStateInterceptor.execute(ValidateTransactionalStateInterceptor.java:44)
    at org.mule.execution.IsolateCurrentTransactionInterceptor.execute(IsolateCurrentTransactionInterceptor.java:44)
    at org.mule.execution.ExternalTransactionInterceptor.execute(ExternalTransactionInterceptor.java:52)
    at org.mule.execution.RethrowExceptionInterceptor.execute(RethrowExceptionInterceptor.java:32)
    at org.mule.execution.RethrowExceptionInterceptor.execute(RethrowExceptionInterceptor.java:17)
    at org.mule.execution.TransactionalErrorHandlingExecutionTemplate.execute(TransactionalErrorHandlingExecutionTemplate.java:113)
    at org.mule.execution.TransactionalErrorHandlingExecutionTemplate.execute(TransactionalErrorHandlingExecutionTemplate.java:34)
    at org.mule.transport.file.FileMessageReceiver.processWithoutStreaming(FileMessageReceiver.java:405)
    at org.mule.transport.file.FileMessageReceiver.processFile(FileMessageReceiver.java:397)
    at org.mule.transport.file.FileMessageReceiver.poll(FileMessageReceiver.java:238)
    at org.mule.transport.AbstractPollingMessageReceiver.performPoll(AbstractPollingMessageReceiver.java:219)
    at org.mule.transport.PollingReceiverWorker.poll(PollingReceiverWorker.java:84)
    at org.mule.transport.PollingReceiverWorker.run(PollingReceiverWorker.java:53)
    at org.mule.work.WorkerContext.run(WorkerContext.java:311)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

更新

根据@RyanCarter提供的答案(有效),我还发现以下解决方案在Mule Studio中没有任何错误。它涉及全局定义模式验证过滤器并在消息过滤器中引用它,如下所示:

<mulexml:schema-validation-filter name="myValidator".../>

<flow>          
    <message-filter throwOnUnaccepted="true">
       <filter ref="myValidator"/>
    </message-filter>

3 个答案:

答案 0 :(得分:8)

您可以将架构验证过滤器打包在消息过滤器中,并将 throwOnUnaccepted 设置为&#34; true&#34; :

<message-filter throwOnUnaccepted="true">
   <mulexml:schema-validation-filter... />
</message-filter>

然后,您可以使用异常策略来处理异常。

答案 1 :(得分:1)

过滤器的目的是在条件不满足时停止处理。他们通常不会抛出异常并扰乱流程。如果你必须获得异常并处理它,那么尝试在过滤器中使用errorHandler-ref属性并将其指向可以处理错误的spring bean。

<mulexml:schema-validation-filter  errorHandler-ref="" ></mulexml:schema-validation-filter>

希望这有帮助。

答案 2 :(得分:-1)

如果你想使用errorHandler-ref =&#34;&#34;在模式验证中,只需创建一个实现ErrorHandler的java组件。在java组件中,您可以处理任何类型的验证错误并相应地处理。这是代码段

<mulexml:schema-validation-filter
    schemaLocations="abc.xsd"
    errorHandler-ref="Bean" returnResult="true" doc:name="Schema Validation"
    name="xmlvalidation" schemaLanguage="http://www.w3.org/2001/XMLSchema" />

在Mule中创建了BEAN -

<spring:beans>
  <spring:bean id="Bean" name="Bean" class="com.training.ErrorHandler"/>
</spring:beans>

创建一个java类并实现&#34; org.xml.sax.ErrorHandler&#34;如下所示 -

package com.training;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ErrorHandler implements org.xml.sax.ErrorHandler{

public ErrorHandler() {
    super();
    // TODO Auto-generated constructor stub
}

@Override
public void warning(SAXParseException exception) throws SAXException {
    System.out.println("do something here");

}

@Override
public void error(SAXParseException exception) throws SAXException {
    System.out.println("do something here");


}

@Override
public void fatalError(SAXParseException exception) throws SAXException {
    System.out.println("do something here");

}

}