How to stop Spring Batch job Executing automatically

154 Views Asked by At
@ComponentScan({
  "com."
})@Configuration@EnableBatchProcessing
public class InterestDateUpdationConfiguration {@Autowired
  private DataSource dataSource;

  @Autowired
  private JdbcTemplate jdbcTemplate;

  @Bean
  public Job runJob(Step step1, JobRepository jobRepository) {
    System.out.println("Runner");
    return new JobBuilder("job", jobRepository).incrementer(new RunIdIncrementer()).flow(step1).end().build();
  }

  @Bean
  public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager, JdbcBatchItemWriter < InterestDateUpdationEntity > writer) {
    System.out.println("Step");
    return new StepBuilder("step1", jobRepository). < InterestDateUpdationEntity,
    InterestDateUpdationEntity > chunk(10, transactionManager).reader(JdbcCursorItemReader(dataSource)).processor(processor()).writer(writer).build();
  }

  @Bean
  public ItemReader < InterestDateUpdationEntity > JdbcCursorItemReader(DataSource dataSource) {
    System.out.println("reader");
    JdbcCursorItemReader < InterestDateUpdationEntity > itemReader = new JdbcCursorItemReader < >();
    itemReader.setDataSource(dataSource);
    itemReader.setSql("SELECT SYSDATE FROM DUAL");
    itemReader.setRowMapper(new InterestDateUpdationRowMapper());
    return itemReader;
  }

  @Bean
  public JdbcBatchItemWriter < InterestDateUpdationEntity > jdbcBatchItemWriter() {
    JdbcBatchItemWriter < InterestDateUpdationEntity > itemWriter = new JdbcBatchItemWriter < >();
    itemWriter.setDataSource(dataSource);
    itemWriter.setSql("SELECT SYSDATE FROM DUAL");
    itemWriter.setItemPreparedStatementSetter(new ItemPreparedStatementSetter < InterestDateUpdationEntity > () {@Override
      public void setValues(InterestDateUpdationEntity item, PreparedStatement ps) throws SQLException {}
    });
    return itemWriter;
  }

  @Bean
  public JobExecutionListener listener() {
    return new JobExecutionListener() {@Override
      public void beforeJob(JobExecution jobExecution) {
        System.out.println("Started date and time" + new Date());
        System.out.println("Batch Status" + jobExecution.getStatus());
      }@Override
      public void afterJob(JobExecution jobExecution) {
        System.out.println("Ended date and time" + new Date());
        System.out.println("Batch Status" + jobExecution.getStatus());
      }
    };
  }

  @Bean
  public ItemProcessor < InterestDateUpdationEntity,
  InterestDateUpdationEntity > processor() {
    String insertSql = "INSERT INTO MT_INSTRUMENT_TEMP_VAIBHAV (select I.INSTR_CODE, I.FREQ_CODE, I.INSTR_FST_PYMT_DT, I.NEXT_PAY_DATE, I.LAST_PYMT_DATE, I.ISSUE_DATE,I.MATURE_DT, I.INSTR_INT_RATE,'' from MT_INSTRUMENT I where I.FREQ_CODE IS NOT NULL AND I.INSTR_FST_PYMT_DT IS NOT NULL AND I.MATURE_DT IS NOT NULL AND I.NEXT_PAY_DATE IS NOT NULL AND I.FREQ_CODE != 'X' AND I.NEXT_PAY_DATE <= (SELECT SYS_DATE FROM PRO_SYS_DATE))";
    String updateSql = "UPDATE MT_INSTRUMENT_TEMP_VAIBHAV SET NEXT_PAY_DATE_NEW = ADD_MONTHS(NEXT_PAY_DATE ,(CASE FREQ_CODE WHEN 'A' THEN 12 WHEN 'M' THEN 1 WHEN 'Q' THEN 3 WHEN 'S' THEN 6 ELSE 0   END ))";
    String updateByFirstDate = "update MT_INSTRUMENT_TEMP_VAIBHAV set NEXT_PAY_DATE_NEW = to_date(SUBSTR(INSTR_FST_PYMT_DT,0,2) ||SUBSTR(NEXT_PAY_DATE_NEW,3),'dd/mm/yyyy hh24:mi:ss') where to_number(SUBSTR(INSTR_FST_PYMT_DT,0,2)) < to_number(SUBSTR(NEXT_PAY_DATE_NEW,0,2))";
    String updateByInstrDetails = "UPDATE MT_INSTRUMENT_TEMP_VAIBHAV A SET A.NEXT_PAY_DATE_NEW = ( SELECT INTEREST_DATE FROM MT_INSTRUMENT_DETAILS B WHERE A.INSTR_CODE = B.INSTR_CODE ) WHERE EXISTS ( SELECT * FROM MT_INSTRUMENT_DETAILS B WHERE A.INSTR_CODE = B.INSTR_CODE)";
    String updateByMatureDate = "UPDATE MT_INSTRUMENT_TEMP_VAIBHAV SET NEXT_PAY_DATE_NEW = CASE WHEN NEXT_PAY_DATE_NEW > MATURE_DT THEN MATURE_DT ELSE NEXT_PAY_DATE_NEW END";
    String mergerSql = "MERGE INTO MT_INSTRUMENT_VAIBHAV_BACKUP A USING MT_INSTRUMENT_TEMP_VAIBHAV B ON (A.INSTR_CODE = B.INSTR_CODE) WHEN MATCHED THEN UPDATE SET A.LAST_INCOM_RT = B.INSTR_INT_RATE, A.NEXT_PAY_DATE = B.NEXT_PAY_DATE_NEW, A.OLD_LST_PAYDT = B.LAST_PYMT_DATE, A.LAST_PYMT_DATE = B.NEXT_PAY_DATE, A.MOD_LAST_PAYDT = 'Y' ";
    jdbcTemplate.update(insertSql);
    jdbcTemplate.update(updateSql);
    jdbcTemplate.update(updateByFirstDate);
    jdbcTemplate.update(updateByInstrDetails);
    jdbcTemplate.update(updateByMatureDate);
    jdbcTemplate.update(mergerSql);
    return null;
  }
}`

package com;
import org.springframework.batch.core.Job;@RestController@RequestMapping("/api/job")
public class InterestDateUpdationController {

  @Autowired
  private JobLauncher jobLauncher;

  @Autowired
  private Job runJob;

  @PostMapping("/run-job")
  public ResponseEntity < String > runJob() {
    try {
      JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();

      jobLauncher.run(runJob, jobParameters);
      return ResponseEntity.ok("Job executed successfully");
    } catch(Exception e) {
      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to execute job");
    }
  }

}

My Batch job is triggering automatically even if I have set spring.batch.job.enabled=false. Also, I am not using any Job Runner but at the time of running application it's saving data into the database by executing Reader, writer and processor.

I have tried to set spring.batch.job.enabled=false, and I have commented all Runner related code including CommandLineRunner.

1

There are 1 best solutions below

0
vaibhav burgute On

@MahmoudBenHassine Thank you for your comment, Actually we can use @StepScope over Processor, because all queries declared in processor and @Bean responsible for configuring it and running my job. That helped me.