Spring集成 - 错误通道处理问题

时间:2011-09-15 07:18:16

标签: spring spring-integration

我是Spring Integeration的新手。我有一个要求使用spring integeration

  1. 读取txt文件(来自Source文件夹)
  2. 做一些验证
  3. 如果验证成功 - 写入成功文件(在sucess文件夹中)
  4. 如果验证失败 - 写入失败文件(在错误文件夹中)
  5. 如果文件格式不正确,则意味着我必须将该文件移动到错误文件夹中(例外列为2,但在我的文件中包含列为1)
  6. 我的配置文件是这样的

         <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:si="http://www.springframework.org/schema/integration"
            xmlns:file="http://www.springframework.org/schema/integration/file"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/integration
                    http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
                    http://www.springframework.org/schema/integration/file
                    http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd">
    
            <bean id="checkCSVReader"
                class="com.check.wrapper">
                <property name="pzMapXML" value="classpath:sampleFileFormat.xml" />
            </bean>
    
            <bean id="checkTrasnFomer"
                class="com.check.checkTransfomer">
                <property name="wrapper" ref="checkCSVReader" />
            </bean>
    
            <bean id="fileErrorProcessor"
                class="com.check.ErrorChannelWriter">
            </bean>
            <bean id="listToStringTrans"
                class="com.check.ListToStringTransfomer"></bean>
    
    
        <bean id="validation"
                class="com.check.Validation"/>
    
            <file:inbound-channel-adapter directory="file://D:\check\soruce"   prevent-duplicates="false" 
                auto-create-directory="true" channel="readChannel" >
                <si:poller id="Poller">
                    <si:interval-trigger interval="10000" />
                </si:poller>
            </file:inbound-channel-adapter>
    
            <si:channel id="readChannel" />
    
            <si:chain input-channel="readChannel" output-channel="processChannel">
                <si:header-enricher error-channel="errorFile" />
                <file:file-to-string-transformer />
                <si:transformer ref="checkTrasnFomer" method="transform" />
                <si:service-activator ref="validation"
                    method="validate" />
            </si:chain>
    
            <si:channel id="processChannel" />
    
            <si:transformer ref="listToStringTrans" method="transformList"
                input-channel="processChannel" output-channel="finalOut" />
    
            <si:channel id="finalOut" />
    
            <file:outbound-channel-adapter id="checkSuccFileOutBound"
                auto-create-directory="true" delete-source-files="true"
                directory="file://D:\check\success" channel="finalOut">
            </file:outbound-channel-adapter>
    
            <si:channel id="errorFile" />
    
            <si:transformer ref="fileErrorProcessor"
                input-channel="errorFile" output-channel="errorChannel" method="transformError" />
    
            <file:outbound-channel-adapter id="errorChannel"
                directory="file://D:\check\error" delete-source-files="true"
                 />
    
            <si:channel id="checkFileErr" />
        </beans>
    

    我的checkFlatPackCVSParserWrapper类是

        public class checkFlatPackCVSParserWrapper {
            private static final Log LOG = LogFactory.getLog("checkFlatPackCVSParserWrapper");
            private Resource pzMapXML;
            private char delimiter = ',';
            private char qualifier = '"';
            private boolean ignoreFirstRecord = false;
    
            public Resource getPzMapXML() {
                return pzMapXML;
            }
            public void setPzMapXML(Resource pzMapXML) {
                this.pzMapXML = pzMapXML;
            }
            public char getDelimiter() {
                return delimiter;
            }
            public void setDelimiter(char delimiter) {
                this.delimiter = delimiter;
            }
            public char getQualifier() {
                return qualifier;
            }
            public void setQualifier(char qualifier) {
                this.qualifier = qualifier;
            }
            public boolean isIgnoreFirstRecord() {
                return ignoreFirstRecord;
            }
            public void setIgnoreFirstRecord(boolean ignoreFirstRecord) {
                this.ignoreFirstRecord = ignoreFirstRecord;
            }
    
            public Parser getParser(String csv) {
                if(LOG.isDebugEnabled())
                    LOG.debug("getParser: " + csv);
    
                Parser result = null;
                try {
                    result = DefaultParserFactory.getInstance().newDelimitedParser(
                            pzMapXML.getInputStream(), //xml column mapping
                            new ByteArrayInputStream(csv.getBytes()),  //txt file to parse
                            delimiter, //delimiter
                            qualifier, //text qualfier
                            ignoreFirstRecord);
    
                }catch (Exception e) {
                    if(LOG.isDebugEnabled())
                       LOG.debug("Unable to read file:  " + e );
                    throw new RuntimeException("File Parse exception");
                }   
                return result;
            }
        }
    

    sampleFileFormat.xml是

        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE PZMAP SYSTEM  "flatpack.dtd" >
        <PZMAP>
            <COLUMN name="FIRSTNAME" />
            <COLUMN name="LASTNAME" />
        </PZMAP> 
    
    
     and checkTransfomer is 
    
    
        public class checkTransfomer {
            private static final Log LOG = LogFactory.getLog(checkTransfomer.class);
            private CheckFlatPackCVSParserWrapper wrapper;
    
            public String transform(String csv) {
                Parser parser = wrapper.getParser(csv);
                if(LOG.isDebugEnabled()) {
                    LOG.debug("Parser is: " + parser);
                }        
                DataSet ds = parser.parse();
                ArrayList<Check> list = new ArrayList<Check>();
                while(ds.next()) {
                    Check check= new Check();
                    check.setFirstName(ds.getString("FIRSTNAME"));
                    check.setLastName(ds.getString("LASTNAME"));
                    if(LOG.isDebugEnabled()) {
                        LOG.debug("Bean value is: " + bean);
                    }        
                    list.add(bean);            
                }
                if(LOG.isDebugEnabled()) {
                    LOG.debug("Records fetched is: " + list.size());
                }        
                return list.toString();
            }
    
            public CheckFlatPackCVSParserWrapper getWrapper() {
                return wrapper;
            }
    
            public void setWrapper(CheckFlatPackCVSParserWrapper wrapper) {
                this.wrapper = wrapper;
            }
    

    我的ErrorChannelWriter是

        public class ErrorChannelWriter {
    
            public static final Log LOG = LogFactory.getLog(ErrorChannelWriter.class);
    
            public Message<?> transformError(ErrorMessage errorMessage) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Transforming errorMessage is: " + errorMessage);
                }
                return ((MessagingException) errorMessage.getPayload())
                        .getFailedMessage();
            }
            }
    

    我的验证类是

      com.check.Validation
    
      public class Validation 
      {
    
       void validation(CheckCheck)
       {
      if(Check.getFirstName().equals("maya"))
      {
      throw new RuntimeException("Name Already exist");
      }
    
    
    
      }
    
      }
    

    我的ListToStringTransfomer是

          public class ListToStringTransfomer {
            private static final Log LOG=LogFactory.getLog(ListToStringTransfomer.class);
    
    
            public String transformList(List<IssueAppBean> list) {
                return list.toString();
            }
    
        }
    

    我的文件包含一个字段而不是两个字段

    > maya 
    

    这里我的文件格式错误,因此记录正在移动到错误文件夹。但是没有错误消息。当文件格式不正确时,如何添加错误消息(TOO FEW COLUMNS WANTED:2 GOT:1)。 我的要求是在我的错误文件中应该加入

      

    maya -TOO少数专栏通缉:2 GOT:1或(任何错误信息)

    请给我任何解决方案

1 个答案:

答案 0 :(得分:1)

我认为您不应该通过错误通道来解决此要求。主要原因是在这种情况下无效输入是预期的情况。 errorChannel是Spring Integration在端点中发生意外异常时发送消息的通道。

如果在验证失败时向邮件添加一些标头,则可以根据该标头进行路由,并在那里记录失败消息。然后,您可以将错误消息发送到日志文件或您自己的任何内容。