I'm implementing my own custom item writer that gets an excel as input data, and returns as a output another excel which is the same file but with a specific row modified where all its cells are -1.
The program modifies the file as expected but after writing the file I get a lot of exceptions from nowhere, despite it doesn't affect the requirement of what I want to do, I'm afraid if those exceptions could make a potential barrier on future features I could add to my batch application. How can I avoid this?
org.springframework.batch.item.ItemStreamException: Error while closing item reader
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:144) ~[spring-batch-infrastructure-5.1.1.jar:5.1.1]
at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:111) ~[spring-batch-infrastructure-5.1.1.jar:5.1.1]
at org.springframework.batch.core.step.tasklet.TaskletStep.close(TaskletStep.java:287) ~[spring-batch-core-5.1.1.jar:5.1.1]
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:312) ~[spring-batch-core-5.1.1.jar:5.1.1]
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:153) ~[spring-batch-core-5.1.1.jar:5.1.1]
at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:418) ~[spring-batch-core-5.1.1.jar:5.1.1]
at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:132) ~[spring-batch-core-5.1.1.jar:5.1.1]
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:317) ~[spring-batch-core-5.1.1.jar:5.1.1]
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:157) ~[spring-batch-core-5.1.1.jar:5.1.1]
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) ~[spring-core-6.1.4.jar:6.1.4]
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:148) ~[spring-batch-core-5.1.1.jar:5.1.1]
at org.springframework.batch.core.launch.support.TaskExecutorJobLauncher.run(TaskExecutorJobLauncher.java:59) ~[spring-batch-core-5.1.1.jar:5.1.1]
at com.batch.spring.apache.scheduler.CustomScheduler.schedule(CustomScheduler.java:24) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMethod.invoke(InitDestroyAnnotationBeanPostProcessor.java:457) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:401) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:219) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:422) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975) ~[spring-beans-6.1.4.jar:6.1.4]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:959) ~[spring-context-6.1.4.jar:6.1.4]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624) ~[spring-context-6.1.4.jar:6.1.4]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.2.3.jar:3.2.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.2.3.jar:3.2.3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.2.3.jar:3.2.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:334) ~[spring-boot-3.2.3.jar:3.2.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354) ~[spring-boot-3.2.3.jar:3.2.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-3.2.3.jar:3.2.3]
at com.batch.spring.apache.app.SpringBatchApplication.main(SpringBatchApplication.java:12) ~[classes/:na]
Caused by: org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : The part /xl/sharedStrings.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@78318ac2
at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:554) ~[poi-ooxml-4.1.2.jar:4.1.2]
at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1505) ~[poi-ooxml-4.1.2.jar:4.1.2]
at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1492) ~[poi-ooxml-4.1.2.jar:4.1.2]
at org.apache.poi.openxml4j.opc.ZipPackage.closeImpl(ZipPackage.java:427) ~[poi-ooxml-4.1.2.jar:4.1.2]
at org.apache.poi.openxml4j.opc.OPCPackage.close(OPCPackage.java:457) ~[poi-ooxml-4.1.2.jar:4.1.2]
at org.apache.poi.ooxml.POIXMLDocument.close(POIXMLDocument.java:204) ~[poi-ooxml-4.1.2.jar:4.1.2]
at org.apache.poi.xssf.usermodel.XSSFWorkbook.close(XSSFWorkbook.java:594) ~[poi-ooxml-4.1.2.jar:4.1.2]
at org.springframework.batch.extensions.excel.poi.PoiItemReader.doClose(PoiItemReader.java:68) ~[spring-batch-excel-0.1.1.jar:na]
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:141) ~[spring-batch-infrastructure-5.1.1.jar:5.1.1]
... 37 common frames omitted
Caused by: org.apache.poi.openxml4j.exceptions.OpenXML4JException: The part /xl/sharedStrings.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@78318ac2
at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:543) ~[poi-ooxml-4.1.2.jar:4.1.2]
... 45 common frames omitted
CustomItemWriter
public class CustomItemWriter implements ItemWriter<Model> {
private static final Integer skipLine = 1;
private static final String directory = "example.xlsx";
@Override
public void write(Chunk<? extends Model> chunk) throws Exception {
try (FileInputStream inputStream = new FileInputStream(directory)) {
final Workbook workbook = new XSSFWorkbook(inputStream);
final Sheet sheet = workbook.getSheetAt(0);
final Row row = sheet.getRow(skipLine);
row.cellIterator().forEachRemaining(cell -> cell.setCellValue(-1));
try (FileOutputStream outputStream = new FileOutputStream(directory)) {
workbook.write(outputStream);
}
workbook.close();
}
}
}
Spring Batch Configuration Class
@Configuration
public class SpringBatchConfiguration {
@Autowired
private JobRepository jobRepository;
@Autowired
private PlatformTransactionManager transactionManager;
private static final Integer skipLine = 1;
private static final String directory = "example.xlsx";
@Bean
public Job exampleJob(
final Step dataMigrationStep,
final Step markExcelAsReadStep) {
return new JobBuilder("exampleJob", jobRepository)
.start(markExcelAsReadStep)
.build();
}
@Bean
public Step markExcelAsReadStep(
final ItemReader<Model> reader,
final CustomItemWriter writer) {
return new StepBuilder("markExcelAsReadStep", jobRepository)
.<Model, Model>chunk(1, transactionManager)
.reader(reader)
.writer(writer)
.build();
}
@Bean
public ItemReader<Model> itemReader() {
final var reader = new PoiItemReader<Model>();
reader.setResource(new FileSystemResource(directory));
reader.setRowMapper(excelRowMapper());
reader.setLinesToSkip(skipLine);
reader.setMaxItemCount(1);
return reader;
}
@Bean
public CustomItemWriter customItemWriter() {
return new CustomItemWriter();
}
@Bean
public RowMapper<Model> excelRowMapper() {
final var mapper = new BeanWrapperRowMapper<Model>();
mapper.setTargetType(Model.class);
return mapper;
}
@Bean
public CustomScheduler customScheduler() {
return new CustomScheduler();
}
}
public class Model {
private String name;
private String email;
public Model() { }
public Model(String name, String email) {
super();
this.name = name;
this.email = email;
}
public final String getName() {
return name;
}
public final String getEmail() {
return email;
}
public final void setName(String name) {
this.name = name;
}
public final void setEmail(String email) {
this.email = email;
}
}
I provide the source code here so you can reproduce the error on the easiest way: https://github.com/dev-pascal/apache
I tried to look on the official Spring Batch Excel Extension Repository (https://github.com/spring-projects/spring-batch-extensions/tree/main/spring-batch-excel) but there's not writer implementation so I decided to make my own custom item writer instead. I tried as well to look at official Spring Batch documentation (https://docs.spring.io/spring-batch/reference/readers-and-writers/custom.html#restartableWriter) in "Making the ItemWriter Restartable" section cause I thought it could possibly have a hint on why this error is happening, but no, implementing ItemStream is useless and does nothing and the exceptions are still reproducing. As well investigating on Spring Batch - how to generate Output file of a batch job as excel sheet