SPRING BATCH : commit interval changing according to data in input file

我与影子孤独终老i 提交于 2019-12-12 05:38:06

问题


Currently my batch has a static commit-interval (1000). We asked me to change it in order to commit once a data in my flat file get changed. So I should have a reader which reads lines from flat file, once it notice that this data has changed, it should process the read lines and writes in database.

I tried with completionPolicy as follows:

  1. ReceptionCompletionPolicy.java:

    public class ReceptionCompletionPolicy extends SingleItemPeekableItemReader<ReceptionLineFieldHelper> implements CompletionPolicy{
    
        private ReceptionLineFieldHelper current;
    
        public boolean isComplete(RepeatContext context) {
            return ((ReaderRepeatContext) context).isComplete();
        }
    
    
        public boolean isComplete(RepeatContext context, RepeatStatus result) {
            return ((ReaderRepeatContext) context).isComplete();
        }
    
    
        public RepeatContext start(RepeatContext parent) {
            /*
             * Set first item of the chunk for later comparison.
             */
            this.current = invokePeek();
            return new ReaderRepeatContext(parent);
        }
    
    
        public void update(RepeatContext context) {
            /*
             * Check if the step should finish.
             * In this case when there are no more records to process.
             */
            if (current == null) {
                context.setCompleteOnly();
            }
        }
    
        private ReceptionLineFieldHelper invokePeek() {
            ReceptionLineFieldHelper peeked = null;
            try {
                peeked = peek();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return peeked;
        }
    
        //custom RepeatContext
        protected class ReaderRepeatContext extends RepeatContextSupport{
    
            public ReaderRepeatContext(RepeatContext parent) {
                super(parent);
            }
    
            public boolean isComplete() {
                ReceptionLineFieldHelper next = null;
                next = invokePeek();
    
                if (next == null || !(next.getCode().equals(current.getCode()))) {
                    current = next;
                    return true;
                }
                return false;
            }               
        }
    }
    
  2. ReceptionReader.java:

    public class ReceptionReader implements ItemReader {
    
        public Object read() {
            return this.filterFile(inputFile);
        }
    }
    
  3. reception.xml:

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:batch="http://www.springframework.org/schema/batch"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/batch
        http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">
    
        <!-- import config Spring générale -->
        <import
            resource="classpath*:spring/batch-spring.xml" />
    
        <batch:job id="reception" parent="simpleJob"
            job-repository="jobRepository">
    
    
            <!-- Déclaration step T01 -->
            <batch:step id="t01" parent="simpleStep">
                <batch:tasklet transaction-manager="transactionManager">
                    <batch:chunk reader="receptionReader"
                        writer="receptionProcessor"
                        chunk-completion-policy="receptionCompletionPolicy" />
                </batch:tasklet>
            </batch:step>
        </batch:job>
    
        <!-- 
            *****************
            *   ETAPE T01   *
            *****************   -->
        <!-- reader -->
        <bean id="receptionReader"
            class="com.reception.reader.ReceptionReader">
        </bean>
    
        <!-- writer -->
        <bean id="receptionProcessor"
            class="com.reception.processor.ReceptionProcessor">
        </bean>
    
        <!-- completionPolicy -->
        <bean id="receptionCompletionPolicy"
            class="com.reception.completionPolicy.ReceptionCompletionPolicy">
        </bean>
    </beans>
    

Apparently I have problem with completionPolicy at invokePeek() method. Does any one have an idea?

If there is an other way to reach my goal it will be welcome too.

来源:https://stackoverflow.com/questions/37922390/spring-batch-commit-interval-changing-according-to-data-in-input-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!