2017-02-21 29 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”时(或根据您的要求在下一次轮询之前)停止适配器。 –

相关问题