Dec 15, 2013

Spring batch working with the listeners

Q. Why do you have listeners in Spring batch?A. Reader --> to read data and Writer --> to write chunked data and processor --> to filter out records before they are passed to the ItemWriter. Filtering is an action distinct from skipping; skipping indicates that a record is invalid whereas filtering simply indicates that a record should not be written.

Listeners are events that can be executed before or after a point in time like before or after executing a step, etc.

Here is an example that uses all the listeners except for the ItemProcessListener.

Q. What do you understand by the terms StepExecution, JobExecution, JobExecutionContext, and StepExecutionContext?A. You can store data between readers and writers using StepExecutionor JobExecution. For example, you may want to keep track of the failed accounts by storing them in a job control table. These accounts can be read again from the job control table and rerun in the next job run.

4 Comments:

Anonymous said...

When an exception is thrown in listener. Does it stops the entire process? For Example I have a custom listener that implements ItemProcessListener and in the beforeProcess method there is an IOException. But this exception is being swallowed not sure how because I checked the source code they catch the exception and rethrow. Even the exception is thrown the Process method on the item processor is called. Can you please help me on this >>??

Job and Step are not supposed to throw exceptions even if they fail. Your program should be able to retrieve the exceptions from a local execution via the JobExecution or StepExecution objects. Then it can re-throw.