2011-10-31 1 views
0

У меня есть следующие конфигурации в файле dataloader.propertiesMultiResourceParitioner в Spring Batch принимать файлы из нескольких папок

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

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

<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, проверьте файлов multiresourcepartitioner-фильтр-папки-job.xml

более подключаемым решение: Factory, который создает ресурсы для 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, проверьте файл-multiresourcepartitioner-фильтр-папка-завод-job.xml

вы можете выбрать метод установки массива ресурсов из List all files from a directory recursively with Java и аналогичных решений по всей сети, остался только фильтр, есть одно решение по ссылке

+0

спасибо .pattern изменения. я должен вызвать setResource из конструктора. –

+0

Я не понимаю, это вопрос? –

+0

да вопрос. Я не очень хорошо владею английским языком. –