gpt4 book ai didi

java - Apache Camel 。 InterceptSendTo 不适用于 bean 端点

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:57 25 4
gpt4 key购买 nike

我不明白为什么我的测试不起作用。我尝试拦截并跳过发送到 bean 引用的端点,但没有任何反应。我使用的是2.16.2版本。

测试-camel.xml

<bean id="eb" class="com.rencap.emf.bpipe.EndpointBean"/>    
<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
<camel:endpoint id="requestEP" uri="direct:request"/>
<endpoint id="beanEP" uri="bean:eb?method=processMessage" />
<camel:route id="testRoute">
<camel:from ref="requestEP"/>
<camel:to ref="beanEP" />
</camel:route>
</camel:camelContext>

EndpointBean.java

package com.rencap.emf.bpipe;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EndpointBean {
private static Logger LOG = LoggerFactory.getLogger(EndpointBean.class);

public void processMessage( String msg ){
LOG.info("Processing message: {} ",msg);
}
}

单元测试:

@EndpointInject(ref="requestEP")
ProducerTemplate requestEP;
@EndpointInject(ref="beanEP")
ProducerTemplate beanEP;
@Autowired
ModelCamelContext camelContext;
@Test
public void test() throws Exception{

camelContext.getRouteDefinition("testRoute").adviceWith( camelContext , new AdviceWithRouteBuilder(){
@Override
public void configure() throws Exception {
interceptSendToEndpoint( beanEP.getDefaultEndpoint().getEndpointUri() ).
to("mock:send").
skipSendToOriginalEndpoint();
}
});
TestUtils.waitingFor("Configuration applied", 2000);

MockEndpoint mockEP = camelContext.getEndpoint("mock:send",MockEndpoint.class);
mockEP.setExpectedCount( 1 );

requestEP.sendBody("Message");

mockEP.assertIsSatisfied();

TestUtils.waitingFor("All rows commited", 2000);
}

测试总是失败。日志:

13:11:02.512 [main] INFO  o.apache.camel.model.RouteDefinition - AdviceWith route after: Route(testRoute)[[From[ref:requestEP]] -> [InterceptSendToEndpoint[bean://eb?method=processMessage -> [To[mock:send]]], To[ref:beanEP]]]
13:11:02.537 [main] INFO o.a.camel.spring.SpringCamelContext - Route: testRoute started and consuming from: Endpoint[direct://request]
13:11:02.538 [main] INFO com.rencap.emf.bpipe.test.TestUtils - Wait 2000 ms. Configuration applied
13:11:04.554 [main] INFO com.rencap.emf.bpipe.EndpointBean - Processing message: Message
13:11:04.556 [main] INFO o.a.c.component.mock.MockEndpoint - Asserting: Endpoint[mock://send] is satisfied

这意味着发送到端点的消息不会被拦截和跳过。可能是我不明白一些东西,但我找不到使用此方法的任何限制。

此外,我注意到日志端点也存在同样的问题。如果我将 beanEP 替换为:

<endpoint id="beanEP" uri="log:LOGMESSAGE" />

我收到了相同的结果。但如果我把它替换为

<endpoint id="beanEP" uri="seda:send" />

并添加新路线:

<camel:route id="route2">
<camel:from ref="sendEP"/>
<camel:log message="msg received ${body}"/>
</camel:route>

我会得到预期的结果并且测试会成功。

我做错了什么?或者也许这个方法有一些限制?

最佳答案

有人建议我重写 isUseAdviceWith。就我而言,它看起来像:

public class TestTest extends  CamelSpringTestSupport {
@Test
public void test() throws Exception{
Endpoint beanEP = context.getEndpoint("beanEP");
Endpoint requestEP = context.getEndpoint("requestEP");
context.getRouteDefinition("testRoute").adviceWith( context , new AdviceWithRouteBuilder(){
@Override
public void configure() throws Exception {
interceptSendToEndpoint( beanEP.getEndpointUri() ).
to("mock:send").
skipSendToOriginalEndpoint();
}
});
context.start();
TestUtils.waitingFor("Configuration applied", 2000);
MockEndpoint mockEP = context.getEndpoint("mock:send",MockEndpoint.class);
mockEP.setExpectedCount( 1 );
context.createProducerTemplate().sendBody(requestEP, "Message");
mockEP.assertIsSatisfied();
TestUtils.waitingFor("All rows commited", 2000);
}
@Override
protected AbstractApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("test-camel.xml");
}
@Override
public boolean isUseAdviceWith(){
return true;
}
}

它有效。

但是我想使用注释,但是 this现在对我来说还不起作用。

关于java - Apache Camel 。 InterceptSendTo 不适用于 bean 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36128703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com