Java Reference
In-Depth Information
convert them into JobParameter instances. To begin the conversion, you call the getNextJobParameters 1
method to increment any parameters that are required to be incremented. Because that process returns
a JobParameters instance, you then add those parameters to the JobParametersBuilder you're currently
working with. With the incremented parameters added, you add the Spring-passed parameters. In this
case, you know they're all String s, and you can simplify the code accordingly.
With SpringBatchQuartzJobLauncher written, you can move on to writing the tasklet that is the base
of this job. In this case, you have a simple tasklet (similar to the HelloWorld tasklet you wrote in Chapter
2) that deletes all files in a specified directory that haven't been modified in longer than a given period of
time. Listing 6-10 shows the code to accomplish this.
Listing 6-10. DeleteFilesTasklet
package com.apress.springbatch.chapter6;
import java.io.File;
import java.util.Date;
import java.util.Map;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class DeleteFilesTasklet implements Tasklet {
public RepeatStatus execute(StepContribution step, ChunkContext chunk)
throws Exception {
Map<String, Object> params =
chunk.getStepContext().getJobParameters();
String path = (String) params.get("path");
Long age = Long.valueOf((String) params.get("age"));
File tempDirectory = new File(path);
File[] files = tempDirectory.listFiles();
Date now = new Date();
long oldesttime = now.getTime() - age;
for (File file : files) {
if (file.lastModified() < oldesttime) {
file.delete();
}
}
1 The source code of the getNextJobParameters method comes from Spring Batch's
CommandLineJobRunner . Unfortunately, there is no standard way of starting a job that requires parameters
and incremented parameters (JobOperator calls an incrementer but doesn't accept parameters, and
JobLauncher takes parameters but doesn't call an incrementer).
 
Search WWH ::




Custom Search