2014-10-20 49 views
1

創建我有一大堆的相關PDF和XML文件中輸入文件夾附文件:如何分割由聚合

1.PDF - 1.XML 
2.PDF - 2.XML 
3.PDF - 3.XML 
etc. 

的PDF文件是掃描的文檔,以及相關的XML文件中包含相關的索引信息到PDF文件。

我使用Apache Camel來輪詢文件夾中的XML和PDF文件,然後一旦我有一對相關的文件,我就會發布到輸出文件夾。理想情況下,我想最後發佈XML文件。

下面的代碼似乎正確地拿起這對,但當我嘗試再次分割文件時,我收到一條錯誤消息。

@Grab('org.apache.camel:camel-core:2.13.0') 
@Grab('org.slf4j:slf4j-simple:1.6.6') 
import org.apache.camel.* 
import org.apache.camel.impl.* 
import org.apache.camel.builder.* 
import org.apache.camel.util.jndi.* 

def jndiContext = new JndiContext(); 

def dataDir = "/${System.properties['user.home']}/test/file-poller-demo" 
def camelContext = new DefaultCamelContext(jndiContext) 
camelContext.addRoutes(new RouteBuilder() { 
    def void configure() { 
    from("file://${dataDir}/in") 
     .aggregate(simple('${file:onlyname.noext}')).completionSize(2).groupExchanges() 
     .split(body()) 
     .to("file://${dataDir}/out") 
} 
}) 
camelContext.start() 
addShutdownHook{ camelContext.stop() } 
synchronized(this){ this.wait() } 

錯誤消息:

無可用的類型的主體:java.io.InputStream中但具有值:交換[1.XML]類型:org.apache.camel.impl.DefaultExchange上:1.txt。導致:沒有類型轉換器可用於從類型:org.apache.camel.impl.DefaultExchange轉換爲所需類型:值爲Exchange [1.xml]的java.io.InputStream。交換[1.TXT。

回答

1

以下是Java DSL中的工作示例。

您可以添加額外的處理器,以在彙總交換後設置正確的輸出主體,但在文件使用者中沒有noop = true選項時,第一個文件將在聚合之後移動到.camel,並且文件生產者將拋出一個當它試圖將其寫入發件箱文件夾時的表達式。

from("file:data/inbox?noop=true") 
     .aggregate(simple("${file:onlyname.noext}")).completionSize(2).groupExchanges() 
     .split(body()) 
     .process(new Processor() { 
     @Override 
     public void process(Exchange exchange) throws Exception { 
      Exchange exchangeIn = exchange.getIn().getBody(Exchange.class); 
      exchange.getIn().setBody(exchangeIn.getIn().getBody(GenericFile.class)); 
      exchange.getIn().setHeaders(exchangeIn.getIn().getHeaders()); 
     } 
     }) 
     .to("file:data/outbox/"); 

我認爲將文件加載到內存數據存儲中然後再進行聚合會更好。

在下面的路由中,在文件被使用後,它被添加到內存中的數據存儲中。聚合完成後,文件從分離器中的數據存儲中檢索,數據存儲在上次交換中清除。文件使用者中的刪除選項是可選的。如果沒有添加,文件將在完成後移動到.camel。

from("file:data/inbox?delete=true") 
.setProperty("correlationId", simple("${file:onlyname.noext}")) 
.beanRef("dataStoreBean","addToStore") 
.aggregate(property("correlationId")).completionSize(2).groupExchanges() 
.split(body()) 
.beanRef("dataStoreBean", "getFromStore") 
.to("file:data/outbox/"); 

當數據存儲bean是:

public class DataStoreBean { 

    Map<String,byte[]> dataStore = new HashMap<String,byte[]>(); 

    public void addToStore(Exchange exchange) { 
     dataStore.put(exchange.getIn().getHeader(Exchange.FILE_NAME,StrinTheg.class),exchange.getIn().getBody(byte[].class)); 
     exchange.getOut().setBody(exchange.getIn().getHeader(Exchange.FILE_NAME,String.class)); 
    } 

    public void getFromStore(Exchange exchange) { 
    Exchange fileExchange = exchange.getIn().getBody(Exchange.class); 
    String fileName = fileExchange.getIn().getBody(String.class); 
    exchange.getOut().setHeader(Exchange.FILE_NAME, fileName); 
    exchange.getOut().setBody(dataStore.get(fileName)); 
    if(exchange.getProperty("CamelSplitComplete", Boolean.class)) { 
     String correlationId = fileExchange.getProperty("correlationId", String.class); 
     dataStore.remove(correlationId+".pdf"); 
     dataStore.remove(correlationId+".xml"); 
    } 
} 
}