繁体   English   中英

Apache camel 2.16丰富-JUnit中的终结点上没有可用的使用者

[英]Apache camel 2.16 enrich - No consumers available on endpoint in JUnit

我升级到骆驼2.16,并且我的一项路线单元测试开始失败。

这是我的路线定义:

public class Route extends RouteBuilder{

    @Override
    public void configure() throws Exception {

        from(start).enrich("second");

        from("direct:second")
          .log(LoggingLevel.DEBUG, "foo", "Route [direct:second] started.");

    }
}

这是我的测试:

@RunWith(MockitoJUnitRunner.class)
public class RouteTest extends CamelTestSupport  {

    private Route builder;

    @Produce(uri = "direct:start")
    protected ProducerTemplate template;

    @Before
    public void config() {
        BasicConfigurator.configure();
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        builder = new Route();
        return builder;
    }

    @Override
    protected CamelContext createCamelContext() throws Exception {
        SimpleRegistry registry = new SimpleRegistry();
        return new DefaultCamelContext(registry);
    }

    @Test
    public void testPrimeRouteForSubscriptionId() {
        Exchange exchange = ExchangeBuilder.anExchange(new DefaultCamelContext()).build();
        exchange.getIn().setBody(new String("test"));
        template.send(exchange);
    }
}

运行测试时出现的错误是:

org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: Endpoint[direct://second]. Exchange[][Message: test]

值得注意的是骆驼2.16注释中的以下行: http : //camel.apache.org/camel-2160-release.html

和上的resourceUri和resourceRef属性已被删除,因为它们现在支持根据表达式计算的动态uri。

在此先感谢您的帮助。

交换订单,以便在浓缩之前开始直接路线。 http://camel.apache.org/configuring-route-startup-ordering-and-autostartup.html

或在您的单元测试中使用seda代替直接使用: http//camel.apache.org/seda

或者在直接uri中使用?block=true来告诉Camel阻止并等待消费者启动并准备就绪,然后再向其发送消息: http : //camel.apache.org/direct

蓝图测试不断抛出异常,没有可用的使用者。

我的情况是我有一个osgi svc ,它公开了可以从任何其他osgi svc调用的方法。

因此,公开的svc方法调用了直接方法:

@EndpointInject(uri = "direct-vm:toRestCall")
    ProducerTemplate toRestCall;

svcMethod(Exchange xch){
exchange.setOut(
        toRestCall.send("seda:toDirectCall", xch -> {
            try{
                xch.getIn().setBody("abc");
            }catch (Exception ex){
                ex.getMessage();
            }
          }
        }).getIn());

当我测试它所调用的直接指令时,带有JUnit Blueprint建议常会JUnit以下异常:

org.apache.camel.component.direct.DirectConsumerNotAvailableException:在终结点:端点上没有可用的使用者。 Exchange [消息:{..........

这是一个有点老的问题,但是由于我昨晚抽出了大部分头发,试图弄清楚为什么可以使用to("direct:myEndpoint")而不是enrich("direct:myEndpoint") ,所以我无论如何都会发布答案-也许它将使其他人免于秃头;-)

事实证明这是一个测试问题。 如果是Direct终结点,则rich将在将Exchange传递给上下文之前检查上下文中是否存在正在运行的路由,但这是通过查看当前正在处理的Exchange所拥有的CamelContext来进行的。 自从您的ProducerTemplate通过Exchange传递之后,使用new DefaultCamelContext()创建的对象就没有可用的“ direct:second”路由。

幸运的是,有两个简单的解决方案。 使用来自CamelTestSupport的CamelContext创建Exchange,或者改为使用ProducerTemplate sendBody(...)方法:

@Test
public void testWithSendBody() {
    template.sendBody(new String("test"));
}

@Test
public void testPrimeRouteForSubscriptionId() {
    Exchange exchange = ExchangeBuilder.anExchange(context()).build();
    exchange.getIn().setBody(new String("test"));
    template.send(exchange);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM