【问题标题】:Spring Boot Camel Testing - No consumers available on endpointSpring Boot Camel 测试 - 端点上没有可用的消费者
【发布时间】:2017-06-03 02:57:19
【问题描述】:

我正在我的 Spring Boot、Camel 应用程序中测试 spring 安全性。当我尝试使用 Autowired ProducerTemplate 测试路由时,会引发异常并显示消息“端点上没有可用的消费者:direct://secure”。路线在骆驼上下文中正确定义。

RouteBuilder 代码:

@Component
public class SpringSecurityRoute extends RouteBuilder {
    protected Logger logger = LoggerFactory.getLogger(SpringSecurityRoute.class);

    @Override
    public void configure() throws Exception {

        from("direct:secure")
            .process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    logger.info("********************* Processing!");
                }
            });
    }
}

测试类代码:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringSecurityRouteTest {

    private static Logger logger = LoggerFactory.getLogger(SpringSecurityRouteTest.class);

    @Autowired
    ProducerTemplate producerTemplate;

    @Test
    public void testRoute() {
        logger.info("Starting test.");
        producerTemplate.sendBody("direct:secure", "body");
    }
}

堆栈跟踪:

org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[ID-xxxxxxxxxxxxxxx-51669-1484755486463-0-2]

    at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1706)
    at org.apache.camel.util.ExchangeHelper.extractResultBody(ExchangeHelper.java:677)
    at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:515)
    at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:511)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:163)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:168)
    at com.incomm.imp.neo.routes.SpringSecurityRouteTest.testRoute(SpringSecurityRouteTest.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: direct://secure. Exchange[ID-xxxxxxxxxxxxx-51669-1484755486463-0-2]
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:55)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197)
    at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97)
    at org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:529)
    at org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:497)
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:365)
    at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:497)
    at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:225)
    at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:144)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:161)
    ... 35 more

【问题讨论】:

  • 您可能需要注意,这不是单元测试,因为它依赖于集成多个组件,因此您可能需要更改方法并使其成为集成测试。相应地改变你的观点可能有助于解决这些初始化问题。
  • 我只是将上述内容作为评论提供,因为它不能直接帮助您解决此问题。这是一个老问题,所以我向后代陈述我的建议。

标签: spring-boot apache-camel


【解决方案1】:

我也不得不花费大量时间来解决这个问题。原来我的RouteBuilder 没有被添加到CamelContext,因为我在RouteBuilder 类上没有@Component 注释。

我看到您在您的RouteBuilder (SpringSecurityRoute) 中包含了必要的@Component 注释,但我无法从您的帖子中看出您的SpringSecurityRoute 是否被添加到 Spring使用组件扫描或其他类似方式的上下文。确定的一种方法是在您调用ProducerTemplate 上的#sendBody 之前查看路由是否已添加到您的上下文中,如下所示:

@Test
public void testRoute() {

  logger.info("Starting test.");

  List<Route> routes = producerTemplate.getCamelContext().getRoutes();
  // inspect list of routes in your debugger and see if your route is
  // present within the camel context.

  producerTemplate.sendBody("direct:secure", "body");
}

如果它在那里,它导致您的帖子标题中提到的“端点上没有可用的消费者”错误。因此,请确保您正在对包含您的路由的包进行组件扫描。

以下是我的接线方式:

路线建设者:

package com.example.camel.routes;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class HelloCamelRoute extends RouteBuilder {

  public void configure() {
    from("direct:sayHello").to("stream:out");
  }
}

Spring Java 配置:

package com.example.camel.routes;

import org.apache.camel.spring.javaconfig.CamelConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.example.camel.routes")
public class CamelConfig extends CamelConfiguration {
}

我将它包含在我的单元测试类中,如下所示:

package com.example.camel.routes;
    
import org.apache.camel.CamelContext;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.Route;
import org.apache.camel.test.spring.CamelSpringRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
    
import java.util.List;

@RunWith(CamelSpringRunner.class)
@ContextConfiguration(classes = CamelConfig.class)
public class HelloCamelRouteIT {
    
  @Autowired
  protected CamelContext camelContext;
    
  @Produce
  protected ProducerTemplate producerTemplate;
    
  @Test
  public void doit() {
    producerTemplate.sendBody("direct:sayHello", "Hello!");
  }
}

控制台输出:

你好!

【讨论】:

  • 我赞成您的回答,因为这是一种不错的方法。但这些是集成测试,所以我对它们的唯一问题是它们没有被指定为集成测试(例如,在测试类名称后加上“IT”后缀等),因此视角可能不适合处理使用 Spring 和 Camel 等进行初始化的复杂性。
  • 感谢@SteveStorck 的支持和反馈。那只是我错过的一个细节。我在帖子中进行了更新。
【解决方案2】:

问题是在测试运行之前没有初始化骆驼上下文。如果在运行测试之前为骆驼上下文准备就绪添加了延迟,则错误就会消失。我将以下代码添加到测试类中,以便为骆驼上下文提供 5 秒的初始化时间。不再抛出异常。我注意到,如果没有增加延迟,则不会打印表明路由已准备好的日志语句。

11:23:11.440 [CamelMainRunController] INFO  o.a.camel.spring.SpringCamelContext - Route: route3 started and consuming from: direct://secure  

添加到测试类的新代码:

private boolean isCamelContextInitialized = false;

@Before
public void initializeCamelContext() throws Exception {
    if (!isCamelContextInitialized) {
        logger.info("Waiting for Camel Context to become initialized.");
        Thread.sleep(5000L);
    }
}

更新了测试类代码:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringSecurityRouteTest {

    private static Logger logger = LoggerFactory.getLogger(SpringSecurityRouteTest.class);
    private boolean isCamelContextInitialized = false;

    @Before
    public void initializeCamelContext() throws Exception {
        if (!isCamelContextInitialized) {
            logger.info("Waiting for Camel Context to become initialized.");
            Thread.sleep(5000L);
        }
    }

    @Autowired
    ProducerTemplate producerTemplate;

    @Test
    public void testRoute() {
        logger.info("Starting test.");
        producerTemplate.sendBody("direct:secure", "body");
    }
}

【讨论】:

    【解决方案3】:

    当您在 Spring Boot 应用程序中测试骆驼时,还可以使用其他跑步者。看看https://camel.apache.org/manual/latest/spring-testing.html,让你找到正确的方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-11
      • 2016-12-09
      • 2012-11-05
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多