Announcement Announcement Module
Collapse
No announcement yet.
How to write skipped data to itemWriter inside a SkipListener Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to write skipped data to itemWriter inside a SkipListener

    I have a SkipListener set up and I inject an itemWriter into it. When I use the itemWriter, I get:

    org.springframework.batch.item.WriterNotOpenExcept ion: Writer must be open before it can be written to

    How do I handle this situation? I read a posting that suggested opening it in an earlier step, but I can't see how to do this...

    Suggestions gratefully recieved.

    Cheers,

    Alph
    Last edited by alpheratz; Apr 23rd, 2009, 05:21 AM. Reason: unmunge the exception name

  • #2
    To answer my own question...

    Registering the writer as a stream for the chunk worked.

    Code:
    <bean id= "passthroughLineAggregator" class="fm.PassThroughLineAggregator" />
    
      <bean id="errorItemWriter"
            class="org.springframework.batch.item.file.FlatFileItemWriter"
            p:resource-ref="errorOutputResource"
            p:lineAggregator-ref="passthroughLineAggregator"
            p:shouldDeleteIfExists="true" />
    
      <bean id="skipListener" class="fm.SkipListener"
          p:writer-ref="errorItemWriter" />
    
      <batch:job id="job">
        <batch:step id="load">
    
          <batch:tasklet>
    
            <batch:chunk skip-limit="10"
                         reader="itemReader"
                         writer="itemWriter"
                         processor="validatingItemProcessor"
                         commit-interval="1">
              <batch:streams>
                <batch:stream ref="errorItemWriter"/>
              </batch:streams>
              <batch:skippable-exception-classes>
                org.springframework.batch.item.file.FlatFileParseException
                org.springframework.batch.item.validator.ValidationException
              </batch:skippable-exception-classes>
            </batch:chunk>
            <batch:listeners>
              <batch:listener ref="skipListener"/>
            </batch:listeners>
          </batch:tasklet>
        </batch:step>
      </batch:job>

    Comment


    • #3
      Can you please provide details on your skipListener implementation?
      I am trying to do the same thing, except on db write exceptions.

      Comment


      • #4
        Sure.

        I wrote a 'how-to' article for GroovyMag (http://www.groovymag.com) a while back.

        It's all at:

        http://wordpress.transentia.com.au/w...-spring-batch/

        Comment


        • #5
          Thanks for the info. I tried similar skip listener but it is throwing exceptions. I am using SB 2.1


          11:58:30,758 ERROR <> [support.CommandLineJobRunner] Job Terminated in error: Error creating bean with name 'businessLoad': Cannot create inner bean '(inner bean)' of type [org.springframework.batch.core.listener.StepListen erFactoryBean] while setting bean property 'listeners' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'skipListener' while setting bean property 'delegate'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'skipListener' defined in class path resource [BusinessJob.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'writer' of bean class [com.xx.CompositeItemFailureSkipListener]: Bean property 'writer' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

          job setup
          <bean id="skipListener" class="com.xx.CompositeItemFailureSkipListener" p:writer-ref="skipWriter"/>

          <bean id="skipWriter" 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.DelimitedLineAggregator" >
          <property name="fieldExtractor">
          <bean class="org.springframework.batch.item.file.transfo rm.BeanWrapperFieldExtractor">
          <property name="names" value="business,name,number" />
          </bean>
          </property>
          </bean>
          </property>
          </bean>


          CompositeItemFailureSkipListener method.

          private FlatFileItemWriter<Business> writer = new FlatFileItemWriter<Business>();


          public void onSkipInWrite(Business business, Throwable t) throws Exception {
          writer.write(Collections.singletonList(business));
          }

          Comment


          • #6
            The error message says: 'Bean property 'writer' is not writable...'

            YOu have declared writer to be private:

            private FlatFileItemWriter<Business> writer

            It can't be set by the context.

            Make it public.

            HTH

            Comment


            • #7
              It's proabably also worth looking at @Required.

              BOB

              Comment


              • #8
                Really appreciate your help.
                I made the ffiw a public and still got the same error. i am not sure about your reference to @Required.

                Can you please provide details of your writer definition for java. I think this is groovy which I am not familiar. Also, It does not allow me to Override onSkipInProcess(compile error- must override a superclass method).


                package au.com.transentia.sb

                import org.springframework.batch.core.listener.SkipListen erSupport

                public class SkipListener extends SkipListenerSupport<MultilineRecord, Object> {

                def writer

                @Override
                public void onSkipInProcess(MultilineRecord item, Throwable t) {
                writer.write ( [ item ] )
                }
                }

                Comment

                Working...
                X