拆分消息并在骆驼中路由它们

发布于 2024-12-10 23:13:15 字数 645 浏览 0 评论 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。 我发现原因是分离器删除了标头,因为它位于路由之前。

看来我不能在路由后使用分割。

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

I have a large XML message read from queue, I need to split it in chunks and convert it into objects and then route them to various destinations based on the object.

So I have configured the routeBuilder to

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());

But the routing is not happening at all, All messages are sent to the errorEndpoint.
I found the reason to be the splitter deleting the header, as its ahead of the routing.

It seems I cannot use splitting after routing.

What is the solution to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2024-12-17 23:13:15

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;
    }
}

split() shouldn't remove the headers...are you sure your xmlSplitter/xmlProcessor aren't causing issues?

here is a simple example to show that the headers are preserved...

@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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文