Spring Batch中的MultiResourceParitioner接受来自多个文件夹的文件

时间:2011-10-31 12:14:59

标签: spring-batch

我在 dataloader.properties

文件中有以下配置
filepath = /xx
exchange = M00,M01,MF2,MF3

我需要MultiResourcePartitioner来处理所有这些文件夹中的文件,例如

/xx/M00/*
/xx/M01/*
/xx/MF2/*
/xx/MF3/*

/xx下可以有其他文件夹,但它只应处理文件夹M00,M01,MF2,MF3

中的文件
<bean id="filepartitioner" class="org.springframework.batch.core.partition.support.MultiResourcePartitioner">
    <property name="resources" value="classpath:./${filepath}" />
</bean>

请告诉我,如何在春季批次中执行此操作。我查看了filesystemresourceResourcesItemReader api,但不知道如何将其注入MultiResourceParitioner < / p>

1 个答案:

答案 0 :(得分:1)

简单的解决方案 - 如果可能的话 如果模式保持稳定,你可以尝试

<bean id="filepartitioner" class="org.springframework.batch.core.partition.support.MultiResourcePartitioner">
    <property name="resources" value="classpath:./${filepath}/M*/*" />
</bean>

自定义MultiResourcePartitioner - 过滤文件夹

public class CustomM... extends MultiResourcePartitioner {
    // constructor with filePath and exchange argument
    // convert exchange argument to list of folder patterns,
    // or let it convert by spring magic
    // use one of the "list all files.." methods from below
    // call setResources(...)
}

<bean id="filepartitioner" class="org.springframework.batch.core.partition.support.CustomMultiResourcePartitioner">
    <constructor-arg type="java.lang.String" value="${filePath}" />
    <!-- spring can convert comma separated values to array and list
         just look into the spring documentation -->
    <constructor-arg type="java.lang.String" value="${exchange}" />
</bean>

my github repo https://github.com/langmi/spring-batch-examples-playground/blob/4e733dce09daffca1c10d4907f410ac5bead6887/src/main/resources/spring/batch/job/file-multiresourcepartitioner-filter-folders-factory-job.xml上提供了一个工作示例,请检查 file-multiresourcepartitioner-filter-folders-job.xml

更可插拔的解决方案:为MultiResourcePartitioner创建资源的工厂

public class FilterFactory {
  public static Resource[] getInstance(final String filePath, final List<String> acceptedFolders) {
    final List<FileSystemResource> files = new ArrayList<FileSystemResource>();
    yourListAllFilesButFilterFoldersMethod(files, filePath, acceptedFolders)
    return files.toArray(new Resource[0]);
  }
}

<bean id="resources" class="de.langmi.spring.batch.examples.playground.resource.FiltersFoldersResourceFactory" factory-method="getInstance">
    <constructor-arg name="filePath" type="java.lang.String" value="${filePath}" />
    <constructor-arg name="acceptedFolders" type="java.util.List" value="${acceptedFolders}" />
</bean>

my github repo https://github.com/langmi/spring-batch-examples/tree/master/playground/src/main/resources/spring/batch/job上提供了一个工作示例,请检查 file-multiresourcepartitioner-filter-folders-factory-job.xml

你可以选择从List all files from a directory recursively with Java设置资源数组的方法和网上的类似解决方案,只留下过滤器,提供的链接中有一个解决方案