【问题标题】:How Spring Boot run batch jobsSpring Boot 如何运行批处理作业
【发布时间】:2014-06-20 07:22:23
【问题描述】:

我关注 this sample 的 Spring Batch with Boot。

当您运行 main 方法时,作业将被执行。 这样我就无法弄清楚如何控制作业执行。例如,您如何安排作业、访问作业执行或设置作业参数。

我尝试注册自己的 JobLauncher

@Bean
public JobLauncher jobLauncher(JobRepository jobRepo){
    SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
    simpleJobLauncher.setJobRepository(jobRepo);
    return simpleJobLauncher;
}

但是当我尝试在 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());   
}

加载上下文时再次执行该作业,当我尝试手动运行它时得到JobInstanceAlreadyCompleteException。 有没有办法阻止自动执行作业?

【问题讨论】:

    标签: java spring spring-batch spring-boot


    【解决方案1】:

    可以通过设置阻止作业执行

    spring.batch.job.enabled=false
    

    在 application.properties 中。或者您可以使用spring.batch.job.names,它采用逗号分隔的将要运行的作业名称列表。

    取自这里:how to stop spring batch scheduled jobs from running at first time when executing the code?

    【讨论】:

    • 我尝试了 spring.batch.job.enabled=false 并使用 java sys properties -Dspring.batch.job.enabled=false 运行 jar 仍然执行作业。怎么调试的,我不知道
    • @Anuj Acharya 你可以提出一个问题,在那里你可以提供有关问题的更多详细信息(如春季版本)并分享一些代码。
    • 抱歉 Evgeni,我创建了一个单独的问题 stackoverflow.com/questions/31276011/…
    【解决方案2】:

    您可以使用休息控制器 POST 启用 Job 的执行:

    @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);
            }                       
        }
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 2014-09-28
      • 2020-08-29
      • 2021-01-15
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 2017-07-06
      相关资源
      最近更新 更多