Spring Batch: How to create a Composite Item Writer?

只谈情不闲聊 提交于 2019-12-08 02:16:44

问题


I am using Spring Batch in Spring Boot application. The Spring Boot version is 1.3.5.RELEASE.

I am trying to use CompositeItemWriter so that the list of items will first be compressed/zipped by WriterOne and then be passed to WriterTwo where they will be written in the database.

Here is my writer 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
        }
    }
}

Here is my writer 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.
        }
    }
}

Here is I am trying to initializing task-step and providing it a CompositeItemWriter instead of 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();

Then the code gives compile time error:

Error:(337, 83) java: cannot find symbol
  symbol:   method build()
  location: class org.springframework.batch.core.step.builder.StepBuilderHelper

回答1:


This below code worked in my case.

Here is my writer 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 {

    }
}

Here is my writer 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 {

    }
}

Here I am trying to initializing Tasklet and providing it a 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();

Kindly let me know if I missed anything or had some incorrect information.



来源:https://stackoverflow.com/questions/47967932/spring-batch-how-to-create-a-composite-item-writer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!