Reputation: 1
In case I am passing job parameters, the spring batch job is executed twice, firstly without parameters and then with parameters. I have downloaded many examples from net, the behaviour is same. Is there a way to call spring batch job just once i.e only with parameters?
Class JobParametersApplication :
@EnableBatchProcessing
@SpringBootApplication
public class JobParametersApplication implements CommandLineRunner {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public static void main(String[] args) {
SpringApplication.run(JobParametersApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// Pass the required Job Parameters from here to read it anywhere within Spring Batch infrastructure
JobParameters jobParameters = new JobParametersBuilder().addString("message", "MyHello").addDate("date", new Date())
.addLong("time",System.currentTimeMillis()).toJobParameters();
JobExecution execution = jobLauncher.run(job, jobParameters);
System.out.println("STATUS :: "+execution.getStatus());
}
}
Class JobConfiguration :
@Configuration
public class JobConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
@StepScope
public Tasklet helloWorldTasklet(@Value("#{jobParameters['message']}") String message) {
return (stepContribution, chukContext) -> {
System.out.println(message);
return RepeatStatus.FINISHED;
};
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1").tasklet(helloWorldTasklet(null)).build();
}
@Bean
public Job jobParametersJob() {
return jobBuilderFactory.get("jobParametersJob").start(step1()).build();
}
}
Console output :
2022-09-03 18:15:43.014 INFO 26884 --- [ main] o.s.b.a.b.JobLauncherCommandLineRunner : Running default command line with: []
2022-09-03 18:15:43.054 INFO 26884 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] launched with the following parameters: [{}]
2022-09-03 18:15:43.069 INFO 26884 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
null
2022-09-03 18:15:43.108 INFO 26884 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
2022-09-03 18:15:43.114 INFO 26884 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] launched with the following parameters: [{message=MyHello, date=1662209143110, time=1662209143110}]
2022-09-03 18:15:43.118 INFO 26884 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
MyHello
2022-09-03 18:15:43.124 INFO 26884 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] completed with the following parameters: [{message=MyHello, date=1662209143110, time=1662209143110}] and the following status: [COMPLETED]
STATUS :: COMPLETED
2022-09-03 18:15:43.127 INFO 26884 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2022-09-03 18:15:43.129 INFO 26884 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Upvotes: 0
Views: 2748
Reputation: 1452
Add the following property to your properties or yaml config to prevent automatic execution of jobs
spring.batch.job.enabled=false
Upvotes: 1