Partitioned Job can't stop by itself after finishing? Spring Batch

后端 未结 3 1274
無奈伤痛
無奈伤痛 2021-01-03 01:49

I wrote a Job of two Steps, with one of two steps being a partitioning step. The partition step uses TaskExecutorPartitionHandler and runs 5 slave steps in threads. The job

3条回答
  •  离开以前
    2021-01-03 02:19

    There are 2 solutions to your problem, although I don't know the cause.

    First, you can use a CommandLineJobRunner to launch the Job. See documentation here. This class automatically exits the program at the end of the job and converts the ExitStatus to a return code (COMPLETED = 0, FAILED = 1...). The default return code are provided by a SimpleJvmExitCodeMapper.

    The second solution would be to manually call a System.exit() instruction after your JobLauncher.run(). You can also convert the ExitStatus of the Job manually and use it in your manual exit :

    // Create Job
    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    Job job = (Job) context.getBean(jobName);
    
    // Create return codes mapper
    SimpleJvmExitCodeMapper mapper = new SimpleJvmExitCodeMapper();
    
    // Start Job
    JobExecution execution = jobLauncher.run(job, new JobParameters());
    
    // Close context
    context.close();
    
    // Map codes and exit
    String status = execution.getExitStatus().getExitCode();
    Integer returnCode = mapper.intValue(status);
    System.exit(returnCode);
    

提交回复
热议问题