0

I am trying to build a job in spring batch to perform below actions :

  • Read CSV file
  • Dump CSV files into a table
  • Call Stored Procedure to perform some action

I am able to write logic for the first 2 points but not sure how to call a Stored procedure? Also, this procedure should be called after first 2 points are completed, so thinking this should be in another step.

Based on my assumption, I should have a job configuration like this :

@Bean
    public Job job(){
        return jobBuilderFactory.get("job")
                .start(step1())  --> To read and write CSV file
                .next(step2())   --> To call SP
                .incrementer(new RunIdIncrementer())
                .build();
    }

Below is my step configuration :

In this step, I am reading 1000 Provider objects at a time from csv and dumping it into a table. This is working as expected.

@Bean
    public Step step1(){
        return stepBuilderFactory.get("step1")
                .<Provider, Provider>chunk(1000)
                .reader(batchItemReader.providerMultiResourceItemReader())
                .writer(batchItemWriter.providerJdbcBatchItemWriter())
                .build();
    }

To call Stored procedure : SP_UpdateTemp, I am thinking to include step2() but now sure how to do it ? In this SP, I am reading records from first table and performing some updates on another table.

@Bean
    public Step step2(){
        return stepBuilderFactory.get("step2")
                //call procedure

    }
Praveenks
  • 1,436
  • 9
  • 40
  • 79

1 Answers1

2

Your step2 could be a simple tasklet step that calls the stored procedure with a JdbcTemplate for example:

@Bean
public Step step2() {
    return stepBuilderFactory.get("step2")
            .tasklet((contribution, chunkContext) -> {
                // call stored procedure
                jdbcTemplate.update("call SOME_PROC (?, ?)", param1, param2);
                return RepeatStatus.FINISHED;
            })
            .build();
}

Refer to Spring JDBC Template for calling Stored Procedures for more details about calling stored procedures with a JdbcTemplate.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50