0
votes

I am currently passing the file name in spring batch job parameter in command line and running my job, the spring batch job will look for file and read, process and write the file. I am currently the job parameter file name in reader and reading the file name, how can I use the same job parameter file name in processor and writer.

1
How did you do that for the reader? Can you share your code? If you managed to do it for the reader, you should be able to do it for the writer as well using the same approach. - Mahmoud Ben Hassine

1 Answers

0
votes

One of the ways to access the Job Parameters is to implement StepExecutionListener to your processor/writer Class to make use of its Overridden methods beforeStep and afterStep,

public class TestWriter implements ItemWriter<TestData>,StepExecutionListener {

    private String filePath;

    @Override
    public void beforeStep(StepExecution stepExecution) {

        try {
            filePath = (String) stepExecution.getJobExecution().getExecutionContext()
            .get("filePath");
        } catch (Exception e) {
            logger.error("Exception while performing read {}", e);
        }
    }


    @Override
    public void write(List<? extends TestData> items) throws Exception {
        // filePath value read from the job execution can be used inside read use case impl

    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return ExitStatus.COMPLETED;
    }
}

To Add a value to the jobExecutionContext, you can use like below,

stepExecution.getJobExecution().getExecutionContext().put("path", filePath);