MultiResourceParitioner в Spring Batch для приема файлов из нескольких папок - PullRequest
0 голосов
/ 31 октября 2011

У меня есть следующая конфигурация в файле 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>

, пожалуйста, дайте мне знать, как это сделать в весеннем пакете. Я посмотрелв filesystemresource и ResourcesItemReader API, но не знаю, как ввести его в MultiResourceParitioner

1 Ответ

1 голос
/ 01 ноября 2011

простое решение - если вообще возможно если шаблон остается стабильным, вы можете попробовать его с

<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>

рабочий пример доступен на моем репозитории github на https://github.com/langmi/spring-batch-examples-playground/blob/4e733dce09daffca1c10d4907f410ac5bead6887/src/main/resources/spring/batch/job/file-multiresourcepartitioner-filter-folders-factory-job.xml, check file-multiresourcepartitioner-filters-folder-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>

, рабочий пример доступен на моем репозитории github на https://github.com/langmi/spring-batch-examples/tree/master/playground/src/main/resources/spring/batch/job,проверьте, file-multiresourcepartitioner-filter-folder-factory-job.xml

вы можете выбрать метод для установки массива ресурсов из Список всех файлов из каталога рекурсивно с Java и аналогичные решения в сети, остается только фильтр, в приведенной ссылке есть одно решение

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...