From 447997a5bcb8295b1e2059a4938757776b1c91d9 Mon Sep 17 00:00:00 2001 From: lucasward Date: Tue, 22 Apr 2008 02:43:05 +0000 Subject: [PATCH] BATCH-599:Added additional content to the section on TaskletStep --- docs/src/site/docbook/reference/execution.xml | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/docs/src/site/docbook/reference/execution.xml b/docs/src/site/docbook/reference/execution.xml index b2eee87ff..ed4525364 100644 --- a/docs/src/site/docbook/reference/execution.xml +++ b/docs/src/site/docbook/reference/execution.xml @@ -1304,6 +1304,94 @@ <property name="tasklet" ref="tasklet" /> <property name="jobRepository" ref="repository" /> </bean> + +
+ TaskletAdapter + + As with other adapters for the ItemWriter + and ItemReader interfaces, the + Tasklet interface contains an implementation + that allows for adapting itself to any pre-existing class: + TaskletAdapter. An example where this may be + useful is an existing DAO that is used to upate a flag on a set of + records. The TaskletAdapter can be used to call + this class without having to write an adapter for the + Tasklet interface: + + <bean id="deleteFilesInDir" parent="taskletStep"> + <property name="tasklet"> + <bean class="org.springframework.batch.core.step.tasklet.TaskletAdapter"> + <property name="targetObject"> + <bean class="org.mycompany.FooDao"> + </property> + <property name="targetMethod" value-"updateFoo" /> + </bean> + </property> + </bean> +
+ +
+ Example Tasklet implementation + + Many batch jobs contains steps that must be done before the main + processing begins in order to set up various resources, or after + processing has completed to cleanup those resources. In the case of a + job that works heavily with files, it is often necessary to delete + certain files locally after they have been uploaded successfully to + another location. The example below taken from the Spring Batch + samples project, is a Tasklet implementation + with just such a responsibility: + + public class FileDeletingTasklet implements Tasklet, InitializingBean { + + private Resource directory; + + public ExitStatus execute() throws Exception { + File dir = directory.getFile(); + Assert.state(dir.isDirectory()); + + File[] files = dir.listFiles(); + for (int i = 0; i < files.length; i++) { + boolean deleted = files[i].delete(); + if (!deleted) { + throw new UnexpectedJobExecutionException("Could not delete file " + files[i].getPath()); + } + } + return ExitStatus.FINISHED; + } + + public void setDirectoryResource(Resource directory) { + this.directory = directory; + } + + public void afterPropertiesSet() throws Exception { + Assert.notNull(directory, "directory must be set"); + } + } + + The above Tasklet implementation will + delete all files within a given directory. It should be noted that the + execute method will only be called once. All + that is left is to inject the Tasklet into a + TaskletStep: + + <bean id="taskletJob" parent="simpleJob"> + <property name="steps"> + <bean id="deleteFilesInDir" parent="taskletStep"> + <property name="tasklet"> + <bean class="org.springframework.batch.sample.tasklet.FileDeletingTasklet"> + <property name="directoryResource" ref="directory" /> + </bean> + </property> + </bean> + </property> + </bean> + + <bean id="directory" + class="org.springframework.core.io.FileSystemResource"> + <constructor-arg value="target/test-outputs/test-dir" /> + </bean> +