我在Spring Boot,Camel应用程序中测试spring security。当我尝试使用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
答案 0 :(得分:1)
问题是在运行测试之前没有初始化camel上下文。如果在运行测试之前为驼峰上下文添加延迟以使其准备就绪,则错误消失。我将以下代码添加到测试类中,以便为驼峰上下文提供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");
}
}
答案 1 :(得分:0)
我也不得不花很多时间来解决这个问题。原来我的RouteBuilder
并没有添加到CamelContext
中,因为我在@Component
类上没有RouteBuilder
注释。
我看到您在@Component
(RouteBuilder
)上包含了必要的SpringSecurityRoute
注释,但是我无法从您的帖子中得知您的{{1 }}通过组件扫描或其他某种方式添加到Spring上下文中。一种确定的方法是,在您像这样的SpringSecurityRoute
上调用#sendBody之前,先查看路由是否已添加到您的上下文中:
ProducerTemplate
如果不在此处,则 会导致帖子标题中提到“端点上没有可用的使用者”错误。因此,请确保对包含您的路线的包裹进行组件扫描。
这是我如何将我联系在一起的方法:
路线构建器:
@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");
}
Spring Java配置:
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");
}
}
我将其包含在我的单元测试类中,如下所示:
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 {
}
你好!
答案 2 :(得分:0)
在Spring Boot应用程序中测试骆驼时,还有其他跑步者可以使用。看看https://camel.apache.org/manual/latest/spring-testing.html可以使您指出正确的方向。