Spring批处理作业在完成后退出。(在调试期间甚至不通过键盘快捷键退出)

时间:2018-02-22 20:28:21

标签: java spring spring-batch

我是Spring Batch的新手。我已经编写了一个弹簧批处理作业的代码,它也在工作。但是在完成这项工作后它就没有退出。即使在调试期间我也无法通过键盘快捷键来停止它。

如果没有文件,则退出。但是当文件可用时,它将在执行后结束。我不喜欢使用System.exit。

对此的任何帮助都会很棒并且事先感谢。

  public class OfferRedemptionJobConfig {
@Bean
public Job RedemptionJob(){
    Flow flow = new FlowBuilder<SimpleFlow>("RedemptionJobFlow")
            .start(jobValidatorStep())
            .next(fileDecisionDecider)
            .on("COMPLETED")
            .end()
            .from(fileDecisionDecider)
            .on("CONTINUE")
            .to(RedemptionFileTaskletStep())
            .next(redemptionFileReprocessStep())
            .next(fileDecisionDecider)
            .on("COMPLETED")
            .end().build();
    return jobBuilderFactory.get("RedemptionJob")
            .incrementer(new RunIdIncrementer())
            .listener(new DefaultJobListener())
            .start(flow).end().build();
}
 @Bean
public Step redemptionFileReprocessStep() {
    return stepBuilderFactory.get("redemptionFileReprocessStep")
            .<RedemptionVO, RedemptionVO>chunk(100)
            .reader(RedemptionFileReader(WILL_BE_INJECTED))
            .processor(FileProcessor(WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED))
            .listener(RedemptionFileListenerStep(WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED,WILL_BE_INJECTED))
            .build();

}
 @Bean
@StepScope
public FlatFileItemReader<RedemptionVO> RedemptionFileReader(@Value("#{jobExecutionContext['localDownloadedPath']}") String filepath) {

    FlatFileItemReader<RedemptionVO> reader = new FlatFileItemReader<>();

    logger.info("FILE READER FILE PATH {}",filepath);
    reader.setResource(new FileSystemResource(new File(filepath)));
    reader.setLineMapper(RedemptionFileLineMapper());
    reader.setLinesToSkip(1);

    return reader;
}
}
}
 @Component
public class OfferRedemptionJob {
private static final Logger logger = LoggerFactory.getLogger(YTBSOfferRedemptionJob.class);
@Autowired
JobRequestLauncher jobRequestLauncher;

@Autowired
BatchJobConfigurationRepo batchJobConfigurationRepo;

@Autowired
CommonJobsHelper commonJobsHelper;
public void run() {
    logger.info("Job is runnning");
    List<BatchJobConfigurationEntity> batchJobConfigurationRepoList = batchJobConfigurationRepo.getAllActiveJobConfigsByJobName("ytbsRedemptionJob");

    Map<String, String> jobConfigs = commonJobsHelper.extractConfigs(batchJobConfigurationRepoList);
    if ((!jobConfigs.containsKey("enabled")) || jobConfigs.get("enabled").equalsIgnoreCase("true")) {
        JobLaunchRequest jobLaunchRequest = new JobLaunchRequest("RedemptionJob", jobConfigs);
        JobExecution jobExecution = jobRequestLauncher.launch(jobLaunchRequest);
        logger.info("jobExecution ExitStatus {}", jobExecution.getExitStatus());
    }
}
}

public class JobRequestLauncher {

private static final Logger logger = LoggerFactory.getLogger(JobRequestLauncher.class);

@Autowired
JobRegistry jobRegistry;

@Autowired
SimpleLauncher simpleLauncher;


public JobExecution launch(JobLaunchRequest request){
    JobExecution jobExecution = null;
    try {
        Job job = jobRegistry.getJob(request.getJobName());
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
        for (Map.Entry<String, String> entry : request.getJobParameters().entrySet()) {
            jobParametersBuilder.addString(entry.getKey(),entry.getValue());
        }
        logger.info(jobParametersBuilder.toString());
        jobParametersBuilder.addLong("jobStartTime",new Date().getTime());
        jobExecution = simpleLauncher.launchJob(job,jobParametersBuilder.toJobParameters());
    } catch (NoSuchJobException | JobInstanceAlreadyCompleteException | JobExecutionAlreadyRunningException | JobRestartException | JobParametersInvalidException e) {
        logger.error(e.getClass().getSimpleName() + " occured while launching job: {} with params: {}. {}",request.getJobName(),request.getJobParameters(),e.getMessage(),e);
    } catch(Exception e){
        logger.error(e.getClass().getSimpleName() + " occured while launching job: {} with params: {}. {}",request.getJobName(),request.getJobParameters(),e.getMessage(),e);
    }

    return jobExecution;
}
}


 @Component
 public class SimpleLauncher {

@Autowired
BatchRepoConfig batchRepoConfig;

@Retryable()
public JobExecution launchJob(Job job, JobParameters jobParameters) throws Exception {
    System.out.println(job.getName());
    return batchRepoConfig.getJobLauncher().run(job,jobParameters);
}
}

0 个答案:

没有答案