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?