繁体   English   中英

Spring Boot Camel 测试 - 端点上没有可用的消费者

[英]Spring Boot Camel Testing - No consumers available on endpoint

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

路由生成器代码:

@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

问题是在运行测试之前没有初始化骆驼上下文。 如果在运行测试之前为骆驼上下文添加延迟以准备就绪,错误就会消失。 我将以下代码添加到测试类中,以便为骆驼上下文提供 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");
    }
}

我也不得不花很多时间来解决这个问题。 事实证明,我的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!");
  }
}

控制台输出:

你好!

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

暂无
暂无

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

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