You can do that by passing these properties as job parameters (those are best passed as non-identifying job parameters in this case) and late-bind them in your reader at runtime. Here is an example:

@Bean
@StepScope
public ItemReader<String[]> itemReader(
        @Value("#{jobParameters['fileName']}") String fileName,
        @Value("#{jobParameters['columnNames']}") String columnNames
        ) {
    return new FlatFileItemReaderBuilder<String[]>()
            .name("hacReader")
            //put file here
            .resource(new FileSystemResource(fileName))
            .lineMapper(new DefaultLineMapper<String[]>() {{
                setLineTokenizer(new DelimitedLineTokenizer() {{
                    //put column names here
                    setNames(columnNames.split(","));
                }});
            }})
            .build();
}

With this setup, the reader will be dynamically configured with the fileName and columnNames specified as job parameters:

JobParameters jobParameters = new JobParametersBuilder()
            .addString("fileName", "/path/to/input/file")
            .addString("columnNames", "column1,column2,column5")
            .toJobParameters();

Hope this helps.

Answer from Mahmoud Ben Hassine on Stack Overflow
🌐
Terasoluna-batch
terasoluna-batch.github.io › guideline › 5.0.0.RELEASE › en › Ch04_JobParameter.html
Job parameters
DefaultJobParametersConverter can specify data types for parameters, but handling becomes complicated when type conversion fails. In TERASOLUNA Batch 5.x, by using JsrJobParametersConverter, RUN_ID is automatically assigned without the user knowledge. By this, the same job is handled as a different job in Spring Batch as seen by the user.
🌐
GitHub
github.com › acogoluegnes › Spring-Batch-Workshop › blob › master › dynamic-job-parameters-solution › src › main › resources › dynamic-job-parameters-job.xml
Spring-Batch-Workshop/dynamic-job-parameters-solution/src/main/resources/dynamic-job-parameters-job.xml at master · acogoluegnes/Spring-Batch-Workshop
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd"> · <batch:job id="dynamicJobParametersJob"> <batch:step id="dynamicJobParametersStep"> <batch:tasklet> <batch:chunk reader="reader" writer="writer" commit-interval="3"/> </batch:tasklet> </batch:step> </batch:job> ·
Author   acogoluegnes
Discussions

