简体   繁体   中英

How to get the number of items read in Spring Batch ItemReadListener

In a Spring Batch application, I need to improve my reading error handling for a StaxEventItemReader.

So far I get:

public class UserAuthorizationErrorListener extends
    ItemListenerSupport<UserAuthorizationType, UserAuthorizationType> {

    @Override
    public void onReadError(Exception ex) {
        ex.printStackTrace();
        //TODO how to get the position in the file ? or the current index of the item that raised this exception?
    }
}

And the spring configuration :

<step id="readFileStep" next="moveFileToFolderDecision">
    <tasklet>
        <chunk reader="userAuthorizationMultipleResourcesReader"
            processor="userAuthorizationItemProcessor"
            writer="userAuthorizationCompositeItemWriter"
            commit-interval="2">

            <listeners>
                <listener ref="userAuthorizationErrorListener" />
            </listeners>
        </chunk>
    </tasklet>
</step>

This is quite great, I am notified when and error occured.

But I need to provide clever error reporting and give the index of the item that is in error.

How can I get this information? Or how can I get the number of correctly read before the error occured ? Or how can I get the line number and the column number of the error?


Edit

I get stack trace like that:

org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.stream.XMLStreamException: ParseError at [row,col]:[20,4]
Message: The element type "Action" must be terminated by the matching end-tag "</Action>".]
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:794)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:715)

But is it possible to avoid casting the Exception and parsing the error message to get the [row,col]:[20,4] information?

Look into the causes of the exception ( ex.getCause() ). They should provide more detail.

Finally, I ended with the following solution.

As I already extended the StaxEventItemReader class to fit another need, I just added the following method to my class:

public class MyStaxEventItemReader extends StaxEventItemReader<UserAuthorizationType> {

    ...

    /**
     * The current index of the item read by the reader.
     * <p>
     * As the counter is incremented just before the {@link #doRead()} method
     * being called, if an exception occurs while reading the next element, the
     * index is always correct.
     * 
     * @return The current index of the item read by the reader
     */
    public int getCurrentItemCount() {
        return super.getCurrentItemCount();
    }
}

Then I inject the reader into the userAuthorizationErrorListener

<bean id="userAuthorizationErrorListener" class="com.my.package.UserAuthorizationErrorListener">
    <property name="itemReader" ref="userAuthorizationItemReader" />
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM