This post is part of a series delving into the details of the JSR-352 (Java Batch) specification. Each post examines a very specific part of the specification and looks at how it works and how you might use it in a real batch application.
To start at the beginning, follow the link to the first post.
The next post in the series is here.
Let’s talk about something of substance, but we’ll start off easy. Every exploration of a new technology usually somehow involves some bit of code that prints “hello, world!”. Writing a batchlet is the way to do that.
What is a batchlet? It is the simplest programming artifact available to a JSR-352 batch application. When invoked as part of a job, a batchlet gets control, does whatever it does, and completes. The principle method you implement to have a batchlet is the process method. It gets control with no parameters and returns a String. That String will be used as the exit status for the step (we’ll get into exit status values in a later post).
What can you do with a batchlet? Pretty much anything you want. The process method is just Java. Often a batchlet is used for file manipulation. Maybe you need to copy a file (or a bunch of files) from one place to another. Or you need to sort the contents of a file using a sort utility. In fact a batchlet is a good place to invoke a utility. The process method is really just a wrapper around a call to some other thing.
Batchlets get used a lot to manage flow control within the job. We’ll talk about flow with a job later, but at this point just recognize that there will be points in the execution of a job where you need to make decisions. Does a file exist already or not? Is there already a row in the account table for this account number? Write code here that will go look at whatever it is you need to know and return an appropriate exit status that the job flow can use to go to the appropriate next step.
A batchlet can be used for pretty much anything you want. Just remember that there is no transaction wrapped around the processing, so if your batchlet does multiple things that need to be committed together, you need to create and manage your own transaction. And if you find you’re doing the same thing in a loop inside your transaction…you should be using a chunk step. Generally speaking, if it isn’t obviously chunk processing, use a batchlet.
See you next time!
<<Previous article in this series
>>Next article in this series