2016-08-14 93 views
2

我需要處理Excel文件並將其內容發佈到REST服務。我可以通過Apache POI加載並將Excel行轉換爲JSON數組。但是,在發佈到REST服務時,它會因HTTP狀態413而失敗,因爲由於Excel文件中的行數太多,POST正文太大。Apache Camel - 分割JSON數組並重復調用POST到REST服務

Apache Camel中有一種方法可以限制REST服務的JSON輸入大小並重復調用REST服務調用。請幫忙。

下面是Java DSL路由配置。

from("file:/excelfilelocation/inputexcel.xls") 
     .bean(new ExcelConverter(), "processExcel") // Converts excel rows to ArrayList of model object 
     .marshal().json(JsonLibrary.Jackson) 
     .setHeader("Authorization", simple(apiKEY)) 
     .setHeader(Exchange.HTTP_METHOD, constant("POST")) 
     .setHeader(Exchange.HTTP_URI, simple(API_URL)) 
     .setHeader(Exchange.CONTENT_TYPE, constant("application/json")) 
     .to(API_URL) 
     .to("mock:finish"); 

錯誤我得到的是因爲大的POST正文內容長度。

org.apache.camel.http.common.HttpOperationFailedException:HTTP 操作失敗,調用 https://test.com/api/v2/receipts用的StatusCode:

回答

2

可以使用Splitter EIP分裂的Excel項,然後Aggregator EIP將它們收集到更可管理的批次中,然後發送那些批次:

from("file:/excelfilelocation?fileName=inputexcel.xls") 
    .bean(new ExcelConverter(), "processExcel") 
    .split(body()) 
    .aggregate(constant(true), new GroupedBodyAggregationStrategy()) 
     .completionSize(100) 
     .completionTimeout(1000) 
     .marshal().json(JsonLibrary.Jackson) 
     .setHeader("Authorization", simple(apiKEY)) 
     .setHeader(Exchange.HTTP_METHOD, constant("POST")) 
     .setHeader(Exchange.HTTP_URI, simple(API_URL)) 
     .setHeader(Exchange.CONTENT_TYPE, constant("application/json")) 
     .to(API_URL); 

下面是彙總策略,將行收集到列表中:

public class GroupedBodyAggregationStrategy extends AbstractListAggregationStrategy<Message> { 

    @Override 
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { 
     if (oldExchange == null) { 
      // for the first time we must create a new empty exchange as the 
      // holder, as the outgoing exchange 
      // must not be one of the grouped exchanges, as that causes a 
      // endless circular reference 
      oldExchange = new DefaultExchange(newExchange); 
     } 
     return super.aggregate(oldExchange, newExchange); 
    } 

    @Override 
    public Object getValue(Exchange exchange) { 
     return exchange.getIn().getBody(); 
    } 
} 
+0

非常感謝,它工作。 – user1637487

+0

您能否接受答案,以便該問題不再列爲未答覆? –