RESOLVED - issue BATCH-473: Create Tasklet sample job.

http://jira.springframework.org/browse/BATCH-473

added tasklet job that deletes files in given directory (description clarifies this would typically be a preliminary step in multi-step job)
This commit is contained in:
robokaso
2008-03-31 12:22:35 +00:00
parent 4c4b44042d
commit 8dc857bfd8
5 changed files with 155 additions and 2 deletions

View File

@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.0.3.v200802061800]]></pluginVersion>
<pluginVersion><![CDATA[2.0.2.v200712142013]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
<config>src/main/resources/jobs/fixedLengthImportJob.xml</config>
<config>src/main/resources/jobs/multilineJob.xml</config>
@@ -36,6 +35,7 @@
<config>src/main/resources/jobs/skipSampleJob.xml</config>
<config>src/main/resources/jobs/multilineOrderInputTokenizers.xml</config>
<config>src/main/resources/jobs/multilineOrderOutputAggregators.xml</config>
<config>src/main/resources/jobs/taskletJob.xml</config>
</configs>
<configSets>
<configSet>
@@ -282,5 +282,16 @@
<config>src/main/resources/jobs/multilineOrderOutputAggregators.xml</config>
</configs>
</configSet>
<configSet>
<name><![CDATA[tasklet]]></name>
<allowBeanDefinitionOverriding>true</allowBeanDefinitionOverriding>
<incomplete>false</incomplete>
<configs>
<config>src/main/resources/data-source-context.xml</config>
<config>src/main/resources/data-source-context-init.xml</config>
<config>src/main/resources/jobs/taskletJob.xml</config>
<config>src/main/resources/simple-job-launcher-context.xml</config>
</configs>
</configSet>
</configSets>
</beansProjectDescription>

View File

@@ -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");
}
}

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<description>
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.
</description>
<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>
</beans>

View File

@@ -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);
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="classpath:/simple-job-launcher-context.xml" />
<import resource="classpath:/jobs/taskletJob.xml" />
</beans>