Announcement Announcement Module
Collapse
No announcement yet.
call to a method in custom ItemWriter method is called twice when there is exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • call to a method in custom ItemWriter method is called twice when there is exception

    Hi Techies,

    I have a problem in Spring batch. The problem goes like this:
    Please check the attached files(Reader, Writer, Listener and XML file)

    Reading csv file with the following format:
    START
    2011010100001,ABC
    2011010100002,DEF
    2011010100003,GHI
    END,3

    I could able to read the entire file using tokenizers. Please check the attachment for all Reader, Writer, Listener and Job.xml file.
    I read all the lines in my reader and form a list (like List<DispatchDeviceResultVO>) of size 3. Now I set this list to a commonVO to capture it in my write method of writer class.
    I will pass the list to my helper/delegate class for further processing/validation.
    If there is any improper data in the input, then I am adding this to errorlist (like List<ErrorVO>).
    After completely processing the helper/delegate method, I am throwing custom Exception (like DataNotFoundException) with the error list adding to one of the property for making all errors available in my Listener class onSkipInWrite method. After throwing exception for the first time, the Listener is not at all called but the helper method is called once again for 2nd time. Now if there are any errors in the list then the Listener onSkipInWrite method is called and writes to an error file.

    Please do the needful.

    Why Listener is not called for 1st time even when I throw an exception?
    Why the helper/delegate class from my writer class is called twice?
    How to solve this problem?

    Please suggest.

    thanking you,

    With Regards,
    Mone

  • #2
    I am not good at English, so if I misunderstand, I'm sorry.


    > Why the helper/delegate class from my writer class is called twice?

    Maybe because of using skip function, it is normal behavior on skip function.
    If exception raises, skip function calls ItemProcessor and ItemWriter again from first item in the chunk.

    Reference url I found is below:
    http://stackoverflow.com/questions/7...k-during-write

    It mentions about roll-back function , but it maybe is same as skip.


    > How to solve this problem?

    How about using ItemProcessor?
    You send improper data to ItemProcessor, and ItemProcessor processes the process of errorlist.
    And if you do not want to send improper data to ItemWriter, ItemProcessor should return null.
    If the item is not improper data , ItemProcessor should return the same item.

    Dose this become your help?

    Comment


    • #3
      This is the exepcted behaviour with respect to listeners. Am quoting the reason from Spring Batch in Action.

      When does Spring Batch call a skip listener method? Just after the item reader, processor, or writer
      throws the to-be-skipped exception you may think. No, not just after. Spring Batch postpones the call to
      skip listeners to right before committing the transaction for the chunk. Why is that? Because something
      wrong can happen after Spring Batch skips an item and Spring Batch could then rollback the transaction.
      Imagine that the item reader throws a to-be-skipped exception. Later on, something goes wrong during
      the writing phase of the same chunk and Spring Batch rolls back the transaction, and could even fail the
      job execution. You wouldn’t want to log the skipped item during the reading phase, because Spring Batch
      rolled back the whole chunk! That’s why Spring Batch calls skip listeners just before the commit of the
      chunk, when it’s almost certain nothing unexpected could happen.

      Comment

      Working...
      X