Java Reference
In-Depth Information
return RepeatStatus.FINISHED;
}
}
The code for
DeleteFilesTasklet
in Listing 6-10 shouldn't come as any surprise. In implementing
the
Tasklet
interface, you implement the
execute
method to do all your work. For
DeleteFilesTasklet
's
work, you need to know where to delete the files from and how long they have been idle. When you have
that information, you can proceed with deleting the files.
The first three lines of the
execute
method retrieve the job parameters so you can get the path to the
directory from which you're to delete the files (
path
) and the time in milliseconds that a file hasn't been
modified (
age
). When you have the job parameters, you can open a directory and delete all files or
directories that meet your requirements. With processing complete, you return
RepeatStatus.FINISHED
to tell Spring Batch that the step has completed.
All you have left to do to make this happen is the configuration. Again, you're using the
JobRegistryBackgroundJobRunner
, so the configuration files are separate:
launch-context.xml
is located
in the
<project_home>/src/main/resources
directory, and
deleteFilesJob.xml
is in
<project_home>/src/main/resources/jobs
. Looking at the
deleteFilesJob.xml
file first, in Listing 6-11,
shows you the configuration for the job itself.
Listing 6-11.
deleteFilesJob.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="
http://www.springframework.org/schema/batch"
xmlns:beans="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
<beans:bean id="deleteFilesTasklet"
class="com.apress.springbatch.chapter6.DeleteFilesTasklet" />
<step id="deleteFilesStep">
<tasklet ref="deleteFilesTasklet" />
</step>
<beans:bean id="idIncrementer"
class="org.springframework.batch.core.launch.support.RunIdIncrementer"/>
<job id="deleteFilesJob" incrementer="idIncrementer">
<step id="step1" parent="deleteFilesStep" />
</job>
</beans:beans>
As with your other jobs, you define the tasklet itself, then have a step that uses the tasklet, and then
finish with the definition of the job, a single step that deletes the files when the job runs. The only
additional piece of configuration this job receives is the addition of
RunIdIncrementer
, as discussed in
Chapter 4, so you can run this job via Quartz multiple times without having to change the job
parameters.