1

我是 Spring 集成的新手。我有一个要求使用 spring 集成

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

我的配置文件是这样的

     <?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 

这里我的文件格式错误,所以记录正在移动到错误文件夹。但没有错误消息。当我的文件格式不正确时,如何添加错误消息(想要的列太少:2 GOT:1)。我的要求在我的错误文件中应该包含

maya - 想要的列太少:2 GOT:1 或(任何错误消息)

请给我任何解决方案

4

1 回答 1

1

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

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

于 2011-12-20T15:03:24.900 回答