gpt4 book ai didi

java - Spring 批处理 : How to create a Composite Item Writer?

转载 作者:行者123 更新时间:2023-12-02 04:04:56 25 4
gpt4 key购买 nike

我在 Spring Boot 应用程序中使用 Spring BatchSpring 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

最佳答案

下面的代码在我的案例中有效。

这是我的作家 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();

如果我遗漏了任何内容或有一些不正确的信息,请告诉我。

关于java - Spring 批处理 : How to create a Composite Item Writer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47967932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com