2016-04-21 74 views
0

我正在努力實施WatchServiceDirectoryScanner。我想使用掃描儀來監視新文件上傳到目錄+子目錄。這將作爲Spring啓動MVC微服務的一部分存在。我可以使用Java 7的WatchService來做到這一點,但更喜歡彈簧文件集成風格,AOP風格春季watchservicedirectoryscanner的工作例子

我已經在我的應用程序配置中註冊爲@Bea n,但我一直在努力弄清楚如何讓它輪詢和掃描一個目錄,然後在檢測到文件時調用... something(消息端點?)。任何人都可以在正確的方向上指向我,甚至在概念上這是如何完成的。我找不到任何地方的示例實現。

http://docs.spring.io/spring-integration/reference/html/files.html#_watchservicedirectoryscanner

http://docs.spring.io/spring-integration/api/org/springframework/integration/file/DefaultDirectoryScanner.html#listFiles-java.io.File-

這裏是我的春天的AppConfig:

public class appConfig { 
    @Bean 
    public DirectoryScanner scanner() { 
     return new WatchServiceDirectoryScanner("/uploads/test"); 
    } 
} 
+0

只有加入bean要取消究竟什麼。你需要將它綁定到一個頻道才能發佈evens /消息,然後爲頻道的另一端創建一些與之相關的內容。示例就在參考指南中,在您發送的鏈接上方10行。 –

+0

是的,我知道我需要一個通道來接收消息(我提到'消息端點')以及它正是我卡住的東西。我現在再次看到它,謝謝 – wired00

回答

0
# create one configuration file and bind an input file channel here 

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:int="http://www.springframework.org/schema/integration" 
     xmlns:int-file="http://www.springframework.org/schema/integration/file" 
     xmlns:int-mail="http://www.springframework.org/schema/integration/mail" 
     xmlns:int-stream="http://www.springframework.org/schema/integration/stream" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd 
      http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd 
      http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd 
      http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

     <int:annotation-config /> 
      <int:channel id="cfpFileIn"></int:channel> 
    <int-file:inbound-channel-adapter id="cfpFileIn" 
      directory="${cfp.flight.data.dir}" auto-startup="true" scanner="csvDirScanner"> 
      <int:poller fixed-delay="${cfp.flight.data.dir.polling.delay}"></int:poller> 
     </int-file:inbound-channel-adapter> 
    <bean id="csvDirScanner" 
      class="org.springframework.integration.file.WatchServiceDirectoryScanner"> 
      <constructor-arg index="0" value="${cfp.flight.data.dir}" /> 
      <property name="filter" ref="csvCompositeFilter" /> 
      <property name="autoStartup" value="true" /> 
     </bean> 

    <bean id="csvCompositeFilter" 
      class="org.springframework.integration.file.filters.CompositeFileListFilter"> 
      <constructor-arg> 
       <list> 
        <bean 
         class="org.springframework.integration.file.filters.SimplePatternFileListFilter"> 
         <constructor-arg value="*.csv" /> 
        </bean> 
        <ref bean="persistentFilter" /> 
       </list> 
      </constructor-arg> 
     </bean> 

    <bean id="persistentFilter" 
      class="org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter"> 
      <constructor-arg index="0" ref="metadataStore" /> 
      <constructor-arg index="1" name="prefix" value="" /> 
      <property name="flushOnUpdate" value="true" /> 
     </bean> 

    <bean name="metadataStore" 
      class="org.springframework.integration.metadata.PropertiesPersistingMetadataStore"> 
      <property name="baseDirectory" value="${metadata.dir}"></property> 
     </bean> 
</beans> 
+0

請給你的答案添加一些解釋 – Sampada

0
import java.io.File; 
import java.util.concurrent.locks.ReentrantLock; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.batch.core.Job; 
import org.springframework.batch.core.JobExecution; 
import org.springframework.batch.core.JobParameters; 
import org.springframework.batch.core.JobParametersBuilder; 
import org.springframework.batch.core.JobParametersInvalidException; 
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; 
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; 
import org.springframework.batch.core.repository.JobRestartException; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.integration.annotation.ServiceActivator; 
import org.springframework.stereotype.Component; 
@Component 
public class BatchJobScheduler { 

    private static final Logger logger = LoggerFactory.getLogger(BatchJobScheduler.class); 
    @Autowired 
protected JobLauncher jobLauncher; 

    @Autowired 
    @Qualifier(value = "job") 
    private Job job; 

    @ServiceActivator(inputChannel = "cfpFileIn") 
    public void run(File file) { 
     String fileName = file.getAbsolutePath(); 
     logger.info("BatchJobScheduler Running #################"+fileName); 
     JobParameters jobParameters = new JobParametersBuilder().addString(
       "input.file", fileName).toJobParameters(); 


     try { 

      JobExecution execution = jobLauncher.run(job, 
        jobParameters); 
      logger.info(" BatchJobScheduler Exit Status : " + execution.getStatus() +"::"+execution.getAllFailureExceptions()); 

     } catch (JobExecutionAlreadyRunningException | JobRestartException 
       | JobInstanceAlreadyCompleteException 
       | JobParametersInvalidException e) { 
      logger.error(" BatchJobScheduler Exit Status : Exception ::",e); 
     } 
    } 
}