Skip to content
Advertisement

Spring Batch – My Batch seems executing two steps at the same time?

I can’t really understand what’s going on. I’m studying Spring Batch and I’d like to execute two steps for some reasons, one after the other.

Now please don’t mind what the steps are currently doing, just keep in mind that I would like to perform two steps sequentially.

This is the code:

JavaScript

Why doesn’t the insertToDBStep starts at the end of the filterStep and it actually looks like the filter is running at the same time? And why it looks like the job starts after the initialization of Root WebApplicationContext?

This is the output.

JavaScript

Thanks in advance.

Advertisement

Answer

The steps are executed correctly in sequence. You are putting System.out.println statements in two “kind” of places:

  • In the bean definition methods executed by Spring Framework when configuring the application context
  • In the code of batch artefacts (item processor, item writer) which are called by Spring Batch when running your job

In your case, Spring Framework will call the following bean definition methods in order to define the first step, filterStep():

  • jsonReader(): prints Try to read JSON. The file is not read at this time, only the json reader bean is defined. A more accurate log message would be: json reader bean created.
  • listReader(): prints Read from list. Same here, the file reading has not started yet. A more accurate log message would be: list reader bean created.
  • filterProcessor(): prints nothing. The log statement is in the ItemProcessor#process method. This will be called by Spring Batch at runtime, not at this point in time which is a configuration time
  • filterWriter(): same here, the print statement is in the write method called at runtime and not at configuration time

This results in the following output for filterStep():

JavaScript

Now Spring Framework moves to defining the next step, insertToDBStep(). For this, it will call the following methods in order, according to your step definition:

  • listReader(): this bean has already bean defined, Spring will reuse the same instance (by default, Spring beans are singletons). Hence, there is no output from this method.
  • insertToDBWriter(): prints Try to save on DB. Same here, there is no actual save to DB here. A more accurate log message would be insertToDBWriter bean created (or even more accurate, attempting to create insertToDBWriter bean, in case the code that follows throws an exception).

You now have the following cumulative output:

JavaScript

At this point, Spring Framework has finished its job of configuring the application context, Spring Batch takes over and starts the job. The actual processing of filterStep() begins:

  • The reader (ListItemReader) does not have any output in the read method.
  • The processor prints Processing JSON
  • The writer prints Save on list ...

You seem to have two chunks (the first with 5 items and the second with 2 items), which leads to the following output:

JavaScript

Then, the next step starts its execution and you get the following output:

JavaScript

Here you might be asking why there are no items written by insertToDBWriter() (ie why there are no Save on DB .. logs). This is because the listReader() is a singleton bean and you are using it in both steps, so when the second step calls its read method, it will still return null, because the same instance is used and which has already exhausted the list of items in step 1. Hence, this step ends immediately since there are no items to process. If you want to re-read the items from the list in the second step, you can annotate the reader method with @StepScope. This will create a distinct instance of the reader for each step.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement