I want to put variable into Spring Batch context

I use java config, my reader/writer/job are in the same class JobConfig() At the moment I don't want to externalize my writer in a specific class

I create a file with my writer, and in my jobListener (afterJob) I want to copy it in archive folder

So, I can't get my JobExecution to put my file name.

All solutions I fund, talk about xml-config, or a specific class for writer.

How do you inject the context ?

I triyed @BeforeStep @BeforeJob

@Autowired
ExportJobListener exportJobListener;

@Bean
@StepScope
public FlatFileItemWriter<ExportCsv> exportItemWriter(
        @Value("#{jobParameters['dateExport']}") Date dateExport) {

    // Construction du Header
    String exportFileHeader = EXPORT_CSV_HEADER;
    StringHeaderWriter headerWriter = new StringHeaderWriter(exportFileHeader);

    // Construction du nom du fichier
    String nomFichierExport = createExportFileName(dateExport);

    // Chemin complet d'enregistrement du fichier
    String exportFilePath = String.join("/", exportCrePath, nomFichierExport);

    // HERE I DON'T WANT TO DO THIS
    exportJobListener.getJobExecution().getExecutionContext().put("file", "toto");
    // ExecutionContext stepContext = this.stepExecution.getExecutionContext();
    // stepContext.put("file", "toto");

    ...

    return csvFileWriter;
}

@Bean
public Step myStep() throws Exception {
    return stepBuilderFactory.get("myStep")
            .<ExportCsv, ExportCsv>chunk(1000)
            .reader(exportItemReader(WILL_BE_INJECTED))
            .writer(exportItemWriter(WILL_BE_INJECTED)).build();
}

java 8 - Spring 4

0 Answers 11

Not the answer you're looking for? Browse other questions tagged or ask your own question.