简体   繁体   English

Spring Integration入站通道适配器中的覆盖方法

[英]Override method in spring integration inbound channel adapter

I have a Spring Integration listener, where the ApplicationContext is something as follow: 我有一个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");

In Spring integration listener, I wish to ask the listener to rescan the folder everytime. 在Spring集成监听器中,我希望让监听器每次都重新扫描文件夹。 So I do as follow: 所以我做如下:

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 );
        }

I set the value of scanEachPoll to true, so that everytime it will rescan the directory again. 我将scanEachPoll的值设置为true,以便每次它都会再次重新扫描目录。 Its work, I saw it scan the directory everytime. 它的工作,我看到它每次都扫描目录。 By default its false. 默认情况下为false。

Edit a variable in FileReadingMessageSource class is not a problem. FileReadingMessageSource类中编辑变量不是问题。 But I am facing some problem for edit a method inside this class. 但是我在编辑此类中的方法时遇到了一些问题。

There will be a method as follow in this class: 此类中将包含以下方法:

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);
            }
        }
    }

Because of my modification, this listener will scan the directory everytime. 由于我的修改,此侦听器将每次扫描目录。 Thus, toBeReceived.addAll(freshFiles); 因此, toBeReceived.addAll(freshFiles); will keep adding some same file. 将继续添加一些相同的文件。 I wish to do something to override this method, so that I can clean the toBeReceived Queue first before addAll() . 我希望做一些重写此方法的工作,以便可以在addAll()之前先清理toBeReceived Queue。

I believe FileReadingMessageSource class is come from bp-integration-fileupload-context.xml . 我相信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>

Anything I can "inject" into this xml portion to override the original FileReadingMessageSource class? 我可以“注入”此xml部分以覆盖原始FileReadingMessageSource类吗?

Or Kindly advise the better way to do it. 或者,请提出更好的方法。

Maybe my question is confusing, let me add some example here: 也许我的问题令人困惑,让我在这里添加一些示例:

actually its something like this: 实际上是这样的:

Original version: 原始版本:

scanEachPoll is false by default. 默认情况下,scanEachPoll为false。

So, scanInputDirectory() method wont be call everytime. 因此,不会每次都调用scanInputDirectory()方法。 It will only call when the Queue is empty. 仅在队列为空时调用。

But now, I change the scanEachPoll to true. 但是现在,我将scanEachPoll更改为true。 ( This one can change easily ) (这个可以轻易改变)

When scanEachPoll is true, then scanInputDirectory() method will be call everytime, so it will something like: 如果scanEachPoll为true,则每次都会调用scanInputDirectory()方法,因此它将类似于:

1st time scan, add to queue (1,2,3,4,5) poll(), become (2,3,4,5) 2nd time scan, add to queue will be (2,3,4,5) + (2,3,4,5) => become (2,2,3,3,4,4,5,5) poll(), become (2,3,3,4,4,5,5) 3rd time scan, add to queue will become original queue + (3,4,5) ==> become (2,3,3,4,4,5,5,3,4,5) and so on... 第一次扫描,添加到队列(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)等...

I manage to handle this problem by creating my own Scanner. 我设法通过创建自己的扫描仪来解决此问题。

which is 这是

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

My scanner only return the first item: 我的扫描仪仅返回第一项:

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()]);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM