file:inbound-channel-adapter在使用自定义扫描程序时失败

时间:2016-02-16 22:50:06

标签: java spring file integration

我正在使用spring集成4.2.4.RELEASE,我遇到了一个bug。我正在尝试使用基于“WatchServiceDirectoryScanner”的自定义目录创建入站通道。当我尝试注入此扫描仪时,我收到错误。

“'过滤器'和'储物柜'选项必须出现在提供的外部'扫描仪'上:”。无论我尝试过哪种属性组合都行不通。原因是因为即使我提供了一个储物柜和过滤器到我的自定义扫描仪,春天的“FileReadingMessageSource”创建了自己的。因此,当它断言

Assert.state(!(this.scannerExplicitlySet && (this.filter != null || this.locker != null)),
                "The 'filter' and 'locker' options must be present on the provided external 'scanner': "
                        + this.scanner);

失败了。有一个过滤器是“FileListFilterFactoryBean.initializeFileListFilter”,无论设置了什么组合,下面的代码都会创建一个,而整个事情都失败了。

        // no filters are provided
        else if (Boolean.FALSE.equals(this.preventDuplicates)) {
            filtersNeeded.add(new AcceptAllFileListFilter<File>());
        }
        else { // preventDuplicates is either TRUE or NULL
            filtersNeeded.add(new AcceptOnceFileListFilter<File>());
        }

我已阅读帖子:How to skip the settings of filter and locker 但它对我不起作用。 有没有人为此找到解决方案?

以下是创建入站通道的XML配置示例。

        <bean id="inboundChannel_3522_filter" class="org.springframework.integration.file.filters.CompositeFileListFilter">
        <constructor-arg>
            <list>
                <bean class="org.springframework.integration.file.filters.RegexPatternFileListFilter">
                    <constructor-arg value="^Test_(20160216).TXT$" />
                </bean>
            </list>
        </constructor-arg>
    </bean>
    <bean id="inboundChannel_3522_nio_locker" class="org.springframework.integration.file.locking.NioFileLocker" />
    <bean id="inboundChannel_3522_scanner" class="com.test.spring.integraton.file.NonRecursiveWatchServiceDirectoryScanner"
>
        <constructor-arg value="e:\data\incoming\Test-V2" />
        <property name="filter" ref="inboundChannel_3522_filter"/> 
        <property name="locker" ref="inboundChannel_3522_nio_locker"/> 
    </bean>
    <file:inbound-channel-adapter  id="inboundChannel_3522"  auto-create-directory="true"  scanner="inboundChannel_3522_scanner" 
 directory="file:/c:/data/incoming/TEST/"  prevent-duplicates="false"  ignore-hidden="false"  >
        <integration:poller fixed-rate="1000"/>
    </file:inbound-channel-adapter>

基本上我想知道是否有任何方法可以覆盖bean FileReadingMessageSource,以便我可以更改Assert?

1 个答案:

答案 0 :(得分:0)

我已经弄清楚如何解决这个错误。所以我创建了一个扫描仪,然后将过滤器和储物柜注入扫描仪。我创建了一个自定义FileReadingMessageSourceFactoryBean,如果已注入扫描程序,它会跳过创建过滤器和锁定器。具有更改的CustomFileReadingMessageSourceFactoryBean如下所示。这些变化发生在第172行。其余的都是相同的。

            if (this.scanner != null)
        {
            this.source.setScanner(this.scanner);
        }
        else
        {
            if (this.filter != null)
            {
                if (this.locker == null)
                {
                    this.source.setFilter(this.filter);
                }
                else
                {
                    CompositeFileListFilter<File> compositeFileListFilter = new CompositeFileListFilter<File>();
                    compositeFileListFilter.addFilter(this.filter);
                    compositeFileListFilter.addFilter(this.locker);
                    this.source.setFilter(compositeFileListFilter);
                    this.source.setLocker(locker);
                }
            }
            else if (this.locker != null)
            {
                CompositeFileListFilter<File> compositeFileListFilter = new CompositeFileListFilter<File>();
                compositeFileListFilter.addFilter(new FileListFilterFactoryBean().getObject());
                compositeFileListFilter.addFilter(this.locker);
                this.source.setFilter(compositeFileListFilter);
                this.source.setLocker(locker);
            }
        }

新的Spring XML如下所示。不同之处在于我们在最后添加了bean inboundChanel_3522.adapter.source。这样做的是当入站通道查找FileReadingMessageSourceFactoryBean类型的bean时,它将向其id添加“.adpater.source”,如果存在,它将使用它,否则它将创建默认值。在这种情况下,它将使用我们的CustomFileReadingMessageSourceFactoryBean,它具有跳过创建其他过滤器的逻辑。自定义bean位于入站通道适配器之后非常重要,否则bean工厂将覆盖它。最后,这里是XML应该是什么样子。

 <bean id="inboundChannel_3522_filter" class="org.springframework.integration.file.filters.CompositeFileListFilter">
     <constructor-arg>
         <list>
             <bean class="org.springframework.integration.file.filters.RegexPatternFileListFilter">
                 <constructor-arg value="^Test_(20160216).TXT$" />
             </bean>
         </list>
     </constructor-arg>
</bean>
<bean id="inboundChannel_3522_nio_locker" class="org.springframework.integration.file.locking.NioFileLocker" />
<bean id="inboundChannel_3522_scanner" class="com.test.spring.integraton.file.NonRecursiveWatchServiceDirectoryScanner">
    <constructor-arg value="e:\data\incoming\Test-V2" />
    <property name="filter" ref="inboundChannel_3522_filter"/> 
    <property name="locker" ref="inboundChannel_3522_nio_locker"/> 
</bean>
<file:inbound-channel-adapter  id="inboundChannel_3522"  auto-create-directory="true"  
    scanner="inboundChannel_3522_scanner" 
    directory="file:/c:/data/incoming/TEST/"  prevent-duplicates="false"  ignore-hidden="false"  >
    <integration:poller fixed-rate="1000"/>
</file:inbound-channel-adapter>
<bean id="inboundChannel_3522.adapter.source" class="com.test.spring.integraton.file.CustomFileReadingMessageSourceFactoryBean">
    <property name="scanner" ref="inboundChannel_3522_scanner"/> 
    <property name="directory" value="e:\data\incoming\Test-V2"/> 
    <property name="autoCreateDirectory" value="true"/> 
</bean>