7

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

Peter Wippermann
  • 4,125
  • 5
  • 35
  • 48
juniorbansal
  • 1,249
  • 8
  • 31
  • 51
  • can you also post your "skipListener" bean config and implementation? Are you implementing `SkipListener` interface? Are you using `@OnSkipInRead` annotation? etc.. – tolitius Oct 03 '11 at 22:48

2 Answers2

14

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 ) {
        // ...
    }

    ....
}
tolitius
  • 22,149
  • 6
  • 70
  • 81
  • Thank You.. I had sysout code inside onSkipInRead(). Moreover i am throwing new nullPointerException from the first line of my reader.I saw in the log that skip limit of 10 was exceeded and then was expecting the sysout in my skip read to print. But don't see it. I did try putting the listeners outside the tasklet. No luck.. – juniorbansal Oct 04 '11 at 13:59
  • 1
    here is a working example from one of my projects in the past. Try to use `@OnReadError` in a skip listener. – tolitius Oct 04 '11 at 14:28
3

I come back on the subject after having had the same problem in more superior versions where the xml configuration is not used

With the bellow configuration , i was not able to reach the skip listener implementations.

  @Bean
    public Step step1( ) {
        return stepBuilderFactory
                .get("step1").<String, List<Integer>>chunk(1)
                .reader(reader)
                .processor(processor)
                .faultTolerant()
                .skipPolicy(skipPolicy)
                .writer(writer)
                .listener(stepListener)
                .listener(skipListener)
                .build();
    }

The issue here is the placement of the skip listener is not correct. The skip listener should be within the faultTolerantStepBuilder.

  @Bean
    public Step step1( ) {
        return stepBuilderFactory
                .get("step1").<String, List<Integer>>chunk(1)
                .reader(reader)
                .processor(processor)
                .faultTolerant()
                .listener(skipListener)
                .skipPolicy(skipPolicy)
                .writer(writer)
                .listener(stepListener)
                .build();
    }

The first snippet is considered as listener within a simpleStepBuilder.

Yassine CHABLI
  • 3,459
  • 2
  • 23
  • 43
  • I could not add the skip policy after listener here.. it is a compile time issue for me. – Jayanth Mar 16 '23 at 19:14
  • Holy Smokes!... how is this not even obvious in compile time?... I spent +1hour stuck, searching the docs, & googling about this issue, not knowing that what I needed was simply a rearrangement of the listeners... Just WoW time wasted on something this simple. – SourceVisor Apr 15 '23 at 00:46
  • @Jayanth, maybe you are using different version – Yassine CHABLI Apr 15 '23 at 22:09