Spring Batch Read From File And Write To Database From Form

I'm new to Spring Batch and trying to implement a batch job where I

  1. Read from a MySQL database
  2. Write the results to a CSV file
  3. Do some processing of MySQL result set and write to another database.

I've looked through this question on StackOverflow, but the main accepted answer was essentially to implement two steps that read twice from the database:

Isn't there a more efficient way of doing this than reading twice from the MySQL database? For example, what if you query is quite large and drags system performance?

Community
Yu ChenYu Chen

2 Answers

What you need is a chunk strategy instead of tasklet. The ItemReader will read chunks from your database, the processor will process you data and then you can for each item send them to the ItemWriter that can write to database and file. This is one of many possible strategies, I don't know the details on your business logic, but I think this is information enough to get you going on your own ideas.

This is the JSR-352 XML type, for Spring you have corresponding approach.

Watch Pirates II: Stagnetti's Revenge on 123movies: Pirate hunter Captain Edward Reynolds and his blond first mate, Jules Steel, return where they are recruited. Pirates 2 stagnettis revenge tube.

Tim MickelsonTim Mickelson

I'll go ahead and answer my own question. There's multiple ways to do this, but I found that saving properties and objects first to the StepExecutionContext, then promoting them to the JobExecutionContext after the Step completes works well. It is also documented pretty thoroughly here. Photoshop editor app download.

Step 1:

In your Writer / Reader declare a private StepExecution. Then, inside your read/write method create the step context, and put your data in as a key/value pair:

Step 2: Add an ExecutionContextPromotionListener to your step's bean configuration. The ExecutionContextPromotionListener must include a String[] property called Keys that includes the keys you wish to promote to Job scope beyond your step, similar to this implementation from a LinkedIn article:

Step 3: You also need to add the StepExecution into your Writer before your step executes:

Step 4: This will give your write() method access to the stepExecution instance, where it can access stepContext for you to save your data. For instance, you can write

Finally, in your next step, you can retrieve this data (example coming directly from the Spring Batch documentation:

Zac brown band discography pirate bay. This time, however, notice that it's being accessed from the jobContext as opposed to the stepContext- it's been promoted!

Yu ChenYu Chen

Not the answer you're looking for? Browse other questions tagged springspring-batch or ask your own question.

Posted on