how to run run synchronously two batch in spring batch?

ehaque95

Enamul Haque

Posted on January 17, 2024

how to run run synchronously two batch in spring batch?

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();
        }

}
Enter fullscreen mode Exit fullscreen mode

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: ;
    }
Enter fullscreen mode Exit fullscreen mode

How to solve the problem?

Please help me..

💖 💪 🙅 🚩
ehaque95
Enamul Haque

Posted on January 17, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Chunk vs Tasklet, which one should I use?
springbatch Chunk vs Tasklet, which one should I use?

February 7, 2024

Best practice in SpringBatch
springbatch Best practice in SpringBatch

October 8, 2021