diff --git a/spring-batch-samples/.springBeans b/spring-batch-samples/.springBeans index dd95a7479..602a737ec 100644 --- a/spring-batch-samples/.springBeans +++ b/spring-batch-samples/.springBeans @@ -1,11 +1,10 @@ 1 - + - src/main/resources/jobs/fixedLengthImportJob.xml src/main/resources/jobs/multilineJob.xml @@ -36,6 +35,7 @@ src/main/resources/jobs/skipSampleJob.xml src/main/resources/jobs/multilineOrderInputTokenizers.xml src/main/resources/jobs/multilineOrderOutputAggregators.xml + src/main/resources/jobs/taskletJob.xml @@ -282,5 +282,16 @@ src/main/resources/jobs/multilineOrderOutputAggregators.xml + + + true + false + + src/main/resources/data-source-context.xml + src/main/resources/data-source-context-init.xml + src/main/resources/jobs/taskletJob.xml + src/main/resources/simple-job-launcher-context.xml + + diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/tasklet/FileDeletingTasklet.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/tasklet/FileDeletingTasklet.java new file mode 100644 index 000000000..2affdd502 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/tasklet/FileDeletingTasklet.java @@ -0,0 +1,44 @@ +package org.springframework.batch.sample.tasklet; + +import java.io.File; + +import org.springframework.batch.core.UnexpectedJobExecutionException; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.ExitStatus; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.io.Resource; +import org.springframework.util.Assert; + +/** + * Deletes files in given directory. Ignores subdirectories. Fails (by throwing + * exception) if any of the files could not be deleted. + * + * @author Robert Kasanicky + */ +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"); + } + +} diff --git a/spring-batch-samples/src/main/resources/jobs/taskletJob.xml b/spring-batch-samples/src/main/resources/jobs/taskletJob.xml new file mode 100644 index 000000000..38c5c2414 --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/taskletJob.xml @@ -0,0 +1,38 @@ + + + + + Deletes files in given directory - TaskletStep is used as this + is the kind of task that is not natural to split into read and + write. In this case the step is wrapped in a standalone job, + however typically it would be a setup step in a multi-step job. + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskletJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskletJobFunctionalTests.java new file mode 100644 index 000000000..3c07a921a --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskletJobFunctionalTests.java @@ -0,0 +1,50 @@ +package org.springframework.batch.sample; + +import java.io.File; + +import org.springframework.core.io.Resource; +import org.springframework.util.Assert; + +/** + * Deletes files in the given directory. + * + * @author Robert Kasanicky + */ +public class TaskletJobFunctionalTests extends AbstractValidatingBatchLauncherTests { + + private Resource directory; + + /** + * Setter for auto-injection. + */ + public void setDirectory(Resource directory) { + this.directory = directory; + } + + /** + * Create the directory and some files in it. + */ + protected void onSetUp() throws Exception { + File dir = directory.getFile(); + dir.mkdirs(); + new File(dir, "file1").createNewFile(); + new File(dir, "file2").createNewFile(); + } + + /** + * We have directory with some files in it. + */ + protected void validatePreConditions() throws Exception { + Assert.state(directory.getFile().isDirectory()); + Assert.state(directory.getFile().listFiles().length > 0); + } + + /** + * Directory still exists but contains no files. + */ + protected void validatePostConditions() throws Exception { + Assert.state(directory.getFile().isDirectory()); + Assert.state(directory.getFile().listFiles().length == 0); + } + +} diff --git a/spring-batch-samples/src/test/resources/org/springframework/batch/sample/TaskletJobFunctionalTests-context.xml b/spring-batch-samples/src/test/resources/org/springframework/batch/sample/TaskletJobFunctionalTests-context.xml new file mode 100644 index 000000000..71285d6a8 --- /dev/null +++ b/spring-batch-samples/src/test/resources/org/springframework/batch/sample/TaskletJobFunctionalTests-context.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file