Java apachecamel中的模拟输入队列
您好,我有一个使用ApacheCamel的应用程序和作为处理起点的输入队列。我正试图找到一种很好的方法来模拟这个输入队列,以便:Java apachecamel中的模拟输入队列,java,apache-camel,Java,Apache Camel,您好,我有一个使用ApacheCamel的应用程序和作为处理起点的输入队列。我正试图找到一种很好的方法来模拟这个输入队列,以便: 我重用生产路由文件,我不想复制和粘贴内容,只对队列的路由进行一次更改 我可以将消息发送到这个“模拟”队列,并像在生产中一样进行处理 这可能是关于将“queue:”更改为“direct:”路由,但除了指定另一个xml之外,我找不到任何其他方法。您可以在测试期间使用方法拦截消息: public class MySuperTest extends CamelTestSu
- 我重用生产路由文件,我不想复制和粘贴内容,只对队列的路由进行一次更改
- 我可以将消息发送到这个“模拟”队列,并像在生产中一样进行处理
public class MySuperTest extends CamelTestSupport {
public void testAdvised() throws Exception {
// advice the first route using the inlined route builder
context.getRouteDefinitions().get(0).adviceWith(context, new RouteBuilder() {
@Override
public void configure() throws Exception {
// intercept sending to mock:foo and do something else
interceptSendToEndpoint("mock:foo")
.skipSendToOriginalEndpoint()
.to("log:foo")
.to("mock:advised");
}
});
getMockEndpoint("mock:foo").expectedMessageCount(0);
getMockEndpoint("mock:advised").expectedMessageCount(1);
getMockEndpoint("mock:result").expectedMessageCount(1);
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
//TODO build your route here
from("direct:start").process(...).to("mock:result");
}
};
}
}
您可以使用方法在测试期间拦截消息:
public class MySuperTest extends CamelTestSupport {
public void testAdvised() throws Exception {
// advice the first route using the inlined route builder
context.getRouteDefinitions().get(0).adviceWith(context, new RouteBuilder() {
@Override
public void configure() throws Exception {
// intercept sending to mock:foo and do something else
interceptSendToEndpoint("mock:foo")
.skipSendToOriginalEndpoint()
.to("log:foo")
.to("mock:advised");
}
});
getMockEndpoint("mock:foo").expectedMessageCount(0);
getMockEndpoint("mock:advised").expectedMessageCount(1);
getMockEndpoint("mock:result").expectedMessageCount(1);
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
//TODO build your route here
from("direct:start").process(...).to("mock:result");
}
};
}
}
我创建了这样的类
import org.apache.camel.CamelContext;
public class JmsToSedaComponent {
private CamelContext camelContext;
public JmsToSedaComponent(CamelContext camelContext) {
this.camelContext = camelContext;
}
public void init() {
camelContext.removeComponent("jms");
camelContext.addComponent("jms", camelContext.getComponent("seda"));
}
}
然后在spring xml文件中:
<bean class="com.lmig.ci.baods.dial.integration.JmsToSedaComponent" init-method="init">
<constructor-arg ref="camelContext"/>
</bean>
这会将所有JMS组件替换为SEDA 我创建了这样的类
import org.apache.camel.CamelContext;
public class JmsToSedaComponent {
private CamelContext camelContext;
public JmsToSedaComponent(CamelContext camelContext) {
this.camelContext = camelContext;
}
public void init() {
camelContext.removeComponent("jms");
camelContext.addComponent("jms", camelContext.getComponent("seda"));
}
}
然后在spring xml文件中:
<bean class="com.lmig.ci.baods.dial.integration.JmsToSedaComponent" init-method="init">
<constructor-arg ref="camelContext"/>
</bean>
这会将所有JMS组件替换为SEDA 您可以建议一条路线来替换来自-组件的
,例如,将amq
端点替换为直接
端点。然后可以使用生产者模板在测试中触发路由
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/your-context.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("none")
@UseAdviceWith
public class ReplaceFromTest {
@Autowired
protected CamelContext context;
@Produce(context = "your-camel-context-id")
protected ProducerTemplate template;
@Before
public void setUp() throws Exception {
AdviceWithRouteBuilder mockAmq = new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith("direct:amq-mock");
}
};
((ModelCamelContext) context).getRouteDefinition("route_to_advise").adviceWith((ModelCamelContext) context, mockAmq);
context.start();
}
@After
public void tearDown() throws Exception {
context.stop();
}
@DirtiesContext
@Test
public void sendMessageTest() {
Map<String, Object> myHeaders = new HashMap<>();
String myBody = "Some content";
template.sendBodyAndHeaders("direct://amq-mock", myBody, myHeaders);
// Verify the results
}
}
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(位置={/META-INF/spring/your context.xml“})
@DirtiesContext(classMode=classMode.AFTER\u每个\u测试\u方法)
@模拟端点(“无”)
@使用建议
公共类替换测试{
@自动连线
受保护的上下文;
@生成(context=“您的骆驼上下文id”)
受保护的产品模板;
@以前
public void setUp()引发异常{
AdviceWithRouteBuilder mockAmq=新AdviceWithRouteBuilder(){
@凌驾
public void configure()引发异常{
替换为(“直接:amq模拟”);
}
};
((ModelCamelContext)context).GetRoutedDefinition(“路由到建议”).adviceWith((ModelCamelContext)context,mockAmq);
context.start();
}
@之后
public void tearDown()引发异常{
context.stop();
}
@肮脏的环境
@试验
public void sendMessageTest(){
Map myHeaders=newhashmap();
String myBody=“某些内容”;
template.sendBodyAndHeaders(“direct://amq-mock“,myBody,MyHeader);
//验证结果
}
}
HTH.您可以建议一条路线,以从
-组件替换,例如,将amq
端点替换为直接
端点。然后可以使用生产者模板在测试中触发路由
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/your-context.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("none")
@UseAdviceWith
public class ReplaceFromTest {
@Autowired
protected CamelContext context;
@Produce(context = "your-camel-context-id")
protected ProducerTemplate template;
@Before
public void setUp() throws Exception {
AdviceWithRouteBuilder mockAmq = new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith("direct:amq-mock");
}
};
((ModelCamelContext) context).getRouteDefinition("route_to_advise").adviceWith((ModelCamelContext) context, mockAmq);
context.start();
}
@After
public void tearDown() throws Exception {
context.stop();
}
@DirtiesContext
@Test
public void sendMessageTest() {
Map<String, Object> myHeaders = new HashMap<>();
String myBody = "Some content";
template.sendBodyAndHeaders("direct://amq-mock", myBody, myHeaders);
// Verify the results
}
}
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(位置={/META-INF/spring/your context.xml“})
@DirtiesContext(classMode=classMode.AFTER\u每个\u测试\u方法)
@模拟端点(“无”)
@使用建议
公共类替换测试{
@自动连线
受保护的上下文;
@生成(context=“您的骆驼上下文id”)
受保护的产品模板;
@以前
public void setUp()引发异常{
AdviceWithRouteBuilder mockAmq=新AdviceWithRouteBuilder(){
@凌驾
public void configure()引发异常{
替换为(“直接:amq模拟”);
}
};
((ModelCamelContext)context).GetRoutedDefinition(“路由到建议”).adviceWith((ModelCamelContext)context,mockAmq);
context.start();
}
@之后
public void tearDown()引发异常{
context.stop();
}
@肮脏的环境
@试验
public void sendMessageTest(){
Map myHeaders=newhashmap();
String myBody=“某些内容”;
template.sendBodyAndHeaders(“direct://amq-mock“,myBody,MyHeader);
//验证结果
}
}
HTH.我发现了这一点:您可以将specity“queue:productuion_queue”作为属性,${start.endpoint}
并为您的测试指定start.endpoint=direct:xyz
我发现:您可以将specity“queue:productuion_queue”作为属性,${start.endpoint}
并为您的测试指定start.endpoint=direct:xyz
如何将direct:start映射到mock:foo?我以前发现过这个例子,但不幸的是我不清楚。@czajek我又添加了一些代码。可能无法运行,我从内存中添加了这个,但没有运行它。它显示了junit测试中的CamelTestSupport
和RouteBuilder
的使用。我想我还是不明白。据我所知,这有助于模拟“最终”端点,这是我通过使用org.apache.camel.impl.InterceptSendToMockEndpointStrategy已经实现的。从我在这里看到的,我仍然有来自“输入”端点的处理,这是一个队列。当队列中有一些消息时,会发现一些意外的行为。如果队列位于处理的中间位置,则上述解决方案可以工作。@ CZAJEK可以从任何端点截取。这个答案能解决您的问题吗?如何将direct:start映射发送到mock:foo?我以前发现过这个例子,但不幸的是我不清楚。@czajek我又添加了一些代码。可能无法运行,我从内存中添加了这个,但没有运行它。它显示了junit测试中的CamelTestSupport
和RouteBuilder
的使用。我想我还是不明白。据我所知,这有助于模拟“最终”端点,这是我通过使用org.apache.camel.impl.InterceptSendToMockEndpointStrategy已经实现的。从我在这里看到的,我仍然有来自“输入”端点的处理,这是一个队列。当队列中有一些消息时,会发现一些意外的行为。如果队列位于处理的中间位置,则上述解决方案可以工作。@ CZAJEK可以从任何端点截取。这个答案能解决你的问题吗?