2017-02-21 20 views
0

我有要求從源目錄讀取文件並按順序(逐個)處理它。如何使用Spring集成文件入站適配器按順序處理文件

我有如下應用上下文的配置,

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

    <int:channel id="controlChannel" /> 

    <int:channel id="adapterInputChanel"> 
     <int:queue /> 
    </int:channel> 

    <int:control-bus input-channel="controlChannel" /> 

    <file:inbound-channel-adapter id="inboundAdapter" 
     directory="inputdir" 
     channel="adapterInputChanel" auto-startup="false" prevent-duplicates="true" 
     ignore-hidden="true"> 
     <int:poller id="poller" fixed-delay="5000" 
      max-messages-per-poll="1"> 
     </int:poller> 
    </file:inbound-channel-adapter> 


    <int:service-activator input-channel="adapterInputChanel" 
     ref="mainService" method="readFiles" output-channel="filesOut"> 
     <int:poller fixed-delay="500" /> 
    </int:service-activator> 

    <bean id="mainService" class="com.sample.MainService"> 

    </bean> 

    <file:outbound-channel-adapter id="filesOut" 
     directory="output dir" /> 

</beans> 

按照上述配置,文件站適配器將得到每一個輪詢文件並執行服務執行。

但是與此同時,如果系統正在獲取另一個文件,則文件入站適配器在第一個事務完成之前不應再次觸發該服務。

請讓我知道如何處理這個問題的一些樣本。

回答

0

adapterInputChanel中刪除<queue/>,並從服務激活器中刪除<poller/>

然後,頻道將是DirectChannel,您的服務將在適配器的輪詢線程上運行,並且下一個文件將不會被處理,直到服務退出。

你可以閱讀關於different channel types here;特別是可訂閱和可輪播頻道之間的區別。

+0

謝謝。有效。我正在使用控制總線來啓動/停止像下面這樣的輪詢過程, controlChannel = ac.getBean(「controlChannel」,MessageChannel.class); SubscribableChannel adapterInputChanel = ac.getBean(「adapterInputChanel」,SubscribableChannel.class); controlChannel.send(new GenericMessage (「@ inboundAdapter.start()」)); 但問題是,如果我停止適配器,相應的服務正在立即終止。在這種情況下,文件入站適配器應該只停止進一步的輪詢。你能否就此提出建議。 – dave

+0

停止適配器不會影響當前線程,除非它執行了可中斷的操作。如果是這樣,您可以使用[智能輪詢器](http://docs.spring.io/spring-integration/reference/html/messaging-channels-section.html#__smart_polling)推遲停止 - 使用控制總線向輪詢者發送消息,告訴它在輪詢返回「null」時(或根據您的要求在下一次輪詢之前)停止適配器。 –

相關問題