从处理器

时间:2016-06-02 14:24:28

标签: java apache-camel

我正在使用Camel集成2个系统。我已经定义了不同的路由,其中​​一条路由从特定的rabbitmq队列中消耗并将其发送到REST服务。没有什么好看的,路线看起来像这样:

public class WebSurfingRabbitToRestRoute extends RouteBuilder{
    @Override
    public void configure() throws Exception {
        from("rabbitmq://rabbit_host:port/Rabbit_Exchange").
        setHeader("CamelHttpMethod", constant("POST")).
        setHeader("Content-Type", constant("application/json")).
        bean(TransformResponse.class, "transform").
        to("http4://rest_service_host:port/MyRestService).
    }
}

正如您所看到的,我会在将每条消息发送到休息服务之前对其进行处理,因为我需要调整一些内容。当我发现有时(我不知道如何或何时),发布到兔子的系统,发送2个连接的消息时,问题就来了。

我期望得到的是一个像这样简单的json:

[{field1:value1, field2:value2}]

我有时得到的是:

[{field1:value1, field2:value2},{field1:value3, field2:value4}]

因此,当我面对这种情况时,将消息路由到的其余服务失败(显然)。

为了解决这个问题,我想知道是否有办法从处理器内部调用路由。从前面的代码片段中你可以看到我调用了transform方法,所以想法会做类似下面的伪代码,因为在路由被触发后,我不能拆分事件并将它们发送到相同的路线“实例”,所以我考虑调用一个不同的路线,我可以从这里调用,它将发送消息2到同一个休息服务。

public class TransformRabbitmqResponse {
    public String transform(String body) throws Exception {
    // In here i do stuff with the message
    // Check if i got 2 messages concatenated
    // if body.contains("},{") {
    //    split_messages
    //    InvokeDifferentRoute(message2)
    //}
}
}

你们认为这是可能的吗?

1 个答案:

答案 0 :(得分:0)

一个选项(虽然我不确定这是最佳选项)是使用The thread 0xc14 has exited with code 259 (0x103). The thread 0xb7c has exited with code 259 (0x103). The thread 0xd40 has exited with code 259 (0x103). 'SAMP_Worker.vshost.exe' (CLR v4.0.30319: SAMP_Worker.vshost.exe): Loaded 'C:\Users\Dmetrey\documents\visual studio 2013\Projects\SAMP_Worker\SAMP_Worker\bin\Debug\SAMP_Worker.exe'. Symbols loaded. A first chance exception of type 'System.IndexOutOfRangeException' occurred in SAMP_Worker.exe 'SAMP_Worker.vshost.exe' (CLR v4.0.30319: SAMP_Worker.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. The program '[6120] SAMP_Worker.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'. 端点将其拆分为两个不同的路由。

direct

然后在您的转换bean中,您可以使用camel Producer Template将转换后的有效负载发布到新的直接端点(假设您使用的是json?)。

public class WebSurfingRabbitToRestRoute extends RouteBuilder{
    @Override
    public void configure() throws Exception {
        from("rabbitmq://rabbit_host:port/Rabbit_Exchange")
        .setHeader("CamelHttpMethod", constant("POST"))
        .setHeader("Content-Type", constant("application/json"))
        .bean(TransformResponse.class, "transform");

        from("direct:transformedResponses")
        .to("http4://rest_service_host:port/MyRestService");
    }
}