Java Reference
In-Depth Information
launch a job from a command line than if you launch your job from a Quartz scheduler. Because you've
been using CommandLineJobRunner up to now, let's start there.
Passing parameters to CommandLineJobRunner is as simple as passing key=value pairs on the
command line. Listing 4-6 shows how to pass parameters to a job using the way you've been calling jobs
up to this point.
Listing 4-6. Passing Parameters to the CommandLineJobRunner
java -jar sample-application-0.0.1-SNAPSHOT.jar jobs/sampleJob.xml sampleJob name=Michael
In Listing 4-6, you pass one parameter, name . When you pass parameter into your batch job, your job
runner creates an instance of JobParameters , which serves as a container for all the parameters the job
received.
JobParameters isn't much more than a wrapper for a java.util.Map<String, JobParameter> object.
Notice that although you're passing in String s in this example, the value of the Map is an
org.springframework.batch.core. JobParameter instance. The reason for this is type. Spring Batch
provides for type conversion of parameters, and with that, type-specific accessors on the JobParameter
class. If you specify the type of parameter to be a long , it's available as a java.lang.Long . String , Double ,
and java.util.Date are all available out of the box for conversion. In order to utilize the conversions,
you tell Spring Batch the parameter type in parentheses after the parameter name, as shown in Listing 4-
7. Notice that Spring Batch requires that the name of each be all lowercase.
Listing 4-7. Specifying the Type of a Parameter
java -jar sample-application-0.0.1-SNAPSHOT.jar jobs/sampleJob.xml sampleJob
param1(string)=Spring param2(long)=33
To view what parameters have been passed into your job, you can look in the job repository.
Chapter 2 noted that there is a table for job parameters called batch_job_params , but because you didn't
pass any parameters to your job, it was empty. If you explore the table after executing the examples in
Listings 4-6 and 4-7, you should see what is shown in Table 4-1.
 
Search WWH ::




Custom Search