我正在从 SB 2.x 迁移到 SB 3.x,我知道 @EnableBatchProcessing 或扩展 DefaultBatchConfiguration 将支持自动启动作业,但由于我需要使用自定义 JacksonSearlizer,所以我正在使用 @EnableBatchProcessing(executionContextSerializerRef="myJacksonSerializer ”)
可以帮助建议我如何控制并开始工作吗?
您可以在启动 Spring Boot 应用程序的
main
方法中添加启动作业的代码,例如:
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());
}
}
另一种选择是将该代码放入
ApplicationRunner
bean 中。这是一个例子:
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());
}
}