To Stop the job from automatic run at the time of server start we should add the below property in application.properties
- Jobs would be created in JobsConfig.java and available as Bean
- JobService.java helps in launching the job. startJob method reads the json parameter and passed them as jobparameter to job
- JobController.java takes jobname as parameter along with json object in body
- JSON body contains JobParamsRequest datatype as argument
- @EnableAsync is a Spring annotation that activates asynchronous method execution in application. Spring starts scanning for methods annotated with @Async and runs them in separate threads, allowing your app to perform tasks without blocking the main thread
- @Async annotation is used over startJob in jobService.java
- createJobParam method uses builder method which builds job parameters
- JSON should be supplied with paramKey and paramValue
application.properties
. . spring.batch.job.enabled=false . .
json as in bruno
[
{
"paramKey": "Name",
"paramValue": "Mugil"
},
{
"paramKey": "Age",
"paramValue": 38
},
{
"paramKey": "Location",
"paramValue": "Chennai"
}
]
SpringBatchWithRestApp.java
@SpringBootApplication
@EnableBatchProcessing
@EnableAsync
public class SpringBatchWithRestApp {
public static void main(String[] args) {
SpringApplication.run(SpringBatchWithRestApp.class, args);
}
}
JobController.java
@RestController
@RequestMapping("/jobs/")
public class JobController {
@Autowired
JobService jobService;
@PostMapping("/{jobName}")
public String triggerJob(@PathVariable String jobName, @RequestBody List<JobParamsRequest> jobParamsRequestList) throws Exception {
jobService.startJob(jobName, jobParamsRequestList);
return "Job Started... .";
}
}
JobService.java
@Service
public class JobService {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job simpleTaskLetJob;
@Async
public void startJob(String jobName, List<JobParamsRequest> jobParamsRequestList) throws Exception {
JobExecution jobExecution = null;
//Thread Added for Testing Async Behaviour
Thread.sleep(3000);
try {
if(jobName.equals("TaskletJob")){
jobExecution = jobLauncher.run(simpleTaskLetJob, createJobParam(jobParamsRequestList));
System.out.println("Job Execution ID = " + jobExecution.getId());
}else{
System.out.println("Invalid Job Name");
}
} catch (Exception e) {
System.out.println("Exception while starting job "+ e.getMessage());
}
}
public JobParameters createJobParam(List<JobParamsRequest> arrRequset) {
JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
arrRequset.forEach(jobParamsRequest -> jobParametersBuilder
.addString(jobParamsRequest.getParamKey(), jobParamsRequest.getParamValue())
.addLong("time", System.currentTimeMillis()));
return jobParametersBuilder.toJobParameters();
}
}
JobParamsRequest.java
@Setter
@Getter
public class JobParamsRequest {
private String paramKey;
private String paramValue;
}
JobsConfig.java
@Configuration
public class JobsConfig {
@Bean
public Job simpleTaskLetJob(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new JobBuilder("job", jobRepository)
.incrementer(new RunIdIncrementer())
.start(simpleTaskletStep(jobRepository, transactionManager))
.build();
}
@Bean
public Step simpleTaskletStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
StepBuilder stepBuilderOne = new StepBuilder("Tasklet Oriented Step", jobRepository);
return stepBuilderOne
.tasklet(simpleTaskLet(), transactionManager)
.build();
}
@Bean
public Tasklet simpleTaskLet(){
return (StepContribution contribution, ChunkContext chunkContext) -> {
return RepeatStatus.FINISHED;
};
}
}
Output
2025-09-15T18:20:12.052+05:30 INFO 25776 --- [FileChecker] [ task-1] o.s.b.c.l.s.TaskExecutorJobLauncher : Job: [SimpleJob: [name=job]] launched with the following parameters: [{'time':'{value=1757940612019, type=class java.lang.Long, identifying=true}','Age':'{value=38, type=class java.lang.String, identifying=true}','Name':'{value=Mugil, type=class java.lang.String, identifying=true}','Location':'{value=Chennai, type=class java.lang.String, identifying=true}'}]
2025-09-15T18:20:12.063+05:30 INFO 25776 --- [FileChecker] [ task-1] o.s.batch.core.job.SimpleStepHandler : Executing step: [Tasklet Oriented Step]
2025-09-15T18:20:12.068+05:30 INFO 25776 --- [FileChecker] [ task-1] o.s.batch.core.step.AbstractStep : Step: [Tasklet Oriented Step] executed in 5ms
2025-09-15T18:20:12.071+05:30 INFO 25776 --- [FileChecker] [ task-1] o.s.b.c.l.s.TaskExecutorJobLauncher : Job: [SimpleJob: [name=job]] completed with the following parameters: [{'time':'{value=1757940612019, type=class java.lang.Long, identifying=true}','Age':'{value=38, type=class java.lang.String, identifying=true}','Name':'{value=Mugil, type=class java.lang.String, identifying=true}','Location':'{value=Chennai, type=class java.lang.String, identifying=true}'}] and the following status: [COMPLETED] in 13ms
Job Execution ID = 56



Stopping Spring Batch Job
JobController.java
@RestController
@RequestMapping("/jobs")
public class JobController {
@Autowired
JobService jobService;
@Autowired
JobOperator jobOperator;
@PostMapping("/start/{jobName}")
public String triggerJob(@PathVariable String jobName, @RequestBody List<JobParamsRequest> jobParamsRequestList) throws Exception {
jobService.startJob(jobName, jobParamsRequestList);
return "Job Started... .";
}
@GetMapping("/stop/{jobExecutionId}")
public String StopJob(@PathVariable Long jobExecutionId) throws Exception {
jobOperator.stop(jobExecutionId);
return "Job Stopped...";
}
}
Note:
To Stop a running job, jobexecutionId should be passed as a parameter which could be fetched from batch_job_execution table in DB which has status as STARTED