Ali*_*Ali 5 apache-camel spring-boot
我正在我的 Spring Boot、Camel 应用程序中测试 spring security。当我尝试使用 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!");
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
测试类代码:
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
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
Run Code Online (Sandbox Code Playgroud)
问题是在测试运行之前骆驼上下文没有初始化。如果在运行测试之前添加延迟以使骆驼上下文准备就绪,则错误就会消失。我将以下代码添加到测试类中,以便为骆驼上下文提供 5 秒的初始化时间。不再抛出异常。我注意到,如果没有增加延迟,就不会打印任何表明路由已准备就绪的日志语句。
11:23:11.440 [CamelMainRunController] INFO o.a.camel.spring.SpringCamelContext - Route: route3 started and consuming from: direct://secure
Run Code Online (Sandbox Code Playgroud)
添加到测试类的新代码:
private boolean isCamelContextInitialized = false;
@Before
public void initializeCamelContext() throws Exception {
if (!isCamelContextInitialized) {
logger.info("Waiting for Camel Context to become initialized.");
Thread.sleep(5000L);
}
}
Run Code Online (Sandbox Code Playgroud)
更新了测试类代码:
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6067 次 |
| 最近记录: |