Spring Batch: How to create a Composite Item Writer?

13.9k views Asked by At

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

There are 1 answers

2
Ashish Pancholi On BEST ANSWER

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.