Spring Integration with RedisLockRegistry example

南笙酒味 提交于 2019-12-11 04:55:51

问题


We are implementing a flow where a <int-sftp:inbound-streaming-channel-adapter/> polls a directory for a file and when found it passes the stream to a service activator.

The issue is we will have multiple instances of the app running and we would like to lock the process so that only one instance can pick up the file.

Looking at the documentation, Redis Lock Registry looks to be the solution, is there an example of this being used in xml?

All I can find is a few references to it and the source code for it.

http://docs.spring.io/spring-integration/reference/html/redis.html point 24.1

Added info: Ive added the RedisMetaDataStore and SftpSimplePatternFileListFilter. It does work but it does have one oddity, when sftpInboundAdapter is activated by the poller it adds an entry for each file in the metadatastore. Say there are 10 files, there would be 10 entries in the datastore, but it does not process all 10 files in "1 go", only 1 file is processed per poll from the adapter, which would be fine, but in a multi instance environment if the server which picked up the files went down after processing 5 files, another server doesn't seem to pick up the remaining 5 files unless the files are "touched".

Is the behaviour of picking up 1 file per poll correct or should it process all valid files during one poll.

Below is my XML

   <int:channel id="sftpInbound"/> <!--  To Java -->
<int:channel id="sftpOutbound"/>
<int:channel id="sftpStreamTransformer"/>

<int-sftp:inbound-streaming-channel-adapter id="sftpInboundAdapter"
        channel="sftpInbound"
        session-factory="sftpSessionFactory"
        filter="compositeFilter"
        remote-file-separator="/"
        remote-directory="${sftp.directory}">
    <int:poller cron="${sftp.cron}"/>
</int-sftp:inbound-streaming-channel-adapter>

<int:stream-transformer input-channel="sftpStreamTransformer" output-channel="sftpOutbound"/>

<bean id="compositeFilter"
    class="org.springframework.integration.file.filters.CompositeFileListFilter">
    <constructor-arg>
        <list>
            <bean
                class="org.springframework.integration.sftp.filters.SftpSimplePatternFileListFilter">
                <constructor-arg value="Receipt*.txt" />
            </bean>
            <bean id="SftpPersistentAcceptOnceFileListFilter" class="org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter">
                <constructor-arg ref="metadataStore" />                 
                <constructor-arg value="ReceiptLock_" />                    
            </bean>
        </list>
    </constructor-arg>
</bean>

<bean id="redisConnectionFactory"
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="port" value="${redis.port}" />
    <property name="password" value="${redis.password}" />
    <property name="hostName" value="${redis.host}" />
</bean>

回答1:


No; you need to use a SftpPersistentAcceptOnceFileListFilter (docs here) with a Redis (or some other) metadata store, not a lock registry.

EDIT

Regarding your comment below.

Yes, it's a known issue; in the next release we've added a max-fetch-size for exactly this reason - so the instances can each retrieve some of the files rather than the first instance grabbing them all.

(The inbound adapter works by first copying files found, that are not already in the store, to the local disk, and then emits them one at a time).

5.0 only available as a milestone right now M2 at the time of writing, but the current version and milestone repo can be found here; it won't be released for a few more months.

Another alternative would be to use outbound gateways - one to LS the files and one to GET individual files; your app would have to use the metadata store itself, though, to determine which file(s) can be fetched.



来源:https://stackoverflow.com/questions/42296517/spring-integration-with-redislockregistry-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!