阿帕奇骆驼。 InterceptSendTo不适用于bean端点

时间:2016-03-21 10:34:51

标签: java apache-camel

我无法理解为什么我的测试不起作用。我尝试拦截并跳过发送到端点,这是bean引用,没有任何反应。 我使用的是2.16.2版。

测试camel.xml

<bean id="eb" class="com.rencap.emf.bpipe.EndpointBean"/>    
<camel:camelContext xmlns="http://camel.apache.org/schema/spring">  
    <camel:endpoint id="requestEP" uri="direct:request"/>
    <endpoint id="beanEP" uri="bean:eb?method=processMessage" />
    <camel:route id="testRoute">
        <camel:from ref="requestEP"/>
        <camel:to ref="beanEP" />
    </camel:route>
</camel:camelContext>

EndpointBean.java

package com.rencap.emf.bpipe;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EndpointBean {
    private static Logger LOG = LoggerFactory.getLogger(EndpointBean.class);

    public void processMessage( String msg ){
        LOG.info("Processing message: {} ",msg);
    }
}

单元测试:

@EndpointInject(ref="requestEP")
ProducerTemplate requestEP;
@EndpointInject(ref="beanEP")
ProducerTemplate beanEP;
@Autowired
ModelCamelContext camelContext; 
@Test
public void test() throws Exception{

    camelContext.getRouteDefinition("testRoute").adviceWith( camelContext , new AdviceWithRouteBuilder(){
        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint( beanEP.getDefaultEndpoint().getEndpointUri() ).
                to("mock:send").
                skipSendToOriginalEndpoint();
        }
    });     
    TestUtils.waitingFor("Configuration applied", 2000);

    MockEndpoint mockEP = camelContext.getEndpoint("mock:send",MockEndpoint.class);
    mockEP.setExpectedCount( 1 );

    requestEP.sendBody("Message");

    mockEP.assertIsSatisfied();

    TestUtils.waitingFor("All rows commited", 2000);
}

测试总是失败。 日志:

13:11:02.512 [main] INFO  o.apache.camel.model.RouteDefinition - AdviceWith route after: Route(testRoute)[[From[ref:requestEP]] -> [InterceptSendToEndpoint[bean://eb?method=processMessage -> [To[mock:send]]], To[ref:beanEP]]]
13:11:02.537 [main] INFO  o.a.camel.spring.SpringCamelContext - Route: testRoute started and consuming from: Endpoint[direct://request]
13:11:02.538 [main] INFO  com.rencap.emf.bpipe.test.TestUtils - Wait 2000 ms. Configuration applied 
13:11:04.554 [main] INFO  com.rencap.emf.bpipe.EndpointBean - Processing message: Message 
13:11:04.556 [main] INFO  o.a.c.component.mock.MockEndpoint - Asserting: Endpoint[mock://send] is satisfied

这意味着发送到端点不会被拦截和跳过。可能是我不明白,但我没有找到使用这种方法的任何限制。

另外,我注意到带有日志的端点存在同样的问题。如果我替换beanEP:

<endpoint id="beanEP" uri="log:LOGMESSAGE" />

我得到了相同的结果。 但如果我在

上更换它
<endpoint id="beanEP" uri="seda:send" />

并添加新路线:

<camel:route id="route2">
        <camel:from ref="sendEP"/>
        <camel:log message="msg received ${body}"/>
    </camel:route>

我将获得预期的结果和测试将成功。

我做错了什么?或许这个方法有一些限制吗?

1 个答案:

答案 0 :(得分:0)

我被建议覆盖isUseAdviceWith。就我而言,它看起来像:

public class TestTest extends  CamelSpringTestSupport {
    @Test
    public void test() throws Exception{
        Endpoint beanEP    = context.getEndpoint("beanEP");
        Endpoint requestEP = context.getEndpoint("requestEP");
        context.getRouteDefinition("testRoute").adviceWith( context , new AdviceWithRouteBuilder(){
            @Override
            public void configure() throws Exception {
                interceptSendToEndpoint( beanEP.getEndpointUri() ).
                    to("mock:send").
                    skipSendToOriginalEndpoint();
            }
        });    
        context.start();
        TestUtils.waitingFor("Configuration applied", 2000);
        MockEndpoint mockEP = context.getEndpoint("mock:send",MockEndpoint.class);
        mockEP.setExpectedCount( 1 );
        context.createProducerTemplate().sendBody(requestEP, "Message");
        mockEP.assertIsSatisfied();
        TestUtils.waitingFor("All rows commited", 2000);
    }
    @Override
    protected AbstractApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext("test-camel.xml");
    }
    @Override
    public boolean isUseAdviceWith(){
        return true;
    }
}

有效。

但是我想使用注释,但this现在还没有为我工作。