Java Reference
In-Depth Information
Launching From a Web Application
Launching a job from a web application requires a slightly different tact because the client thread
(presumably an HTTP request) can't usually wait for a batch job to finish. The ideal solution is to have
the job execute asynchronously when launched from a controller or action in the web tier, unattended
by the client thread. Spring Batch supports this scenario through the use of a Spring TaskExecutor . This
requires a simple change to the configuration for the JobLauncher , although the Java code can stay
the same.
<beans:bean id="jobLauncher" class=" org.springframework.
batch.execution.launch.SimpleJobLauncher"
p:jobRepository-ref="jobRepository" >
<beans:property name="taskExecutor">
<beans:bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
</beans:property>
</beans:bean>
Running from the Command Line
Another common use case is deployment of a batch process from a system scheduler such as cron or
autosys , or even Window's event scheduler. Spring Batch provides a convenience class that takes as its
parameters the name of the XML application context (that contains everything required to run a job ) as
well as the name of the job bean itself. Additional parameters may be provided and used to parameterize
the job . These parameters must be in the form name=value . An example invocation of this class on the
command line (on a Linux/Unix system), assuming that you set up the classpath, might look like this:
java CommandLineJobRunner jobs.xml hourlyReport date
=`date +%m/%d/%Y` time=`date +%H`
The CommandLineJobRunner will even return system error codes (0 for success, 1 for failure, and 2 for an
issue with loading the batch job) so that a shell (such as used by most system schedulers) can react
or do something about the failure. More complicated return codes can be returned by creating and declaring
a top-level bean that implements the interface ExitCodeMapper , in which you can specify a more useful
translation of exit status messages to integer-based error codes that the shell will see on process exit.
9-4. Reading and Writing (but No Arithmetic)
Problem
You want to insert data from a file into a database. This solution will be one of the simplest solutions and
will give you a chance to explore the moving pieces of a typical solution.
Solution
You'll build a solution that does a minimal amount of work, while being a viable application of the
technology. The solution will read in a file of arbitrary length and write out the data into a database. The
end result will be almost 100 percent code free. You will rely on an existing model class and write one
class (a class containing the public static void main(String [] args() method) to round out the
example. There's no reason why the model class couldn't be a Hibernate class or something from your
DAO layer, though in this case it's a brainless POJO.
 
Search WWH ::




Custom Search