我使用spring批处理从数据库中读取数据(使用分区)并根据输入键将其写入一组文件 - 1,2,3,4。
我创建了一个CompositeItemWriter,它由两个ClassifierCompositeItemWriter组成。即使我已将各个编写者注册为流,但仍然会出现以下异常:
org.springframework.batch.item.WriterNotOpenException: Writer must be open before it can be written to
我甚至尝试将ItemWriter1和ItemWriter2注册为流,但是,这给了我一个不同的错误:
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy13 implementing org.springframework.batch.item.ItemWriter,java.io.Serializable,org.springframework.aop.scope.ScopedObject,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.batch.item.ItemStream] for property 'streams[0]': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:264)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:128)
at org.springframework.beans.TypeConverterDelegate.convertToTypedArray(TypeConverterDelegate.java:463)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:195)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:448)
... 74 more
我甚至在编写器中实现了ItemStream,但它还没有工作。
public class WriterA1 implements ItemWriter<List<Object>>, ItemStream {
...
}
以下是xml配置:
......
<job id="abcJob" xmlns="http://www.springframework.org/schema/batch"
restartable="true">
<step id="masterStep">
<partition step="slaveStep" partitioner="abcPartitioner">
<handler grid-size="${grid-size}" task-executor="abcTaskExecutor" />
</partition>
</step>
</job>
<step id="slaveStep" xmlns="http://www.springframework.org/schema/batch">
<tasklet transaction-manager="transactionManager">
<chunk reader="abcReader" writer="abcWriter"
processor="abcProcessor" commit-interval="${a}" skip-limit="${b}" retry-limit="${c}" >
<streams>
<!--
<stream ref="ItemWriter1"/>
<stream ref="ItemWriter2"/>
-->
<stream ref="WriterA1"/>
<stream ref="WriterB2"/>
<stream ref="WriterC3"/>
<stream ref="WriterD4"/>
<stream ref="WriterA5"/>
<stream ref="WriterB6"/>
<stream ref="WriterC7"/>
<stream ref="WriterD8"/>
</streams>
</chunk>
<listeners>
...
</listeners>
</tasklet>
</step>
<bean id="abcWriter" class="org.springframework.batch.item.support.CompositeItemWriter" scope="step">
<property name="delegates">
<list>
<ref bean="ItemWriter1" />
<ref bean="ItemWriter2" />
</list>
</property>
</bean>
<bean id="ItemWriter1" class="org.springframework.batch.item.support.ClassifierCompositeItemWriter" scope="step">
<property name="classifier">
<bean
class="org.springframework.classify.BackToBackPatternClassifier">
<property name="routerDelegate">
<bean class="xxx.xxx.xxx.xxx.Classifier1" scope="step"/>
</property>
<property name="matcherMap">
<map>
<entry key="1" value-ref="WriterA1" />
<entry key="2" value-ref="WriterB2" />
<entry key="3" value-ref="WriterC3" />
<entry key="4" value-ref="WriterD4" />
</map>
</property>
</bean>
</property>
</bean>
<bean id="ItemWriter2" class="org.springframework.batch.item.support.ClassifierCompositeItemWriter" scope="step">
<property name="classifier">
<bean
class="org.springframework.classify.BackToBackPatternClassifier">
<property name="routerDelegate">
<bean class="xxx.xxx.xxx.xxx.Classifier2" scope="step"/>
</property>
<property name="matcherMap">
<map>
<entry key="1" value-ref="WriterA5" />
<entry key="2" value-ref="WriterB6" />
<entry key="3" value-ref="WriterC7" />
<entry key="4" value-ref="WriterD8" />
</map>
</property>
</bean>
</property>
</bean>
<bean id="WriterA1" class="xxx.xxx.xxx.xxx.WriterA1" scope="step">
</bean>
<bean id="WriterB2" class="xxx.xxx.xxx.xxx.WriterB2" scope="step">
</bean>
<bean id="WriterC3" class="xxx.xxx.xxx.xxx.WriterC3" scope="step">
</bean>
<bean id="WriterD4" class="xxx.xxx.xxx.xxx.WriterD4" scope="step">
</bean>
<bean id="WriterA5" class="xxx.xxx.xxx.xxx.WriterA5" scope="step">
</bean>
<bean id="WriterB6" class="xxx.xxx.xxx.xxx.WriterB6" scope="step">
</bean>
<bean id="WriterC7" class="xxx.xxx.xxx.xxx.WriterC7" scope="step">
</bean>
<bean id="WriterD8" class="xxx.xxx.xxx.xxx.WriterD8" scope="step">
</bean>
请告知。
答案 0 :(得分:0)
您有三种类型的作家。从上到下:
abcWriter
是CompositeItemWriter
。它通过将ItemStream
方法调用委派给代理(此处为ItemStream
和ItemWriter1
)来实现ItemWriter2
,前提是它们实现了ItemStream
。事实并非如此。但即使他们实现了ItemStream
,也不应该在步骤配置中单独注册ItemWriter1
和ItemWriter2
作为流(在下一个要点中还有另一个独立的原因)。
ItemWriter1
/ ItemWriter2
是ClassifierCompositeItemWriter
个。此类未实现ItemStream
,因此您不得在步骤配置中将它们注册为流。
WriterA1
属于BeanIOFlatFileItemWriter
类型,因此实施ItemStream
。由于包裹它们的ClassifierCompositeItemWriter
不会调用ItemStream
方法(与CompositeItemWriter
不同),因此必须将每个方法注册为步骤配置中的流
但这就是你声称拥有的。然而,您的作用域范围WriterXX
bean通过单例bean代理(使用接口代理模式),而不是实现ItemStream
或ItemStreamWriter
,而只是ItemWriter
。确保<bean>
元素中的类执行ItemStream
。您还可以尝试显式创建范围内的代理bean(使用ScopedProxyFactoryBean
并设置interfaces
属性)。或者,当ScopedProxyFactoryBean::setBeanFactory
包含字符串targetBeanName
时,您可以尝试在WriterXX
中打破断点(它将类似于stepScopedTarget.WriterD8
)并尝试理解为什么{{1}接口未被代理。