how to run run synchronously two batch in spring batch?
Tags: springbatch, springboot
Published: 2024-01-17 05:33:57

Hi sir,
I am Enamul working spring batch 5. In my case Spring batch running in spring schedule. But my deployment server has multiple instances. When it is running, it make double transaction in database. That means it occurs multiple transaction which occurs miss mass of my transaction. 

I have two batch. I need processes thousand of transaction using batch.I need to run both batches synchronous. One batch is started it won't get any request until complete. If both batch completed then start again.

Here is my batch configuration:

```
public class RtgsJob {

        public Job createEft(){

            String id = UUID.randomUUID().toString();
            id = "RTGS-STEP1-"+id;
            return new JobBuilder(id, jobRepository)
                    .incrementer(new RunIdIncrementer())
                    .start(step1())
                    .build();

        }


        public Job createEftTransaction(){
            String id = UUID.randomUUID().toString();
            id = "RTGS-STEP2-"+id;
            return new JobBuilder(id, jobRepository)
                    .incrementer(new RunIdIncrementer())
                    .flow(step2())
                    .end()
                    .build();



        }

        private Step step1(){

            return new StepBuilder("step1", jobRepository)
                    .<mydata, mydata> chunk(10, transactionManager)
                    .reader(myReader)
                    .processor(myProcessor)
                    .writer(myWritter)
                    .build();
        }

        private Step step2(){

            return new StepBuilder("step2", jobRepository)
                    .<mydata, mydata> chunk(10, transactionManager)
                    .reader(myReader)
                    .processor(myProcessor)
                    .writer(myWritter)
                    .build();
        }

}
```

Here is my schedule code: 

```
JobExecution execution = null;
    JobExecution executionTransaction = null;

    if(execution == null) {
        // rtgs batch step1 {} execution {} Started: 
        try {
            execution = jobLauncher.run(eftJob.createEft(), new JobParameters());

           // rtgs batch {} Step1 existStatus: "+execution.getExitStatus()

        } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException |
                 JobParametersInvalidException ex) {
           
            ex.printStackTrace();
        }catch (Exception ex) {
            ex.printStackTrace();
            
        }



        if(execution.getExitStatus().getExitCode().equals("COMPLETED")){

            if(executionTransaction == null){
                log.info(Constants.batchTag + "{} rtgs batch step2 {} executionTransaction {} Started: ");

                try {
                    executionTransaction =   jobLauncher.run(eftJob.createEftTransaction(),new JobParameters());

                    //{} rtgs batch step2 {} executionTransaction {} Completed;

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

                    ex.printStackTrace();
                }catch (Exception ex) {

                    ex.printStackTrace();
                    

                }
            }
        }


    }else{
        //{} rtgs batch step1 {} execution {} isRunning: ;
    }
```

How to solve the problem?

Please help me..