但是,当我调用批处理时,不会调用编写器。
问题在步骤定义中:
@Bean
public Step treatStock() throws InstantiationException, IllegalAccessException {
return stepBuilderFactory.get("treatStock")
.<Notification, Notification>chunk(1)
.reader(reader())
.processor(notificatonProcessor())
.writer(compositeExchangeWriter())
.writer(updateWriter())
.build();
}
你在打电话给
writer()
方法两次,因此
updateWriter()
将覆盖
compositeExchangeWriter()
. 您需要使用复合编写器作为参数调用一次该方法,您可以在该参数上设置委托编写器。
作为一个附加说明,在使用复合编写器时,如果委托不实现
ItemStream
接口。有关此的详细信息,请访问:
https://docs.spring.io/spring-batch/4.0.x/reference/html/readersAndWriters.html#delegatePatternAndRegistering
Spring如何调用分类器?
当A
ClassifierCompositeItemWriter
如果配置正确,Spring批处理将对每个项调用分类器以确定要使用哪个编写器,然后调用适当的编写器来编写项。
在您的配置中,
分类器组合项编写器
此处未正确配置:
@SuppressWarnings("unchecked")
public T build() throws InstantiationException, IllegalAccessException {
Object compositeItem = getCompositeItem().newInstance();
Method setDelegates = ReflectionUtils.findMethod(compositeItem.getClass(), "setDelegates", List.class);
ReflectionUtils.invokeMethod(setDelegates,compositeItem, delegates);
return (T) compositeItem;
}
我不会用反省来设置代表。问题是您希望使用该方法
compositeExchangeWriter
注册一个
分类器组合项编写器
但它的返回类型是
CompositeItemWriter
. 因此,复合编写器不被视为分类器。
您可以找到如何使用
分类器组合项编写器
在这里:
https://github.com/spring-projects/spring-batch/blob/master/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/ClassifierCompositeItemWriterTests.java#L44