Announcement Announcement Module
Collapse
No announcement yet.
Writer must be open before it can be written to Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Writer must be open before it can be written to

    ...//batch:job
    ...//batch:step
    ...//batch:tasklet
    <batch:chunk reader="itemReader" writer="writerFactory" commit-interval="10" />
    ...//batch:tasklet
    ...//batch:step
    ...//batch:job

    <bean id="writerFactory" class="factory.WriterFactory" factory-method="createWriterInstance" scope="step">
    <constructor-arg index="0" type="java.lang.String" value="#{jobParameters['dbJobName']}"/>
    </bean>
    <bean id="textItemWriter" class="jobs.FlatFileWriter" scope="step">
    <property name="jobName" value="#{jobParameters['dbJobName']}"/>
    </bean>
    <bean id="xmlItemWriter" class="jobs.XMLWriter" scope="step">
    <property name="jobName" value="#{jobParameters['dbJobName']}"/>
    </bean>

    signature of TextItemWriter : public class TextItemWriter extends FlatFileItemWriter<RowMapItem> implements StepExecutionListener,ItemStream

    I am having the requirement where i am using a factory method to dynamically create an instance of ItemWriters like FlatFileItemWriter and StaxEventItemWriter(for XML).
    In afterPropertiesSet i am setting the resource & lineAggregator. In write method i am using super.write(items), at this stage i am getting an error "Writer must be open before it can be written to".

    If i try to override open method by implementing ItemStream, control doesn't enter open method. it is directly going to write method. and throwing that error.
    Last edited by kamath_sv; Feb 16th, 2011, 02:42 AM.

  • #2
    In your step configuration add streams property.
    Within that list your writer references. That should work

    Below is the sample, add this to your step

    <streams>
    <stream ref="fileItemWriter1"/>
    <stream ref="fileItemWriter2"/>
    </streams>

    Comment


    • #3
      thanks for the solution...

      Comment


      • #4
        This issue seem to be there for ClassifierCompositeItemWriter too

        I am getting
        org.springframework.batch.item.WriterNotOpenExcept ion: Writer must be open before it can be written to, when I use ClassifierCompositeItemWriter with FlatFileItemWriter delegates..

        And, ClassifierCompositeItemWriter does not have any method setIgnoreItemStream, as suggested for CompositeItemWriter class.

        What I am trying to do is to route a domain object to either success or failure report writers based on certain flag in the object. Both the success and failure writers are FlatFileItemWriter instances pointing to different file system resources..

        I wonder if there is any fix available to this? If not, is there any alternative, which is almost as simple to code for as ClassifierCompositeItemWriter

        Comment


        • #5
          As specified in Spring BatchBATCH-836, CompositeItemWriter seem to have a solution using setIgnoreItemStream.

          Comment

          Working...
          X