java - Dynamically configure a Job in spring-batch - Stack Overflow
Is it possible to dynamically configure a Job in spring-batch? Here is what I want to do. I have created several different ItemReader, ItemWriter like below: FlatFileItemReader DBItemReader More on stackoverflow.com
🌐 stackoverflow.com
Spring Batch - How to access JobParameters or JobExecution for a dynamically created Job? - Stack Overflow
I have a dynamically created/configured list of ReportJobs which are implemented as standard spring batch job instances. These jobs are executed by the Spring Task Scheduler @Override public void More on stackoverflow.com
🌐 stackoverflow.com
How do I set JobParameters in spring batch with spring-boot - Stack Overflow
I followed the guide at http://spring.io/guides/gs/batch-processing/ but it describes a job with no configurable parameters. I'm using Maven to build my project. I'm porting an existing job that I... More on stackoverflow.com
🌐 stackoverflow.com
java - running a job in foreach loop with dynamic parameters spring batch - Stack Overflow
I have created a spring batch job with spring boot. I customized the Reader to get json data from REST API and convert data to java object and the Writer will push data to queue. i am calling my job in foreach loop to set parameters and send request to REST API with different langauges. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Baeldung
baeldung.com › home › spring › access job parameters from itemreader in spring batch
Access Job Parameters From ItemReader in Spring Batch | Baeldung
July 6, 2024 - Moreover, it makes it possible to create a controller that can initiate a job based on parameters provided by the client. In our scenario, we’ll utilize this class to hold application parameters and dynamics runtime parameters. In addition to the well-known bean scopes in regular Spring, Spring Batch introduces two additional scopes: StepScope and JobScope.
🌐
Javainuse
javainuse.com › spring › batch › jobparam
Spring Boot + Spring Batch Job Parameters Example
JobParameters can be used for identification during the job run. They have reserved names, so to access them we can use Spring Expression Language For example we can provide the following as job parameters- File path of the source location containing the files to be processed by String Batch
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 65391522 › running-a-job-in-foreach-loop-with-dynamic-parameters-spring-batch
java - running a job in foreach loop with dynamic parameters spring batch - Stack Overflow
public class CodeAndLabelItemWriter implements ItemWriter<CodeAndLabel>{ private AmqpTemplate template; public CodeAndLabelItemWriter(AmqpTemplate template) { this.template = template; } @Override public void write(List<? extends CodeAndLabel> items) throws Exception { if (log.isDebugEnabled()) { log.debug("Writing to RabbitMQ with " + items.size() + " items."); } for(CodeAndLabel item : items) { template.convertAndSend(BatchConfiguration.topicExchangeName,"com.batchprocessing.queue",item); System.out.println("item : "+item); } } ... @Component public class JobCompletionNotificationListener extends JobExecutionListenerSupport { @Autowired private JdbcTemplate jdbcTemplate; @Override public void afterJob(JobExecution jobExecution) { if (jobExecution.getStatus() == BatchStatus.COMPLETED) { System.out.println("JOB FINISHED"); } }
🌐
Stack Overflow
stackoverflow.com › questions › 74586632 › dynamic-name-for-a-spring-batch-job
Dynamic name for a spring batch job - Stack Overflow
The problem is to create (as easy as possible) and then externally and automatically manage 30 jobs (which load data in 30 tables) by some external manager application using a rest interface, at different points in time based on data availability and table dependency. So using system properties to pass a user chosen parameter to spring-batch via a rest controller cannot be done safely, dynamically, in a single deployment as there will be concurrency problems accessing the property in order to start the jobs at any moments 2022-12-06T12:44:21.403Z+00:00
🌐
CodingTechRoom
codingtechroom.com › question › -how-to-use-jobparameters-in-spring-batch
How to Use JobParameters in Spring Batch - CodingTechRoom
JobParameters in Spring Batch serve as a way to pass parameters to batch jobs at runtime. These parameters facilitate the operation of the job by allowing dynamic input values such as file names or execution dates, enhancing the flexibility ...
🌐
Tutorials Knight
tutorialsknight.com › home › spring batch › job parameters in spring batch - a comprehensive exploration
Job Parameters in Spring Batch - A Comprehensive Exploration
August 26, 2023 - Job parameters allow developers to dynamically customize the processing behavior of a job instance without modifying the underlying code. This is particularly useful when a job needs to be executed with different settings or data sources.
🌐
tutorialpedia
tutorialpedia.org › blog › how-to-get-access-to-job-parameters-from-itemreader-in-spring-batch
How to Access Job Parameters in ItemReader with Spring Batch: Fixing 'jobParameters' Not Found Error — tutorialpedia.org
A key feature of Spring Batch is **job parameters**—dynamic key-value pairs passed at job launch time that configure job behavior (e.g., input file paths, date ranges, or user IDs).
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › enterprise java › spring › batch
Spring Batch Job Parameters Example - Java Code Geeks
February 15, 2019 - In this article we shall show you how you can pass job parameters to a spring batch job. 1. Introduction A typical enterprise application requires a batch
Top answer
1 of 2
2

There are various questions.

1. To disable starting all jobs automatically on startup just configure this Spring Boot property:

spring.batch.job.enabled=false # Do not execute all Spring Batch jobs in the context on startup.

2. You would run job like this:

java -Dspring.batch.job.names=prepareTeaJob -jar target/0910-job-parameters-cli-0.0.2-SNAPSHOT.jar sugarAmount="no sugar"

sugar amount there is Job parameter. Full example belonging to this listing is hosted in my Github Repository here. BTW, there are much more examples covering these various Spring Batch topics if you are learning Spring Batch.

3. Not sure what you are referring to as "ApplicationReadyEvent". AFAIK job should be executed after the Spring context it fully initialized.

2 of 2
0

My solution was the following:

  • Set the spring.batch.job.enabled property to false - this prevented jobs to start when the context was setup

  • Get the JobLauncher out of the context and run any Job myself:

    SpringApplication app = new SpringApplication(BatchProcessingServiceStarter.class);
    app.setWebEnvironment(false); 
    
    ConfigurableApplicationContext ctx=app.run(args);
    JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
    JobParameters jobParameters = new JobParametersBuilder()
        .addDate("date", new Date())
        .toJobParameters();  
    
    if(!"1".equals(args[0])){       
        jobLauncher.run(ctx.getBean("BatchConfiguration2", Job.class), jobParameters);                   
    } else {
        jobLauncher.run(ctx.getBean("BatchConfiguration1",  Job.class), jobParameters);   
    } 
    
🌐
codecentric
codecentric.de › en › knowledge-hub › blog › spring-batch-2-2-javaconfig-part-2-jobparameters-executioncontext-and-stepscope
Spring Batch 2.2 JavaConfig: Job Parameters & Context
June 8, 2013 - Access JobParameters and ExecutionContext in Spring Batch 2.2 with JavaConfig. Learn to use @StepScope and @Value for dynamic, flexible batch job configurations. Optimize your Spring Batch jobs.