Announcement Announcement Module
Collapse
No announcement yet.
Configure a ItemWriter with skip listener. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configure a ItemWriter with skip listener.

    Hi, I am new to SB.
    I need some details on how to configure a FlatFileItemWriter with skipListener.
    I need to get the failed input record(skipped) and write it to a flat file. I checked the forums, but did not find details. I am not sure if I have the correct approach. Here is what I have setup so far.

    <job id="testJob" >
    <step id="testLoad">
    <tasklet>
    <chunk reader="testReader" writer="compositeWriter" commit-interval="10" skip-limit="1000">
    <streams>
    <stream ref="testReader"/>
    <stream ref="dbWriter"/>
    <stream ref="ffErrorWriter"/>
    </streams>
    <skippable-exception-classes>
    <include class="org.springframework.batch.item.file.FlatFil eParseException" />
    <include class="org.springframework.batch.item.WriteFailedE xception" />
    <include class="java.lang.RuntimeException"/>
    <include class="java.lang.Exception"/>
    <include class="org.hibernate.exception.ConstraintViolation Exception"/>
    <include class="org.springframework.dao.DataIntegrityViolat ionException"/>
    </skippable-exception-classes>
    </chunk>

    <listeners>
    <listener ref="skipListener"/>
    </listeners>
    </tasklet>
    </step>
    </job>

    <bean id="compositeWriter" class="org.springframework.batch.item.support.Comp ositeItemWriter">
    <property name="delegates">
    <list>
    <ref bean="dbWriter" />
    <ref bean="ffErrorWriter" />
    </list>
    </property>
    </bean>
    <bean id="skipListener" class="some custom class implements SkipListener" >
    (Need help here. How to pass the input record and configure the ffitem writer

    public class CustomSkipListener implements SkipListener<Object, Object> {


    private String inputRecord;

    public void onSkipInWrite(Object arg0, Throwable arg1) {
    logger.error("Encountered error in write, Exception is:"+ arg1.getMessage()+"Item that failed:"+arg0.toString());

    // how to get the raw input record not the object which failed during write to db and How to hook it to ffitem writer as a passthrough.

    }


    )
    <bean id="ffErrorWriter" class="org.springframework.batch.item.file.FlatFil eItemWriter">
    <property name="resource" value="file:Skipped.txt" />
    <property name="lineAggregator">
    <bean class="org.springframework.batch.item.file.transfo rm.PassThroughLineAggregator" />
    </property>
    </bean>

    < bean for testReader> Read input from flat file
    < bean for dbWriter> Write a record using hibernate.

  • #2
    I am confused. What do you want to get in your writer exactly? The object that was skipped in the read or processing phase?

    Can you elaborate a bit more what you're trying to achieve? If an item was skipped during read or process, it won't be set in the write method.

    Comment


    • #3
      I am trying to capture the write failures. If I get a constraint violation on write or some other dao exception, I would like to skip that record, continue processing and write that record out to the bad records file.

      Comment


      • #4
        Did you get this figured out ; can you help me ?

        Comment


        • #5
          Hey,
          The below is your scenario ?
          Read is successful from the flat file but it fails when you are trying to write and you want to store it in flatfile ?
          is that your question ? or parsing from the flat file itself it got failed ?

          Comment


          • #6
            Originally posted by syedmhussaini View Post
            I am trying to capture the write failures. If I get a constraint violation on write or some other dao exception, I would like to skip that record, continue processing and write that record out to the bad records file.

            Comment


            • #7
              itemwriter isn't working for me


              Recognition Awards

              Comment


              • #8
                I hope I am getting this right what you want to do

                First off you get by default the output item in the arguments, if you configured the

                This is my Classes definition, I have a document I write out into a file and if that fails I want it to be logged:

                public class DocumentSkipListener implements SkipListener<Object,Document> {

                and this is my onSkip method:
                public void onSkipInWrite(Document item, Throwable t) {
                logger.error(t + " - thrown while writing item " + item);
                }

                So you see you already have the item as an argument from the write attempt, there is no need to have an extra variable for it in your class.

                second, if you want to write the error into a flat file, you should have a reference to your error writer in your skipListener and not in your compositeWriter.

                Maybe you should look at the SkipListener Interface again to clear up how it works.

                Comment

                Working...
                X