Spring批处理:无法记录跳过的项目,不会调用skiplistener,在提交期间会发生异常

时间:2014-11-25 14:03:49

标签: spring-batch

分批job.xml

<bean id="multiResourceReader"
    class=" org.springframework.batch.item.file.MultiResourceItemReader">
    <property name="resources" value="file:${daper.batch.file.inbound.directory.file}" />
    <property name="delegate" ref="cvsFileItemReader" />
</bean>

<bean id="Listener" class="com.batch.RecordListener" scope="step"></bean>

<bean id="oracleDBWriter" class="com.batch.OracleDBItemWriter"/>

<batch:step id="readAndSaveData" >
    <batch:tasklet transaction-manager="transactionManager">
        <batch:transaction-attributes isolation="READ_COMMITTED" 
                                      propagation="REQUIRES_NEW" timeout="200"/>
        <batch:chunk reader="multiResourceReader" writer="oracleDBWriter" 
                     commit-interval="1"  skip-limit="10">
            <batch:skippable-exception-classes>
                <batch:include class="org.springframework.batch.item.file.FlatFileParseException" />
                <batch:include class="org.springframework.batch.item.WriteFailedException" />
                <batch:include class="org.springframework.batch.item.WriteFailedException" />
                <batch:include class="org.hibernate.exception.ConstraintViolationException" />
                <batch:include class="org.springframework.dao.DataIntegrityViolationException" />
            </batch:skippable-exception-classes>
        </batch:chunk>


        <batch:no-rollback-exception-classes>
            <batch:include class="org.springframework.batch.item.file.FlatFileParseException" />
            <batch:include class="org.springframework.batch.item.WriteFailedException" />
            <batch:include class="org.springframework.batch.item.WriteFailedException" />
            <batch:include class="org.hibernate.exception.ConstraintViolationException" />
            <batch:include class="org.springframework.dao.DataIntegrityViolationException" />
        </batch:no-rollback-exception-classes>
    </batch:tasklet>
    <batch:listeners>
        <batch:listener ref="Listener"/>
    </batch:listeners>

    public class RecordListener extends SkipListenerSupport implements ChunkListener,StepExecutionListener,ItemWriteListener {



        @Override
        public void onSkipInRead(Throwable t) {
            System.out.println("skipped item: {} "+t.toString());
        }


        @Override
        public void onSkipInWrite(Object item, Throwable t) {
            // TODO Auto-generated method stub
            System.out.println("inside skip write item "+item.toString());
            System.out.println("error is "+t.getMessage());

        }

        @Override
        public void onSkipInProcess(Object item, Throwable t) {
            // TODO Auto-generated method stub
            System.out.println("inside skip process item "+item.toString());
            System.out.println("error is "+t.getMessage());
        }


        @Override
        public void beforeChunk() {
            // TODO Auto-generated method stub
            System.out.println("inside befre chunk");
        }


        @Override
        public void afterChunk() {
            // TODO Auto-generated method stub
            System.out.println("inside after chunk");

        }


        @Override
        public void beforeStep(StepExecution stepExecution) {
            // TODO Auto-generated method stub
            System.out.println("before step");
        }


        @Override
        public ExitStatus afterStep(StepExecution stepExecution) {
            // TODO Auto-generated method stub
            System.out.println("aftr step");
            return null;
        }


        @Override
        public void beforeWrite(List items) {
            // TODO Auto-generated method stub
        System.out.println("inside beforewrite");

        }


        @Override
        public void afterWrite(List items) {
            // TODO Auto-generated method stub
            System.out.println("afterWrite");

        }


        @Override
        public void onWriteError(Exception exception, List items) {
            // TODO Auto-generated method stub
            System.out.println("onWriteError");

        }

        /*@Override
        public void onSkipInRead(Throwable t) {
            System.out.println("insid skipread");

        }

        @Override
        public void onSkipInWrite(Object item, Throwable t) {
            // TODO Auto-generated method stub
            System.out.println("inside skip write item "+item.toString());
            System.out.println("error is "+t.getMessage());

        }

        @Override
        public void onSkipInProcess(Object item, Throwable t) {
            // TODO Auto-generated method stub
            System.out.println("inside skip process item "+item.toString());
            System.out.println("error is "+t.getMessage());
        }

        @Override
        public void beforeWrite(List<? extends StudentAthlete> items) {
            // TODO Auto-generated method stub
            System.out.println("inside beforewrite");
        }

        @Override
        public void afterWrite(List<? extends StudentAthlete> items) {
            // TODO Auto-generated method stub
            System.out.println("inside afterwrite");
        }

        @Override
        public void onWriteError(Exception exception,
                List<? extends StudentAthlete> items) {
            // TODO Auto-generated method stub
            System.out.println("inside onwriteerror itemssize "+items.size());

            for(StudentAthlete studentAthlete:items){
                System.out.println("studentathlete :" +studentAthlete.getMitId()+" "+studentAthlete.getAssignedSport());
            }

        }

        @Override
        public void beforeStep(StepExecution stepExecution) {
            // TODO Auto-generated method stub
            System.out.println("inside stepexecution");
            JobExecution jobExecution = stepExecution.getJobExecution();
            ExecutionContext jobContext = jobExecution.getExecutionContext();


        }

        @Override
        public ExitStatus afterStep(StepExecution stepExecution) {
            // TODO Auto-generated method stub
            System.out.println("inside afterstep");
            return null;
        }
}

我在登录控制台时

[] [INFO ] [2014-11-25 08:59:11,298] [org.springframework.batch.core.job.SimpleStepHandler] - Executing step: [readAndSaveData]
before step
inside befre chunk
inside beforewrite
[] [DEBUG] [2014-11-25 08:59:11,470] [edu.mit.daper.ftstatus.batch.OracleDBItemWriter] - step: readAndSaveData :OracleDBWriter :Write
[] [DEBUG] [2014-11-25 08:59:11,517] [edu.mit.daper.ftstatus.batch.OracleDBItemWriter] - student size 1  [Student{ id : 7E112B130A0937D200000149E73FB16E ; version : null ;  } ]

[] [DEBUG] [2014-11-25 08:59:11,517] [edu.mit.csf.daper.ftstatus.dao.hibernate.HibernateStudentDao] - HibernateStudentDao : SAVE 
[] [DEBUG] [2014-11-25 08:59:11,517] [edu.mit.daper.ftstatus.batch.OracleDBItemWriter] - step: readAndSaveDAta completed
afterWrite
[] [DEBUG] [2014-11-25 08:59:11,517] [org.hibernate.SQL] - insert into student (VERSION, ID, SPORT, CREATE_DATE, CREATE_BY, MODIFY_DATE, MODIFY_BY, STUDENT_KEY) values (?, ?, ?, ?, ?, ?, ?, ?)
[] [WARN ] [2014-11-25 08:59:11,532] [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 2291, SQLState: 23000
[] [ERROR] [2014-11-25 08:59:11,532] [org.hibernate.util.JDBCExceptionReporter] - ORA-02291: integrity constraint (testschema.SA_ASSIGNED_SPORT_FK) violated - parent key not found

[] [WARN ] [2014-11-25 08:59:11,532] [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 2291, SQLState: 23000
[] [ERROR] [2014-11-25 08:59:11,532] [org.hibernate.util.JDBCExceptionReporter] - ORA-02291: integrity constraint (testschema.SA_ASSIGNED_SPORT_FK) violated - parent key not found

[] [INFO ] [2014-11-25 08:59:11,532] [org.springframework.batch.core.step.tasklet.TaskletStep] - Commit failed while step execution data was already updated. Reverting to old version.
inside befre chunk
inside beforewrite
[] [DEBUG] [2014-11-25 08:59:11,580] [edu.mit.daper.ftstatus.batch.OracleDBItemWriter] - step: readAndSaveData :OracleDBWriter :Write
[] [DEBUG] [2014-11-25 08:59:11,580] [edu.mit.daper.ftstatus.batch.OracleDBItemWriter] - studentList size 1  [Student { id : 7CF682410A0937D200000149E73FB1DC ; version : null ;  } ]

[] [DEBUG] [2014-11-25 08:59:11,580] [edu.mit.csf.daper.ftstatus.dao.hibernate.HibernateStudentAthleteDao] - HibernateStudentAthleteDao : SAVE :about to save 900003588
[] [DEBUG] [2014-11-25 08:59:11,580] [edu.mit.daper.ftstatus.batch.OracleDBItemWriter] - step: readAndSaveDAta completed
afterWrite
[] [DEBUG] [2014-11-25 08:59:11,596] [org.hibernate.SQL] - insert into student (VERSION, ID, SPORT, CREATE_DATE, CREATE_BY, MODIFY_DATE, MODIFY_BY, STUDENT_KEY) values (?, ?, ?, ?, ?, ?, ?, ?)
inside after chunk
inside befre chunk
inside beforewrite

这里---没有调用skiplistener,并且在发生异常时不调用afterchunk。搜索了许多论坛并遵循代码示例..无法正常工作  请帮忙

0 个答案:

没有答案