44

I followed this sample for Spring Batch with Boot.

When you run the main method the job is executed. 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

@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:

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. Is there a way to prevent the automatic job execution?

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145

2 Answers2

62

The jobs execution can be prevented by setting

spring.batch.job.enabled=false

in application.properties. Or you can use spring.batch.job.names it takes a comma-delimited list of job names that will be run.

Taken from here: how to stop spring batch scheduled jobs from running at first time when executing the code?

Community
  • 1
  • 1
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • I tried spring.batch.job.enabled=false and also running the jar with java sys properties -Dspring.batch.job.enabled=false still it execute the job. How it debug it, I have no clue – Anuj Acharya Jul 07 '15 at 17:44
  • 2
    @Anuj Acharya You can ask a question about it where you can give more details about the problem(like the spring version) and share some code. – Evgeni Dimitrov Jul 07 '15 at 17:59
  • Sorry Evgeni, I have created a seprate question http://stackoverflow.com/questions/31276011/spring-boot-spring-batch-job-enabled-false-not-able-to-recognize – Anuj Acharya Jul 07 '15 at 18:13
2

You can enable the execution of a Job using rest controller 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);
        }                       
    }
}

}
dmotta
  • 1,843
  • 2
  • 21
  • 32