mule esb exception(一)

mule esb 异常分类

  1. system exception
  2. message exception

系统异常出现的两种情况:

  1. 应用启动时出的异常
  2. 连接外部系统时出现的异常
    for example:读取文件,当文件正在写入时,file connector endpoint去读这个文件,出现的异常即为系统异常,stackoverflow也有相关参考
    系统异常相当于java中的Error
    由于系统异常处于应用不可用状态,重点分析 message exception
    Message Exception
    message exception适用如下情况:
    mule esb exception(一)_第1张图片
    信息源–>信息处理–>…n–>信息处理时如果处理的信息发生异常,就调用异常策略处理异常,异常中也可以有多个信息处理,然后到输出端(outbound message)
    废话说了这么多,搞技术的重应用直接上例子
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp" xmlns:file="http://www.mulesoft.org/schema/mule/file" 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="EE-3.6.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd http://www.mulesoft.org/schema/mule/smtp http://www.mulesoft.org/schema/mule/smtp/current/mule-smtp.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.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">
    <!-- Typical Connector for Inbound Endpoint: Read files -->
    <file:connector name="input" autoDelete="true" pollingFrequency="1000" fileAge="600000" moveToDirectory="D:\mule\import\backup" moveToPattern="#[message.inboundProperties['originalFilename']].backup" doc:name="File" />
    <!-- Typical Connector for Outbound Endpoint: Write files -->
    <file:connector name="output" doc:name="File" />
    <catch-exception-strategy name="Catch_Exception_Strategy" >
    <logger message=" 文件名: #[message.inboundProperties['originalFilename']] 在 #[server.dateTime.format(&quot;yyyy.MM.dd HH:mm:ss '出现异常信息' &quot;)] #[exception]" level="INFO" doc:name="Logger"/>
    <set-payload value="文件名: #[message.inboundProperties['originalFilename']] 在 #[server.dateTime.format(&quot;yyyy.MM.dd HH:mm:ss '出现异常信息' &quot;)] #[exception]" doc:name="Set Payload"/>
    <smtp:outbound-endpoint host="smtp.mailserver.com" user="host" password="*****" to="[email protected]" from="[email protected]" subject="testfile" responseTimeout="10000" encoding="GB18030" mimeType="text/html" doc:name="SMTP"/>  
    </catch-exception-strategy>
    <configuration defaultExceptionStrategy-ref="Catch_Exception_Strategy" doc:name="Configuration" doc:description="Use as implicit default exception strategy."/> 
    <flow name="fileimport">
        <file:inbound-endpoint connector-ref="input" path="D:\mule\import\input" doc:name="File" responseTimeout="10000"> 
        <file:filename-wildcard-filter pattern="导出*.doc" />
        </file:inbound-endpoint>
        <!-- <null-component /> -->
        <file:outbound-endpoint connector-ref="output" path="D:\mule\import\output" doc:name="File" responseTimeout="10000" />

    </flow>

</mule>

以上例子测试ok,修改了发邮件部分,当然不能暴漏我的邮件服务器啦

<file:connector name="input"  autoDelete="true" pollingFrequency="1000" fileAge="600000" moveToDirectory="D:\mule\import\backup" moveToPattern="#[message.inboundProperties['originalFilename']].backup"   doc:name="File" />
<file:connector name="output"  doc:name="File" />

以上代码是 file connector inbound 参考这里

<catch-exception-strategy name="Catch_Exception_Strategy" >
    <logger message=" 文件名: #[message.inboundProperties['originalFilename']] 在 #[server.dateTime.format(&quot;yyyy.MM.dd HH:mm:ss '出现异常信息' &quot;)] #[exception]" level="INFO" doc:name="Logger"/>
    <set-payload value="文件名: #[message.inboundProperties['originalFilename']] 在 #[server.dateTime.format(&quot;yyyy.MM.dd HH:mm:ss '出现异常信息' &quot;)] #[exception]" doc:name="Set Payload"/>
    <smtp:outbound-endpoint host="smtp.mailserver.com" user="host" password="*****" to="[email protected]" from="[email protected]" subject="testfile" responseTimeout="10000"  encoding="GB18030" mimeType="text/html" doc:name="SMTP"/>  
    </catch-exception-strategy>
    <configuration defaultExceptionStrategy-ref="Catch_Exception_Strategy" doc:name="Configuration" doc:description="Use as implicit default exception strategy."/> 

以上是对应的异常处理:

<logger message=" 文件名: #[message.inboundProperties['originalFilename']] 在 #[server.dateTime.format(&quot;yyyy.MM.dd HH:mm:ss '出现异常信息' &quot;)] #[exception]" level="INFO" doc:name="Logger"/>

捕获异常后打印对应的日志信息

<set-payload value="文件名: #[message.inboundProperties['originalFilename']] 在 #[server.dateTime.format(&quot;yyyy.MM.dd HH:mm:ss '出现异常信息' &quot;)] #[exception]" doc:name="Set Payload"/>

邮件内容信息在 payload中显示,所有赋值payload

<smtp:outbound-endpoint host="smtp.mailserver.com" user="host" password="*****" to="[email protected]" from="[email protected]" subject="testfile" responseTimeout="10000"  encoding="GB18030" mimeType="text/html" doc:name="SMTP"/>  

发邮件输出异常日志信息

<configuration defaultExceptionStrategy-ref="Catch_Exception_Strategy" doc:name="Configuration" doc:description="Use as implicit default exception strategy."/> 

这段非常重要:上面定义的异常策略是为了后期引用,以上代码 指定默认的异常策略为 以上定义的异常:通过名称Catch_Exception_Strategy引用定义异常

以上是个完整的flow例子,一般情况下不会抛出异常,如果我想测试一下,抛出异常后是否会发邮件:
只能手工抛出异常:详细参考这里
mule esb exception(一)_第2张图片
添加如上代码
看这个类的实现:

/* *(c) 2003-2014 MuleSoft, Inc. This software is protected under international copyright *law. All use of this software is subject to MuleSoft's Master Subscription Agreement *(or other master license agreement) separately entered into in writing between you and *MuleSoft. If such an agreement is not in place, you may not use the software. */
package org.mule.component.simple;

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;

/** * <code>NullComponent</code> is a service that is used as a placeholder. This * implementation will throw an exception if a message is received for it. */
public class NullComponent implements Callable {

    public Object onCall(MuleEventContext context) throws Exception
    {
        throw new UnsupportedOperationException("This service cannot receive messages. Service is: "
                                                + context.getFlowConstruct().getName());
    }

}

会抛出:UnsupportedOperationException异常
这样就会调用邮件处理输出端发送邮件:

打印异常信息如下:
这里写图片描述
先简单写到这里,后期再谈 mule esb exception

你可能感兴趣的:(exception,File,mule-esb)