Search code examples
javajsonspringspring-batchnio

Delete the json file after reading its content via JsonItemReader in Sping Batch


How to delete the json file after reading its content via JsonItemReader?

Below is the sample code of my JsonItemReader.

@Bean
@StepScope
public JsonItemReader<MyObj> myReader() {

    LOGGER.info(LOG_TEMPLATE,
            getClass().getSimpleName(),
            Thread.currentThread().getStackTrace()[1].getMethodName(),
            "Inside Reader...");

    final ObjectMapper mapper = new ObjectMapper();

    final JacksonJsonObjectReader<MyObj> jsonObjectReader = new JacksonJsonObjectReader<>(
            MyObj.class);
    jsonObjectReader.setMapper(mapper);

    final String filePath = config.getRootfolder() + SEPERATOR + inputInfo.getFileName();

    return new JsonItemReaderBuilder<MyObj>().jsonObjectReader(jsonObjectReader)
            .resource(new FileSystemResource(filePath))
            .name("myReader")
            .build();

}

Then i added a code in my JobListener that after job execution it will delete the said file. Here is the sample code.

@Override
    public void afterJob(JobExecution jobExecution) {

        if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
            LOGGER.info(LOG_TEMPLATE,
                    getClass().getSimpleName(),
                    Thread.currentThread().getStackTrace()[1].getMethodName(),
                    "Job Completed, verify results.");
        } else {
            LOGGER.error(LOG_TEMPLATE,
                    getClass().getSimpleName(),
                    Thread.currentThread().getStackTrace()[1].getMethodName(),
                    "Job ended abnormally.");
        }

        try {
            Files.delete(Paths.get(config.getRootfolder() + "/" + inputInfo.getFileName()));
        } catch (final IOException ex) {

            LOGGER.error(LOG_TEMPLATE,
                    getClass().getSimpleName(),
                    Thread.currentThread().getStackTrace()[1].getMethodName(),
                    ex.getMessage());
        }

    }

But i am getting an error that file is still open or still being read.


Solution

  • I dont know if this is the proper and efficient way of deleting the json file afer the JsonItemReader Step but this way works for me.

    What i did was i created a new Step with Tasklet that deletes the said json file. Here is the sample code.

    Tasklet Class:

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    
        LOGGER.info(LOG_TEMPLATE,
                getClass().getSimpleName(),
                Thread.currentThread().getStackTrace()[1].getMethodName(),
                "Inside Delete Json File Tasklet");
    
        try {
    
            final String filePath = config.getRootfolder() + SEPARATOR + inputInfo.getFileName();
            Files.delete(Paths.get(filePath));
    
        } catch (final IOException ex) {
    
            LOGGER.error(LOG_TEMPLATE,
                    getClass().getSimpleName(),
                    Thread.currentThread().getStackTrace()[1].getMethodName(),
                    ex.getMessage());
        }
    
        return RepeatStatus.FINISHED;
    }