2017-06-29 86 views
0

我有一個簡單的路由輪詢從FTP服務器的zip文件。該zip文件由一個需要處理的文件和零個或多個附件組成。 我想使用ZipFileDataFormat進行拆分,並且能夠根據需要拆分和路由項目,即將處理文件發送到處理器和其他文件到聚合端點。駱駝ZipFileDataFormat拆分不設置流標題時

路線看起來象下面這樣:

from(sftp://[email protected]/folder/path?password=password&delay=600000) 
.unmarshal(getZipFileDataFormat()).split(body(Iterator.class)).streaming() 
.log("CamelSplitComplete :: ${header.CamelSplitComplete}") 
.log("Split Size :: ${header.CamelSplitSize}") 
.choice() 
    .when(header(MyConstants.CAMEL_FILE_NAME_HEADER).contains(".json")) 
    .to(JSON_ENDPOINT).endChoice() 
    .otherwise() 
    .to(AGGREGATOR_ENDPOINT) 
.endChoice() 
.end(); 

getZipFileDataFormat

private ZipFileDataFormat getZipFileDataFormat() { 
     ZipFileDataFormat zipFile = new ZipFileDataFormat(); 
     zipFile.setUsingIterator(true); 
     return zipFile; 
    } 

分裂正常工作。但是,我可以在日誌中看到兩個標頭CamelSplitCompleteCamelSplitSize未正確設置。 CamelSplitComplete始終爲false,CamelSplitSize沒有任何價值。

因此,我無法根據大小進行聚合。我正在使用eagerCheckCompletion()獲取聚合器路由中的輸入交換。我的聚合器路線如下所示。

from(AGGREGATOR_ENDPOINT).aggregate(new ZipAggregationStrategy()).constant(true) 
.eagerCheckCompletion().completionSize(header("CamelSplitSize"))to("file:///tmp/").end(); 

我讀Apache Documentation這些標題總是設置。我在這裏錯過了什麼嗎?任何指向正確方向的指針都會非常有幫助。

+0

在分流器上使用流模式時沒有'CamelSplitSize',如記錄。完成的應該在那裏。 –

+0

謝謝@ClausIbsen!有沒有辦法確定zip中的文件數量呢?我們可以使用CamelSplitIndex - 1來確定文件的數量嗎? –

+0

@ClausIbsen,它也在文檔中說**從駝峯2.9開始,這個頭文件也是基於流的分割,但是隻在完成的Exchange中。**我正在使用2.12.5 –

回答

0

我能夠得到整個工作路線。我不得不添加一種預處理器來設置聚合所需的一些重要頭文件(傳出文件名和zip文件數)。

from(sftp://[email protected]/folder/path?password=password&delay=600000).to("file:///tmp/") 
.beanRef("headerProcessor").unmarshal(getZipFileDataFormat()) 
.split(body(Iterator.class)).streaming()  
.choice() 
    .when(header(Exchange.FILE_NAME).contains(".json")) 
    .to(JSON_ENDPOINT).endChoice() 
    .otherwise() 
    .to(AGGREGATOR_ENDPOINT) 
.endChoice() 
.end(); 

之後,zip聚合策略按預期工作。把聚合路線放在這裏只是爲了完成答案。

from(AGGREGATOR_ENDPOINT) 
    .aggregate(header(MyConstants.HEADER_OUTGOING_FILE_NAME), new ZipAggregationStrategy()) 
    .eagerCheckCompletion().completionSize(header(MyConstants.HEADER_TOTAL_FILE_COUNT)) 
    .setHeader(Exchange.FILE_NAME, simple("${header.outgoingFileName}")) 
.to("file:///tmp/").end();