Java Reference
In-Depth Information
For example, suppose a batch process that processes a million transactions a night goes down after
processing 900,000 of those records. Even with periodic commits along the way, how do you know where
to pick back up when you restart? The idea of reestablishing that execution state can be daunting, which
is why Spring Batch handles it for you.
You read earlier about how a JobExecution represents an actual attempt at executing the job. It's
this level of the domain that requires state to be maintained. As a JobExecution progresses through a job
or step, the state changes. This state is maintained in ExecutionContext .
If you think about how web applications store state, typically it's through the HttpSession . 6
ExecutionContext is essentially the session for your batch job. Holding nothing more than simple key-
value pairs, ExecutionContext provides a way to store state within your job in a safe way. One difference
between a web application's session and ExecutionContext is that you actually have multiple
ExecutionContext s over the course of your job. JobExecution has an ExecutionContext , as does each
StepExecution (which you'll see later in this chapter). This allows data to be scoped at the appropriate
level (either data-specific for the step or global data for the entire job). Figure 4-4 shows how these
elements are related.
Figure 4-4. The relationship between ExecutionContext s
ExecutionContext provides a “safe” way to store data. The storage is safe because everything that
goes into an ExecutionContext is persisted in the job repository. You briefly looked at the
batch_job_execution_context and batch_step_execution_context tables in Chapter 2, but they didn't
contain any meaningful data at the time. Let's look at how to add data to and retrieve data from the
ExecutionContext and what it looks like in the database when you do.
Manipulating the ExecutionContext
The ExecutionContext is part of the JobExecution or StepExecution as mentioned earlier. Because of this,
to get a handle on the ExecutionContext , you obtain it from the JobExecution or StepExecution based on
which you want to use. Listing 4-21 shows how to get a handle on ExecutionContext in the HelloWorld
tasklet and add to the context the name of the person you're saying hello to.
6 This chapter ignores web frameworks that maintain state in some form of client form (cookies, thick
client, and so on).
Search WWH ::

Custom Search