[英]How Spring Boot run batch jobs
I followed this sample for Spring Batch with Boot. 我按照这个样本进行Spring Batch with Boot。
When you run the main method the job is executed. 运行main方法时,将执行作业。 This way I can't figure out how one can control the job execution.
这样我就无法弄清楚如何控制作业执行。 For example how you schedule a job, or get access to the job execution, or set job parameters.
例如,您如何安排作业,或访问作业执行或设置作业参数。
I tried to register my own JobLauncher 我试着注册我自己的JobLauncher
@Bean
public JobLauncher jobLauncher(JobRepository jobRepo){
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setJobRepository(jobRepo);
return simpleJobLauncher;
}
but when I try to use it in the main method: 但是当我尝试在main方法中使用它时:
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
//try catch removed for readability
jobLauncher.run(ctx.getBean(Job.class), new JobParameters());
}
The job is again executed when the context is loaded and I got JobInstanceAlreadyCompleteException
when I try to run it manually. 加载上下文时再次执行作业,当我尝试手动运行时,我得到了
JobInstanceAlreadyCompleteException
。 Is there a way to prevent the automatic job execution? 有没有办法阻止自动执行作业?
The jobs execution can be prevented by setting 可以通过设置来防止作业执行
spring.batch.job.enabled=false
in application.properties. 在application.properties中。 Or you can use
spring.batch.job.names
it takes a comma-delimited list of job names that will be run. 或者您可以使用
spring.batch.job.names
它将以逗号分隔的作业名称列表进行运行。
Taken from here: how to stop spring batch scheduled jobs from running at first time when executing the code? 从这里开始: 如何在执行代码时第一次停止弹出批量预定作业?
You can enable the execution of a Job using rest controller POST: 您可以使用休息控制器POST启用作业的执行:
@RestController
@RequestMapping(value="/job/")
public class JobLauncherController {
private static final Log LOG = LogFactory.getLog(JobLauncherController.class);
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
@Autowired
private JobRepository jobRepository;
@Autowired
private JobRegistry jobRegistry;
@RequestMapping("/launchjob/{jobName}")
public String handle(@PathVariable("jobName") String jobName, @RequestBody Map<String,Object> request) throws Exception {
try {
request.put("timeJobStarted", DateUtil.getDateFormatted(new Date(), DateUtil.DATE_UUUUMMDDHHMMSS));
Map<String,Object> mapMessage = this.enrichJobMessage(request);
Map<String, JobParameter> jobParameters = new HashMap<>();
mapMessage.forEach((k,v)->{
MapperUtil.castParameter(jobParameters, k, v);
});
jobParameters.put(Field.Batch.JOB_INSTANCE_NAME, new JobParameter(jobName));
jobLauncher.run(job, new JobParameters(jobParameters));
assertNotNull(jobRegistry.getJob(job.getName()));
}catch( NoSuchJobException ex){
jobRegistry.register(new ReferenceJobFactory(job));
} catch (Exception e) {
LOG.error(e.getMessage(),e);
}
return "Done";
}
public static void castParameter(Map<String, JobParameter> jobParameters, String k, Object v){
if(v instanceof String){
jobParameters.put(k, new JobParameter((String)v));
}else if(v instanceof Date){
jobParameters.put(k, new JobParameter((Date)v));
}else if(v instanceof Double){
jobParameters.put(k, new JobParameter((Double)v));
}else if(v instanceof Long){
jobParameters.put(k, new JobParameter((Long)v));
}else{
DslJson dslJson = new DslJson<>();
JsonWriter writer = dslJson.newWriter();
try {
dslJson.serialize(writer,v);
jobParameters.put(k, new JobParameter(writer.toString()));
} catch (IOException e) {
LOG.warn(e.getMessage(), e);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.