Spring batch job is already running not working
Published: 2024-02-12 04:37:50



I am using spring batch 5. I need to stop running the batch again if it is already running. I have tried bellow like

@Component
    @RequiredArgsConstructor
    @Slf4j
    public class RtgsJob {
      private final ProcessorStep1 batchProcessorStep1;
      private final WriterStep1 batchWriterStep1;
      private final PlatformTransactionManager transactionManager;
      private final JobRepository jobRepository;

      public Job createEft(){

            return new JobBuilder("RTGS-STEP1", jobRepository)
                    .incrementer(new RunIdIncrementer())
                    .start(step1())
                    .build();


        }


        private Step step1(){

            return new StepBuilder("step1", jobRepository)
                    .<input, output> chunk(10, transactionManager)
                    .reader(new ReaderStep1())
                    .processor(batchProcessorStep1)
                    .writer(batchWriterStep1)
                    .build();
        }


    }


Job Schdule run the job:

@Component
    @Slf4j
    public class ScheduledJobBean {

        @Autowired
        JobLauncher jobLauncher;

        @Autowired
        RtgsJob eftJob;

        @Autowired
        private  JobRepository jobRepository;

         @Scheduled(cron = "*/30* * * * *")
        public void perform() throws Exception
        {
            JobExecution lastJobExecutionStep1 = jobRepository.getLastJobExecution("RTGS-STEP1", new JobParametersBuilder().toJobParameters());

            if (lastJobExecutionStep1 == null || lastJobExecutionStep1.getStatus().isLessThan(BatchStatus.STARTING)) {

                log.info("{} batch step1 {} executionTransaction {} Started ");

                try {

                    JobParameters jobParameters2 = new JobParametersBuilder()
                            .addLong("startAtRTGSStep1", System.currentTimeMillis())
                            .toJobParameters();
                    executionTransaction =   jobLauncher.run(eftJob.createEft(),jobParameters2);



                } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException |
                         JobParametersInvalidException ex) {

                    log.error("{} rtgs batch {} Step1 {} executionTransaction {} JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException | JobParametersInvalidException message: "+ex.getMessage());
                    ex.printStackTrace();


                }catch (Exception ex) {

                    log.error("{} rtgs batch {} Step1 executionTransaction {} error: "+ex.getMessage());
                    ex.printStackTrace();
                    log.error(ex.getMessage());


                }
            }else{
                log.info("{} rtgs batch step1 {} execution {} isRunning ");
            }



        }
    }


My job started after 30 second. if one job is running it won't start again until it is completed. That means if data process need more than 30 second it won't start again. After completed pressing it will start within 30 second again

But above configuration batch start again within 30 second if one batch is already running.

What is the wrong of my configuration?

please help me