【问题标题】:Split messages and route them in camel拆分消息并以骆驼方式路由它们
【发布时间】:2011-12-11 02:07:21
【问题描述】:

我有一个从队列中读取的大型 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。 我发现原因是拆分器删除了标头,因为它在路由之前。

路由后好像不能使用拆分。

解决这个问题的方法是什么?

【问题讨论】:

    标签: java integration apache-camel


    【解决方案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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 2014-10-15
      • 1970-01-01
      相关资源
      最近更新 更多