Spring Batch SkipListener not called when exception occurs in reader

ε祈祈猫儿з 提交于 2019-12-08 21:04:04

问题


This is my step configuration. My skip listeners onSkipInWrite() method is called properly. But onSkipInRead() is not getting called. I found this by deliberately throwing a null pointer exception from my reader.

<step id="callService" next="writeUsersAndResources">
        <tasklet allow-start-if-complete="true">
            <chunk reader="Reader" writer="Writer"
                commit-interval="10" skip-limit="10">
                <skippable-exception-classes>
                    <include class="java.lang.Exception" />
                </skippable-exception-classes>
            </chunk>
            <listeners>
                <listener ref="skipListener" />
            </listeners>
        </tasklet>
    </step>

I read some forums and interchanged the listeners-tag at both levels: Inside the chunk, and outside the tasklet. Nothing is working...

Adding my skip Listener here

package com.legal.batch.core;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.batch.core.SkipListener;
import org.springframework.jdbc.core.JdbcTemplate;


public class SkipListener implements SkipListener<Object, Object> {


    @Override
    public void onSkipInProcess(Object arg0, Throwable arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSkipInRead(Throwable arg0) {
    }

    @Override
    public void onSkipInWrite(Object arg0, Throwable arg1) {
}

}

Experts please suggest


回答1:


Skip listeners respect transaction boundary, which means they always be called just before the transaction is committed.

Since a commit interval in your example is set to "10", the onSkipInRead will be called right at the moment of committing these 10 items (at once).

Hence if you try to do a step by step debugging, you would not see a onSkipInRead called right away after an ItemReader throws an exception.

A SkipListener in your example has an empty onSkipInRead method. Try to add some logging inside onSkipInRead, move a and rerun your job to see those messages.

EDIT:

Here is a working example [names are changed to 'abc']:

<step id="abcStep" xmlns="http://www.springframework.org/schema/batch">
    <tasklet>
        <chunk writer="abcWriter"
               reader="abcReader"
               commit-interval="${abc.commit.interval}"
               skip-limit="1000" >

            <skippable-exception-classes>
                <include class="com.abc....persistence.mapping.exception.AbcMappingException"/>
                <include class="org.springframework.batch.item.validator.ValidationException"/>
                ...
                <include class="...Exception"/>
            </skippable-exception-classes>

            <listeners>
                <listener ref="abcSkipListener"/>
            </listeners>

        </chunk>

        <listeners>
            <listener ref="abcStepListener"/>
            <listener ref="afterStepStatsListener"/>
        </listeners>

        <no-rollback-exception-classes>
            <include class="com.abc....persistence.mapping.exception.AbcMappingException"/>
            <include class="org.springframework.batch.item.validator.ValidationException"/>
            ...
            <include class="...Exception"/> 
        </no-rollback-exception-classes>

        <transaction-attributes isolation="READ_COMMITTED"
                                propagation="REQUIRED"/>
    </tasklet>
</step>

where an abcSkipListener bean is:

public class AbcSkipListener {

    private static final Logger logger = LoggerFactory.getLogger( "abc-skip-listener" );

    @OnReadError
    public void houstonWeHaveAProblemOnRead( Exception problem ) {
        // ...
    }


    @OnSkipInWrite
    public void houstonWeHaveAProblemOnWrite( AbcHolder abcHolder, Throwable problem ) {
        // ...
    }

    ....
}


来源:https://stackoverflow.com/questions/7638924/spring-batch-skiplistener-not-called-when-exception-occurs-in-reader

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