1
votes

I have the following Spring configuration for a batch. I have encountered an issue with a production batch, when a particular use case throws a non skippable exception. As I reproduced the use case in an integration tests the batch behaviour was still weird. When my batch.commit-interval property is greater than 1, the retry mechanism is triggered, the uncommitted chunk is rolled back and all the previous records are processed again. when the batch-commit-interval is set to 1, the batch fails as soon as the non skippable exception is thrown.

I tried various values for the batch-commit-interval as well as for retry-limit. The particular exception that is causing the issue is a NullPointerException. My goal is to handle that exception preferabbly with a NeverRetryPolicy, if not, with classic exception handling. I don't want failed items processing to be retried, so I have even used the NeverRetryPolicy. But it seems as though the whole thing was ultimately driven by the batch-commit-interval (using a retry-limit, retryable exception classes and the NeverRetryPolicy, seems to be without effect)

I have read the other similar issues from StackOverflow. Thanks for helping.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context.xsd
                     http://www.springframework.org/schema/batch
                     http://www.springframework.org/schema/batch/spring-batch.xsd">


<!--================ JOB & STEPS ================ -->   
<batch:job id="BEL-batch-Retry-Tests">

    <batch:step id="mainRetryTests" >
        <batch:tasklet>
            <batch:chunk  reader="jpaReaderRetryTests" processor="bdToFlatFileProcessorRetryTests" writer="flatWriterRetryTests" commit-interval="${batch.commit-interval}" skip-limit="${batch.skip-limit}">

                <batch:streams>
                    <batch:stream ref="flatWriterRetryTests" />
                </batch:streams>
                <batch:skippable-exception-classes>
                    <batch:include
                        class="org.springframework.batch.item.file.FlatFileParseException" />
                    <batch:include class="ma.awb.ebk.bel.batch.EntiteBatchInvalideException" />
                </batch:skippable-exception-classes>
            </batch:chunk>
            <batch:listeners>

                <batch:listener ref="stepListener"/>
                <batch:listener ref="skipListener"/>
            </batch:listeners>
        </batch:tasklet>

        <batch:listeners>
            <batch:listener ref="stepListener"/>
            <batch:listener ref="skipListener"/>
        </batch:listeners>
    </batch:step>

    <batch:listeners>
        <batch:listener ref="jobListener"/>
    </batch:listeners>
</batch:job>
<!--================ READERS & WRITERS ================-->

<bean id="jpaReaderRetryTests" class="org.springframework.batch.item.database.JpaPagingItemReader">
    <property name="entityManagerFactory" ref="entityManagerFactoryBel"/>
    <property name="queryString" value="select c from CaracteristiquesCarte c"/>
    <property name="pageSize" value="100"/>
</bean>

<bean id="flatWriterRetryTests" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
    <property name="resource" value="file:${retry-tests.outputfile.basename}-#{jobParameters['runDate']}${retry-tests.outputfile.ext}" />
    <property name="lineAggregator">
             <bean
                 class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                 <property name="delimiter" value="${batch.file.writer.delimiter}" />
                 <property name="fieldExtractor">
                     <bean
                         class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                         <property name="names" value="codeProduit,nomCarte,codeAAfficher,rechargeable" />
                     </bean>
                 </property>
             </bean>
     </property>

</bean>



<bean id="bdToFlatFileProcessorRetryTests" class="ma.awb.ebk.bel.batch.retry.BDToFlatFileRetryProcessor" />

the reader and writer are classic. Here is the Processor's code

package ma.awb.ebk.bel.batch.retry;

import ma.awb.ebk.bel.domain.CaracteristiquesCarte;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;



public class BDToFlatFileRetryProcessor implements   ItemProcessor<CaracteristiquesCarte, CaracteristiquesCarte> {

private static int counter = 0;
private static final Logger LOGGER = LoggerFactory.getLogger (BDToFlatFileRetryProcessor.class);


@Override
public CaracteristiquesCarte process(CaracteristiquesCarte  caracteristiques)
        throws Exception {

    String witness = null;
    counter++;

    System.out.println("DEBUGGING iteration N° " +  counter+" "+caracteristiques.getCodeProduit());

    // Tester le cas d'un renvoie de null
    if (counter == 4) {
        System.out.println("PETTAGE " + counter);
        String test = witness.substring(2);

    }

    return caracteristiques;
}

}
1
are you sure got a NPE without fix it is a good pratice?Luca Basso Ricci
@Luca Basso Ricci, sure I can fix it. But it was an opportunity to fully understand the default retry behaviour.alainlompo

1 Answers

0
votes

One of the problem I see is you are getting null pointer, which should never occur. What happens if you fix it? Rest looks like desired behaviour of batch processing.