如何使用camel recipentList

der*_*itz 3 apache-camel

我只是试图让一个使用RecipentList的骆驼路线工作.但首先是一个问题:两者之间有什么区别

  • Multicast/RecipentList(两者都没有并行处理)
  • 多个"到"?

在我的情况下,我希望我的一些路线并行处理.目前所有人都使用多个

在for循环中添加"to":

RouteDefinition someRoute = createSomeRout(fromPart, id); \\ method
for (String pcrfTarget : cepConfig.pcrfCepTargets()) {
    log.info("...to " + pcrfTarget);
    someRoute.to(pcrfTarget + "?mode=" + Mode.insertAddId.name());
}
Run Code Online (Sandbox Code Playgroud)

是否有直接的方法来使用recipientList并在最后添加parallelProcessing?我也尝试创建一个简单的例子,但它失败了(书籍和互联网中唯一的例子就是使用/操作标题:-().这是我的例子(错误):

public class Experiments extends CamelTestSupport {
    private static final String MOCK2 = "mock:mock2";
    private static final String MOCK1 = "mock:mock1";
    private static String PCRF_TEST_FILES;

    public Experiments() throws URISyntaxException {
        PCRF_TEST_FILES = ClassLoader.getSystemResource("pcrf-files").toURI().toString();
    }

    @Test
    public void test() throws InterruptedException {
        MockEndpoint mockIn = getMockEndpoint(MOCK1);
        MockEndpoint mockOut = getMockEndpoint(MOCK2);
        mockIn.expectedMessageCount(5);
        mockOut.expectedMessageCount(5);
        // data in mock are available after this call
        assertMockEndpointsSatisfied();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.camel.test.junit4.CamelTestSupport#createRouteBuilder()
     */
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                //from(PCRF_TEST_FILES + "?noop=true").unmarshal().gzip().to(MOCK1).to(MOCK2); //working
                from(PCRF_TEST_FILES + "?noop=true").unmarshal().gzip().recipientList(method(Experiments.class)).parallelProcessing(); //not working
            }
        };
    }

    @RecipientList
    public String[] recipents() {
        return new String[] { MOCK1, MOCK2 };
    }

}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[EDR_UPCC244_MPU842_0370_20140428000008.csv.gz]
...
Caused by: java.lang.AssertionError: expected null, but was:<[B@48d19957>
Run Code Online (Sandbox Code Playgroud)

我认为由于某种原因,camel尝试使用文件内容来获取收件人?!有人可以提供一个示例,如何动态创建recipentList,但不是基于交换附带的数据,而是基于独立数据(在我的情况下在配置中给出).

谢谢

Pet*_*ler 5

混合@RecipientList并且recipientList()不可能如Camel文档中所述("使用方法调用作为收件人列表"部分).因此,只需使用其中一个.

1.使用recipientList()

@RecipientList从方法中删除:

// No @RecipientList annotation
public String[] recipents() {
    return new String[] { MOCK1, MOCK2 };
}
Run Code Online (Sandbox Code Playgroud)

并按如下方式定义路线:

from("direct:start")
    .recipientList()
    .method(Experiments.class)  // you may define a method name as well if there is more than one
    .parallelProcessing();
Run Code Online (Sandbox Code Playgroud)

要么:

from("direct:start")
    .recipientList(method(Experiments.class))  // you may define a method name as well if there is more than one
    .parallelProcessing();
Run Code Online (Sandbox Code Playgroud)

2.使用@RecipientList

@RecipientListbean以下一起使用:

from("direct:start")
    .bean(Experiments.class);  // you may define a method name as well if there is more than one
Run Code Online (Sandbox Code Playgroud)

要实现并行处理,需要将该parallelProcessing属性添加到@RecipientList注释:

@RecipientList(parallelProcessing = true)
public String[] recipents() {
    return new String[] { MOCK1, MOCK2 };
}
Run Code Online (Sandbox Code Playgroud)