<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Enamul Haque</title>
    <description>The latest articles on DEV Community by Enamul Haque (@ehaque95).</description>
    <link>https://dev.to/ehaque95</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1258631%2F6734b83a-7df6-4efd-aca8-0eb2128b6dd0.png</url>
      <title>DEV Community: Enamul Haque</title>
      <link>https://dev.to/ehaque95</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ehaque95"/>
    <language>en</language>
    <item>
      <title>Spring batch job is already running not working</title>
      <dc:creator>Enamul Haque</dc:creator>
      <pubDate>Mon, 12 Feb 2024 04:37:50 +0000</pubDate>
      <link>https://dev.to/ehaque95/spring-batch-job-is-already-running-not-working-4j65</link>
      <guid>https://dev.to/ehaque95/spring-batch-job-is-already-running-not-working-4j65</guid>
      <description>&lt;p&gt;I am using spring batch 5. I need to stop running the batch again if it is already running. I have tried bellow like&lt;/p&gt;

&lt;p&gt;@Component&lt;br&gt;
    @RequiredArgsConstructor&lt;br&gt;
    @Slf4j&lt;br&gt;
    public class RtgsJob {&lt;br&gt;
      private final ProcessorStep1 batchProcessorStep1;&lt;br&gt;
      private final WriterStep1 batchWriterStep1;&lt;br&gt;
      private final PlatformTransactionManager transactionManager;&lt;br&gt;
      private final JobRepository jobRepository;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  public Job createEft(){

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


    }


    private Step step1(){

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


}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Job Schdule run the job:&lt;/p&gt;

&lt;p&gt;@Component&lt;br&gt;
    @Slf4j&lt;br&gt;
    public class ScheduledJobBean {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    @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 ");
        }



    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;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&lt;/p&gt;

&lt;p&gt;But above configuration batch start again within 30 second if one batch is already running.&lt;/p&gt;

&lt;p&gt;What is the wrong of my configuration?&lt;/p&gt;

&lt;p&gt;please help me&lt;/p&gt;

</description>
    </item>
    <item>
      <title>how to run run synchronously two batch in spring batch?</title>
      <dc:creator>Enamul Haque</dc:creator>
      <pubDate>Wed, 17 Jan 2024 05:33:57 +0000</pubDate>
      <link>https://dev.to/ehaque95/how-to-run-run-synchronously-two-batch-in-spring-batch-32j9</link>
      <guid>https://dev.to/ehaque95/how-to-run-run-synchronously-two-batch-in-spring-batch-32j9</guid>
      <description>&lt;p&gt;Hi sir,&lt;br&gt;
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. &lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Here is my batch configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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)
                    .&amp;lt;mydata, mydata&amp;gt; chunk(10, transactionManager)
                    .reader(myReader)
                    .processor(myProcessor)
                    .writer(myWritter)
                    .build();
        }

        private Step step2(){

            return new StepBuilder("step2", jobRepository)
                    .&amp;lt;mydata, mydata&amp;gt; chunk(10, transactionManager)
                    .reader(myReader)
                    .processor(myProcessor)
                    .writer(myWritter)
                    .build();
        }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is my schedule code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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: ;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How to solve the problem?&lt;/p&gt;

&lt;p&gt;Please help me..&lt;/p&gt;

</description>
      <category>springbatch</category>
      <category>springboot</category>
    </item>
  </channel>
</rss>
