我在Spring Batch
申请中使用Spring Boot
。 Spring Boot
版本为1.3.5.RELEASE
。
我正在尝试使用CompositeItemWriter
,以便项目列表首先由WriterOne压缩/压缩,然后传递给WriterTwo,它们将在数据库中写入。
这是我的作家1:
@Component
public class Writer1 implements org.springframework.batch.item.ItemWriter<SimpleObject> {
@Override
public void write(List<? extends SimpleObject> list) throws Exception {
for(SimpleObject simpleObject: list){
// code which compresses the file
}
}
}
这是我的作家2:
@Component
public class Writer2 implements org.springframework.batch.item.ItemWriter<SimpleObject> {
@Override
public void write(List<? extends SimpleObject> list) throws Exception {
for(SimpleObject simpleObject: list){
// the file object meta data are being writtren to database.
}
}
}
这是我尝试初始化任务步骤并为其提供CompositeItemWriter而不是ItemWriter。
CompositeItemWriter compositeItemWriter = new CompositeItemWriter();
compositeItemWriter.setDelegates(Arrays.asList(writer1,writer2));
TaskletStep processingStep = stepBuilderFactory.get(getLabel() + "-" + UUID.randomUUID().toString())
.<SimpleObject, SimpleObject>chunk(5)
.reader(reader)
.processor(processor)
.writer(compositeItemWriter).transactionManager(txManager).build();
然后代码给出编译时错误:
Error:(337, 83) java: cannot find symbol
symbol: method build()
location: class org.springframework.batch.core.step.builder.StepBuilderHelper
答案 0 :(得分:1)
以下代码适用于我的情况。
这是我的作家1:
@Component
public class Writer1 implements ItemStreamWriter<SimpleObject>,org.springframework.batch.item.ItemWriter<SimpleObject> {
@Override
public void write(List<? extends SimpleObject> list) throws Exception {
for(SimpleObject simpleObject: list){
// some logic
}
}
}
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
}
@Override
public void update(ExecutionContext executionContext) throws ItemStreamException {
}
@Override
public void close() throws ItemStreamException {
}
}
这是我的作家2:
@Component
public class Writer2 implements ItemStreamWriter<SimpleObject>, org.springframework.batch.item.ItemWriter<SimpleObject> {
@Override
public void write(List<? extends SimpleObject> list) throws Exception {
for(SimpleObject simpleObject: list){
// some logic
}
}
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
}
@Override
public void update(ExecutionContext executionContext) throws ItemStreamException {
}
@Override
public void close() throws ItemStreamException {
}
}
在这里,我尝试初始化Tasklet
并提供CompositeItemWriter
:
CompositeItemWriter<SimpleObject> compositeItemWriter = new CompositeItemWriter<>();
List<org.springframework.batch.item.ItemWriter<? super SimpleObject>> delegates = new ArrayList<>();
delegates.add(writer1);
delegates.add(writer2);
compositeItemWriter.setDelegates(delegates);
TaskletStep processingStep = stepBuilderFactory.get(getLabel() + "-" + UUID.randomUUID().toString())
.<SimpleObject, SimpleObject>chunk(5)
.reader(reader)
.processor(processor)
.writer(compositeItemWriter)
.stream(writer1)
.stream(writer2)
.transactionManager(txManager)
.build();
如果我遗漏了任何信息或者有不正确的信息,请告诉我。