DEV Community

Laurent Dumas
Laurent Dumas

Posted on • Edited on

Best practice in SpringBatch

Hi there,

I'm looking for best practice in SpringBatch like: things which are mandatory, itemReader/writer declare in the same class as job, or in a separate one..
Is there any resource on the subject? I think i may try to write some of these; I discover the subject so I need it, are people would be interested?

It seems that there's not much of it online, i'm suprised.

Edit: After reading here and there and trying myself on a sample project. I would start with these:

  • Just One Job in your batch
  • Log jobParameters values, startTime via (JobLoggerListener)
  • Log at the end of Job (JobLoggerListener): endTime, Status of Job, ExitStatus.exitCode, and description (maybe the returnCode for the system).
  • Implements a ChunkListener to see in your log the progressing of your treatment.
  • Organize your listeners in differents package so you can specify appender log for example just jobListener without log all the other details. So in Production, you have a nice log with just minimum information.
  • Implements an ExitCodeGenerator to return to your system a meaning code for your ops team.
  • Don't use Hibernate if not necessary, prefer JdbcTemplate.
  • Use a HSQLDB database for unit testing.

For books, I read the "The Definitive Guide to Spring Batch" from Michael Minella which is great to learn the framework.

Top comments (8)

Collapse
 
scottshipp profile image
scottshipp

I’ve been looking for the same, and also a way to pass job params to the job when run with maven

Collapse
 
laurentld79 profile image
Laurent Dumas

what maven command are you running?

Collapse
 
scottshipp profile image
scottshipp

I think I have found it. Addin a run.arguments property to the command seems to do the trick:

mvn spring-boot:run -Drun.arguments="key1=value1, key2=value2"
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
laurentld79 profile image
Laurent Dumas

I have edited the post with some best practice I think are good. If you have time, tell me about it.
I have intention to make a sample project on github, but.. just need time to do it outside of daily work :p

Thread Thread
 
scottshipp profile image
scottshipp

By the way, I found that Baeldung has a couple recent articles about it and they include a companion repository in GitHub with a sample application. Here is one of them.

Thread Thread
 
laurentld79 profile image
Laurent Dumas

yep, thanks!

Collapse
 
__nikolamicic21 profile image
Nikola Micić • Edited

I would start first with Spring Batch Samples (github.com/spring-projects/spring-...).
After that, I would consult Spring Batch Reference Documentation (docs.spring.io/spring-batch/docs/c...).
There is also official SpringDeveloper YT channel where videos about Spring Batch could be found (youtube.com/user/SpringSourceDev/v...). Hope it helps!

Collapse
 
laurentld79 profile image
Laurent Dumas

Hi,
Thanks for sharing these resources. It sure helps. I am glad that you take time to answer my post. In fact, I'm also looking for organizing source kind of stuff. Like in a video, spring tells it is more convenient to create a static class like "StepConfiguration" which has reader/writer declaration, it is more readable for your job configuration class.
And too often, code in video are made for a presentation purpose, so a lot of code is in the same class.
I am new in this subject so I try things maybe I will make a post about this to see what people think (like I hate xml configuration, I prefer the annotation way). Maybe some are really obvious for spring expert but I prefer keep an objective eye on it.