0

我有一个 Spring Integration 侦听器,其中ApplicationContext内容如下:

final AbstractApplicationContext context = new ClassPathXmlApplicationContext(

        // Spring Integration common xml
                "classpath:contexts/bp-common.xml",

                // File Upload Listener
                "classpath:contexts/bp-integration-fileupload-context.xml");

在 Spring 集成侦听器中,我希望每次都要求侦听器重新扫描文件夹。所以我这样做:

Map< String, FileReadingMessageSource > fileReadingSourceMap = new HashMap< String, FileReadingMessageSource >( );

        fileReadingSourceMap = context.getBeansOfType( FileReadingMessageSource.class );

        List< FileReadingMessageSource > fileReadingSourceList = new ArrayList< FileReadingMessageSource >(
                fileReadingSourceMap.values( ) );

        for ( FileReadingMessageSource frms : fileReadingSourceList ) {
            frms.setScanEachPoll( true );
        }

我将值设置scanEachPoll为true,这样每次它都会重新扫描目录。它的工作,我看到它每次都扫描目录。默认情况下它是假的。

在类中编辑变量FileReadingMessageSource不是问题。但是我在编辑这个类中的方法时遇到了一些问题。

在这个类中会有一个方法如下:

private void scanInputDirectory() {
        List<File> filteredFiles = scanner.listFiles(directory);
        Set<File> freshFiles = new LinkedHashSet<File>(filteredFiles);
        if (!freshFiles.isEmpty()) {
            toBeReceived.addAll(freshFiles);
            if (logger.isDebugEnabled()) {
                logger.debug("Added to queue: " + freshFiles);
            }
        }
    }

因为我的修改,这个监听器每次都会扫描目录。因此,toBeReceived.addAll(freshFiles);将继续添加一些相同的文件。我希望做一些事情来覆盖这个方法,这样我就可以toBeReceived先清理队列了addAll()

我相信FileReadingMessageSource类来自bp-integration-fileupload-context.xml.

<int-file:inbound-channel-adapter id="hostFilesOut" channel="hostFileOutChannel"
        directory="${hostfile.dir.out}" prevent-duplicates="false"
        filename-regex="${hostfile.out.filename-regex}" >
        <int:poller id="poller" cron="${poller.cron:0,4,8,12,16,20,24,28,32,36,40,44,48,52,56 * * * * * }"
            max-messages-per-poll="1" />
    </int-file:inbound-channel-adapter>

任何我可以“注入”到这个 xml 部分来覆盖原始FileReadingMessageSource类的东西?

或者请建议更好的方法。

也许我的问题令人困惑,让我在这里添加一些示例:

实际上它是这样的:

原始版本:

scanEachPoll 默认为 false。

所以,scanInputDirectory() 方法不会每次都被调用。它只会在队列为空时调用。

但是现在,我将 scanEachPoll 更改为 true。(这个很容易改变)

当 scanEachPoll 为 true 时,每次都会调用 scanInputDirectory() 方法,所以它类似于:

第一次扫描,加入队列 (1,2,3,4,5) poll(),成为 (2,3,4,5) 第二次扫描,加入队列将是 (2,3,4,5) + (2,3,4,5) => 变成 (2,2,3,3,4,4,5,5) poll(), 变成 (2,3,3,4,4,5,5)第三次扫描,加入队列会变成原来的队列 + (3,4,5) ==> 变成 (2,3,3,4,4,5,5,3,4,5) 以此类推...

4

1 回答 1

0

我设法通过创建自己的扫描仪来解决这个问题。

这是

<bean id="myDirectoryScanner" class="bp.main.myDirectoryScanner" ></bean>

我的扫描仪只返回第一项:

protected File[] listEligibleFiles(File directory)
            throws IllegalArgumentException {

        File[] rootFiles = directory.listFiles();
        List<File> files = new ArrayList<File>(rootFiles.length);
        if ( rootFiles != null ) {
            files.add(rootFiles[0]);
        }
        return files.toArray(new File[files.size()]);
    }
于 2018-02-26T03:49:34.687 回答