2011-10-19 43 views
0

我有一個從隊列中讀取的大型XML消息,我需要將它拆分爲塊並將其轉換爲對象,然後根據對象將它們路由到各個目標。拆分郵件並將它們路由到駝峯

所以我已經配置了routeBuilder到

ChoiceDefinition choice = from(routeConfig.getFromEndpoint()) 
       .split().method(xmlSplitter, "splitMessage").streaming().process(xmlProcessor).choice(); 
for (RouteConfig filter : filters) { 
    choice = choice.when(header(REPORT_TYPE_HEADER_NAME).contains(filter.getReportTypeHeaderFilter())) 
        .to(filter.getToEndpoint()); 
} 
choice.otherwise().to(routeConfig.getErrorEndpoint()); 

但路由是不會發生在所有的,所有消息都發送到errorEndpoint。 我發現原因是分隔符刪除標題,因爲它在路由之前。

看來我不能在路由後使用分割。

解決此問題的解決方案是什麼?

回答

1

split()不應刪除標頭......您確定您的xmlSplitter/xmlProcessor不會引起問題嗎?

這裏是一個簡單的例子來表明,頭被保留下來......

@EndpointInject(uri = "mock:mock") 
protected MockEndpoint mock; 

@Test 
public void test() throws Exception { 
    mock.expectedMessageCount(2); 
    mock.expectedHeaderReceived("foo","bar"); 
    template.sendBodyAndHeader("direct:start", "msg1,msg2", "foo", "bar"); 
    assertMockEndpointsSatisfied(); 
} 

@Override 
protected RouteBuilder createRouteBuilder() throws Exception { 
    return new RouteBuilder() { 
     @Override 
     public void configure() throws Exception { 

      from("direct:start") 
       .to("log:+++before+++?showHeaders=true") 
       .split().method(MySplitterBean.class, "splitBody").streaming() 
       .to("log:+++after+++?showHeaders=true") 
       .choice().when(header("foo").contains("bar")) 
        .to("mock:mock") 
       .otherwise() 
        .to("mock:error"); 
     } 
    }; 
} 

public static class MySplitterBean { 
    public List<String> splitBody(String body) { 
     List<String> answer = new ArrayList<String>(); 
     String[] parts = body.split(","); 
     for (String part : parts) { 
      answer.add(part); 
     } 
     return answer; 
    } 
} 
相關問題