I am migrating from SB 2.x to SB 3.x and I am aware that @EnableBatchProcessing or extending DefaultBatchConfiguration will back of auto starting of jobs but as i need to use a custom JacksonSearlizer i am using @EnableBatchProcessing(executionContextSerializerRef="myJacksonSerializer")
Can help to suggest how can i takecontrol and start the Job ?
You can add the code that launches the job in the main
method that starts your Spring Boot application, something like:
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws Exception {
ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}
}
Another option is put that code in an ApplicationRunner
bean. Here is an example:
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public ApplicationRunner jobRunner(JobLauncher jobLauncher, Job job) {
return args -> jobLauncher.run(job, new JobParameters());
}
}