小编典典

Spring Batch:如何创建复合项目编写器?

spring-boot

Spring BatchSpring 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

阅读 373

收藏
2020-05-30

共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();

请让我知道我是否错过了任何内容或信息不正确。

2020-05-30