mule2.2.x架构(五)示例学习errorHandle

mule2.2.x架构(五)示例学习errorHandle

所有的示例文档
http://www.mulesoft.org/display/MULE2INTRO/Examples

本示例参考地址
http://www.mulesoft.org/display/MULE2INTRO/Error+Handler+Example

5.1 从目录扫描文件输出
配置文件error-config.xml,其中很多地方做了些调整
<!-- spring管理的bean -->
<spring:bean id="errorManager"
class="com.sillycat.easymule.errorhandler.services.ErrorManager">
<spring:property name="handlers">
   <spring:list>
    <spring:ref local="fatalHandler" />
    <spring:ref local="defaultHandler" />
    <spring:ref local="businessHandler" />
   </spring:list>
</spring:property>
</spring:bean>
<!-- 默认异常处理,输出到文件 -->
<spring:bean id="defaultHandler"
class="com.sillycat.easymule.errorhandler.handlers.DefaultHandler">
<spring:property name="endpointName" value="ExceptionFileWriter" />
<spring:property name="errorManager">
   <spring:ref local="errorManager" />
</spring:property>
</spring:bean>
<!-- 严重错误异常,输出到邮件 -->
<spring:bean id="fatalHandler"
class="com.sillycat.easymule.errorhandler.handlers.FatalHandler">
<spring:property name="endpointName" value="ExceptionEmailer" />
<spring:property name="errorManager">
   <spring:ref local="errorManager" />
</spring:property>
</spring:bean>
<!-- 业务异常处理,输出到队列 -->
<spring:bean id="businessHandler"
class="com.sillycat.easymule.errorhandler.handlers.BusinessHandler">
<spring:property name="endpointName" value="ExceptionQueue" />
<spring:property name="errorManager">
   <spring:ref local="errorManager" />
</spring:property>
</spring:bean>
<!-- 读取配置properties -->
<context:property-placeholder location="errorhandler/email.properties" />
<jms:activemq-connector name="JMSConnector" />
<!-- 文件链接 -->
<file:connector name="FileConnector" pollingFrequency="1000"
   moveToDirectory="${file.out}" outputPattern="[DATE].xml" />
<email:string-to-email-transformer
   name="StringToEmailMessage" />
<jms:object-to-jmsmessage-transformer
   name="ObjectToJMSMessage" />
<jms:jmsmessage-to-object-transformer
   name="JMSMessageToObject" />
<xm:xml-to-object-transformer name="XMLToExceptionBean"
   returnClass="com.sillycat.easymule.errorhandler.model.ExceptionBean" />
<xm:object-to-xml-transformer name="ExceptionBeanToXML"
   returnClass="java.lang.String" />
<custom-transformer name="ExceptionBeanToErrorMessage"
   class="com.sillycat.easymule.errorhandler.transformer.ExceptionBeanToErrorMessage"
   returnClass="com.sillycat.easymule.errorhandler.model.ErrorMessage" />
<custom-transformer name="ErrorMessageToException"
   class="com.sillycat.easymule.errorhandler.transformer.ErrorMessageToException"
   returnClass="java.lang.Throwable" />
<custom-transformer name="ErrorMessageToExceptionBean"
   class="com.sillycat.easymule.errorhandler.transformer.ErrorMessageToExceptionBean"
   returnClass="com.sillycat.easymule.errorhandler.model.ExceptionBean" />
<model name="errorhandler-test">
<service name="Error Manager">
   <inbound>
    <!-- 读入properties中配置的file.in目录下的*.xml文件 -->
    <inbound-endpoint address="${file.in}"
     transformer-refs="XMLToExceptionBean ExceptionBeanToErrorMessage">
     <file:filename-wildcard-filter
      pattern="*.xml" />
    </inbound-endpoint>
   </inbound>
   <!-- 调用spring的bean处理,默认调用了ErrorManager里面的onException方法,这里还没有搞清楚?有点迷惑 -->
   <pooled-component>
    <prototype-object
     class="com.sillycat.easymule.errorhandler.services.ErrorManager">
     <properties>
      <spring:entry key="handlers">
       <spring:list>
        <spring:ref local="fatalHandler" />
        <spring:ref local="defaultHandler" />
        <spring:ref local="businessHandler" />
       </spring:list>
      </spring:entry>
     </properties>
    </prototype-object>
   </pooled-component>
   <outbound>
    <filtering-router>
     <!--默认错误输出到文件 -->
     <file:outbound-endpoint path="test-data/exceptions"
     outputPattern="Exception-[UUID].xml" transformer-refs="ErrorMessageToExceptionBean ExceptionBeanToXML" />
     <expression-filter evaluator="groovy"
      expression="payload.throwable instanceof org.mule.api.DefaultMuleException" />
    </filtering-router>
    <filtering-router>
     <!-- 输出到邮箱 -->
     <smtp:outbound-endpoint user="${smtp.username}"
      password="${smtp.password}" host="${smtp.host}" port="${smtp.port}"
      to="${email.toAddress}" from="${email.fromAddress}" subject="${email.subject}"
      transformer-refs="ErrorMessageToExceptionBean ExceptionBeanToXML StringToEmailMessage" />
     <expression-filter evaluator="groovy"
      expression="payload.throwable instanceof org.mule.api.lifecycle.FatalException" />
    </filtering-router>
     <!-- 输出到jms队列 -->
    <filtering-router>
     <outbound-endpoint address="jms://exception.queue"
      transformer-refs="ErrorMessageToExceptionBean ExceptionBeanToXML ObjectToJMSMessage" />
     <expression-filter evaluator="groovy"
    expression="payload.throwable instanceof com.sillycat.easymule.errorhandler.exceptions.BusinessException" />
    </filtering-router>
    <custom-catch-all-strategy
     class="org.mule.routing.LoggingCatchAllStrategy" />
   </outbound>
</service>

<service name="Business Error Manager">
   <inbound>
    <!-- 接受jms队列 -->
    <inbound-endpoint address="jms://exception.queue"
     transformer-refs="JMSMessageToObject XMLToExceptionBean ExceptionBeanToErrorMessage" />
   </inbound>
   <!-- 处理队列中的消息 -->
   <component class="com.sillycat.easymule.errorhandler.services.BusinessErrorManager" />
</service>
</model>

对于component和pooled-component还有待认识。
这个示例中有spring bean的配置,同时有邮箱smtp协议,jms等的调用。

你可能感兴趣的:(spring,bean,xml,配置管理,jms)