[英]Spring integration: no such method exception for custom inbound channel adapter
[英]Override method in spring integration inbound channel adapter
我有一個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,以便每次它都會再次重新掃描目錄。 它的工作,我看到它每次都掃描目錄。 默認情況下為false。
在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);
將繼續添加一些相同的文件。 我希望做一些重寫此方法的工作,以便可以在addAll()
之前先清理toBeReceived
Queue。
我相信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)等...
我設法通過創建自己的掃描儀來解決此問題。
這是
<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()]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.