Announcement Announcement Module
Collapse
No announcement yet.
Exception Processing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exception Processing

    Hi,
    I am working on Spring Batch and my input source is text file.
    I am using a custom reader to identify the type of object(line)

    I will be reading from a text file , validate and write to a output text file.

    while validating each line, if i get exception, that line should be written to a seperate file and process the remaining lines.

    Need help how to configure it.

    Thanks

  • #2
    You can catch the exception in your LineMapper. Then map the exception to its own object type and return it. That way your writer will receive two types of objects: items and errors. You can use a composite writer pattern to write the difference types to different files.

    Comment


    • #3
      Thanks Garrette,

      currently i am using default line mapper, can i have sample custom line mapper and writer code.

      thanks for your time.

      Comment


      • #4
        Use a custom LineMapper to proxy the DefaultLineMapper:
        Code:
        public class CustomLineMapper<T> implements LineMapper<Object> {
            private LineMapper<T> delegate;
        
            public Object mapLine(String line, int lineNumber) throws Exception {
                try {
                    return delegate.mapLine(line, lineNumber);
                } catch (Exception e) {
                    return new ErrorObject(line, lineNumber);
                }
            }
        }
        Wire the DefaultLineMapper bean into this custom LineMapper as the 'delegate' property. That way your custom mapper will use the DefaultLineMapper and handle exceptions if necessary. Any exception raised during mapping will result in an ErrorObject being returned instead of the true item.

        Comment


        • #5
          Thanks Garette,
          In my scenario, i will be reading a set of records(a batch), if i get exception, i need to write this batch into a seperate file.

          I think custom line mapper will work if we are reading line by line.

          Can you suggest me how to write a batch(assuming there is some exception in this batch, means not meeting the business requirement) so such records(batches) have to be written to a seperate file, so that these can be corrected or edited later.

          Thanks

          Comment

          Working...
          X