Davidson
Davidson

Reputation: 1108

I get "Writer must be open before it can be written to" while using ClassifierCompositeItemWriter

The question states my problem. Can you not use FlatFileItemWriters (FFIW) as the writers on anything but simple chunk processing with a single writer? I'm new.

I've attempted to inject an FFIW into ItemProcessors and gotten the same thing. Perhaps I need to write my own custom writers. I was trying to leverage the FFIW to do the work, because all I need is to sift the one input file and populate three outfiles. My routerDelegate works fine, no problems there. Just fails on the write because the file is not open, and I can't see how to manually open it (which I think is the wrong approach, even if I could).

Thanks...

here's my code:

    <batch:step id="processCustPermits" next="somethingElse">
        <batch:description>Sift permits></batch:description>
        <batch:tasklet>
            <batch:chunk reader="custPermitReader" writer="custPermitCompositeWriter"
                commit-interval="1" />
        </batch:tasklet>
    </batch:step>

<bean id="custPermitCompositeWriter"
    class="org.springframework.batch.item.support.ClassifierCompositeItemWriter">
    <property name="classifier">
        <bean
            class="org.springframework.batch.classify.BackToBackPatternClassifier">
            <property name="routerDelegate" ref="permitRouterClassifier" />
            <property name="matcherMap">
                <map>
                    <entry key="hierarchy" value-ref="custPermitWriter" />
                    <entry key="omit" value-ref="custPermitOmithWriter" />
                    <entry key="trash" value-ref="custPermitTrashWriter" />
                </map>
            </property>
        </bean>
    </property>
</bean>
<bean id="custPermitWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
    <property name="resource" value="${sap.cust.permit.outfile.heirarchy}" />
    <property name="lineAggregator" ref="passThroughLineAggregator" />
    <property name="shouldDeleteIfExists" value="true" />
    <property name="shouldDeleteIfEmpty" value="false" />
</bean>
<bean id="custPermitOmithWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
    <property name="resource" value="${sap.cust.permit.outfile.omits}" />
    <property name="lineAggregator" ref="passThroughLineAggregator" />
    <property name="shouldDeleteIfExists" value="true" />
    <property name="shouldDeleteIfEmpty" value="true" />
</bean>
<bean id="custPermitTrashWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
    <property name="resource" value="${sap.cust.permit.outfile.trash}" />
    <property name="lineAggregator" ref="passThroughLineAggregator" />
    <property name="shouldDeleteIfExists" value="true" />
    <property name="shouldDeleteIfEmpty" value="true" />
</bean>

Upvotes: 4

Views: 12032

Answers (2)

stefan.m
stefan.m

Reputation: 2012

For those who prefer a Java configuration to XML configuration, it is done as follows:

@Bean
public Step processCustPermits(StepBuilderFactory stepBuilderFactory, 
        @Qualifier("custPermitReader")  ItemReader<Wscpos> custPermitReader,
        @Qualifier("custPermitCompositeWriter") ItemWriter<Wscpos> custPermitCompositeWriter,
        @Qualifier("custPermitWriter") FlatFileItemWriter<Wscpos> custPermitWriter,
        @Qualifier("custPermitOmithWriter") FlatFileItemWriter<Wscpos> custPermitOmithWriter,
        @Qualifier("custPermitTrashWriter") FlatFileItemWriter<Wscpos> custPermitTrashWriter)
{
    return stepBuilderFactory.get("processCustPermits")
        .<Wscpos, Wscpos> chunk(1)
        .reader(custPermitReader)
        .writer(custPermitCompositeWriter)
        .stream(writerCustodyMismatch)
        .stream(writerNoMatch)
        .stream(custPermitTrashWriter)
        .build();
}

Upvotes: 2

Davidson
Davidson

Reputation: 1108

Sometimes you just have to read real closely. I added the Streams element to my chunk element and voila!

<batch:step id="processCustPermits" next="somethingElse">
    <batch:description>Sort out unwanted permits></batch:description>
    <batch:tasklet>
        <batch:chunk reader="custPermitReader" writer="custPermitCompositeWriter"
            commit-interval="1">
            <batch:streams>
                <batch:stream ref="custPermitWriter" />
                <batch:stream ref="custPermitOmithWriter" />
                <batch:stream ref="custPermitTrashWriter" />
            </batch:streams>
        </batch:chunk>
    </batch:tasklet>
</batch:step>

Upvotes: 7

Related Questions