如何使用camel recipentList

时间:2014-06-27 12:22:37

标签: 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());
}

是否有直接的方法来使用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 };
    }

}

我收到错误:

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>

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

由于

1 个答案:

答案 0 :(得分:5)

Camel documentation中所述,无法混合@RecipientListrecipientList()(&#34;使用方法调用作为收件人列表&#34;)。因此,只需使用其中一种。

<强> 1。使用recipientList()

从方法中删除@RecipientList

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

按如下方式定义路线:

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

或者:

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

<强> 2。使用@RecipientList

@RecipientListbean一起使用:

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

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

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