Reputation: 333
So like the title says, exactly how does an entire batch job work with Spring Batch? I understand (of course, please correct me if I'm wrong) that there's a repository that stores jobs, and then each of those jobs have steps and each step is either a tasklet or a combination of ItemReaders and ItemProcessors followed by an ItemWriter when a chunk has been filled.
What I don't understand is how exactly this all looks in actual code; I have been reading through the documentation and looking through examples, but I am beyond lost. How exactly do I even tell a job to run when/wherever I want it to? There are like a million classes that start with "Job", how do I use a minimal amount of them together to just store jobs I define and then run them when/wherever? Also, all the examples I see are either completely/mostly XML based, but I want to use as little XML as possible.
Another big issue of confusion for me, is how exactly are the ItemReader/Processor/Writers aware of what item/chunk of items they're working with? It's not too farfetched to imagine that I make my own implementation of an ItemProcessor and for that, I'd obviously need to access the item it is processing to do stuff with it, or for an ItemWriter, I'd like to access the chunk of items it wants to write.
Upvotes: 2
Views: 3982
Reputation: 17811
Mkyong.com has some nice spring batch tutorials which give some fairly clear examples of what job configuration actually looks like. See for a basic example here. Unfortunately, these examples are mostly in XML. Here's a blog post with an example using java config.
In terms of the item reader/processor/writer type of step. You generally will have a reader and at least one of processor/writer combination. Both are not required.
In terms of understanding each one, check out the interfaces - they might help clear it up for you. For example, here's the interface for an ItemWriter:
void write(java.util.List<? extends T> items)
http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/ItemWriter.html
Notice it takes a batch of items to write. Processors generally work item-by-item. Readers can be paged (by implementing the AbstractPagingItemReader for example) and will collect up items in the iterations before actually performing fetches (for example, paged queries to a database).
Upvotes: 2
Reputation: 5659
Answering your base question:-
1) Every job has an id, you will identify which job to execute by using this id.
2) Job consist of multiple steps in general, every step has an attribute called next. In next attribute you will define the id of step which you want to execute next.
3) Every step has a reader, processor and writer in case if your step is doing something other than these you can define it in a tasklet.
4) When defining the Reader and Writer you have to define the type of data it will fetch or write. for example, in BatchItemReader you define a RowMapper.
5) You can use JobExecutionDecider to decide after loading the job definition which step need to be executed. For remaining steps it can just use the next attribute.
Like in spring your application context just need to be aware of your job definition as it need to be aware of bean definition to load.
Which job to execute
: Id of job definition is used.
Which step to execute in Job
: For first step, JobExecutionDecider can be used else use the next attribute will be used with id of next step to be executed.
What is there in a step
: A reader to read the data, processor to do processing on the fetched data and finally writer to write the data. If step do something other than this define it in tasklet.
Upvotes: 1