在使用ClassifierCompositeItemWriter时,我得到“Writer必须在可写入之前打开”

时间:2013-07-08 21:21:12

标签: spring-batch

问题陈述了我的问题。除了使用单个编写器进行简单的块处理之外,你能不能使用FlatFileItemWriters(FFIW)作为编写器吗?我是新人。

我试图将一个FFIW注入ItemProcessors并得到同样的东西。也许我需要编写自己的自定义编写器。我试图利用FFIW来完成工作,因为我只需要筛选一个输入文件并填充三个outfiles。我的routerDelegate工作正常,没有问题。刚刚写入失败,因为文件未打开,我无法看到如何手动打开它(我认为这是错误的方法,即使我可以)。

...谢谢

这是我的代码:

    <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>

2 个答案:

答案 0 :(得分:7)

有时你只需要仔细阅读。我将Streams元素添加到我的chunk元素中,然后瞧!

<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>

答案 1 :(得分:2)

对于那些喜欢Java配置到XML配置的人,可以按如下方式完成:

@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();
}