Task is to read excel files in spring batch where the resource path get after the file download from GCS.
pom.xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.batch.extensions</groupId>
<artifactId>spring-batch-excel</artifactId>
<version>0.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
ExcelReader Method
@Bean
@StepScope
public ItemReader<HashMap<String, String>> excelReader() {
PoiItemReader poiItemReader = new PoiItemReader();
poiItemReader.setResource(new FileSystemResource(Constants.absolutePath));
poiItemReader.setLinesToSkip(1);
poiItemReader.setRowMapper(excelRowMapper());
return poiItemReader;
}
After downloading from GCS file path is stored in static variable Constants.absolutePath file is saving correctly.
But an error is causing like this.
ErrorTrace
java.lang.NullPointerException: null
at org.springframework.batch.extensions.excel.poi.PoiItemReader.getNumberOfSheets(PoiItemReader.java:56) ~[spring-batch-excel-0.1.1.jar:na]
at org.springframework.batch.extensions.excel.AbstractExcelItemReader.nextSheet(AbstractExcelItemReader.java:178) ~[spring-batch-excel-0.1.1.jar:na]
While debugging the workbook field is Null.
I have checked with the path is hardcoded without @StepScope it works correctly.
I need a solution with dynamically setting the resource for the batch process.
Changing the return type of the
excelReadertoPoiItemReaderworked for me. I think that is because first the file has to be opened, so the methodopen(...)onItemStreamneeds to be called. If you only provide anItemReaderspring does not identify your reader asItemStreamand hence there is no call toopen.