Spring批处理:如何创建复合项目编写器?
Spring Batch: How to create a Composite Item Writer?
我在 Spring Boot
应用程序中使用 Spring Batch
。 Spring Boot
版本为 1.3.5.RELEASE
。
我正在尝试使用 CompositeItemWriter
,这样项目列表将首先由 WriterOne compressed/zipped,然后传递给 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();
如果我遗漏了什么或有不正确的信息,请告诉我。
我在 Spring Boot
应用程序中使用 Spring Batch
。 Spring Boot
版本为 1.3.5.RELEASE
。
我正在尝试使用 CompositeItemWriter
,这样项目列表将首先由 WriterOne compressed/zipped,然后传递给 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();
如果我遗漏了什么或有不正确的信息,请告诉我。