在apache camel中模拟输入队列

时间:2015-01-15 09:10:11

标签: java apache-camel

您好我有使用apache camel和输入队列的应用程序,这是处理的起点。我试图找到一种很好的方式来模拟这个输入队列,所以:

  • 我重复使用生产路由文件,我不想复制和粘贴内容,只为队列路由做一次更改
  • 我可以将此消息发送给“嘲笑”'队列和处理在生产中完成

这可能是关于改变队列:'进入'直接:'路由,但除了指定另一个xml之外,我找不到任何其他方法。

3 个答案:

答案 0 :(得分:3)

您可以使用Camels AdviceWith方法在测试期间拦截消息:

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");
      }
    };
  }
}

答案 1 :(得分:1)

您可以建议路线替换您的from - 组件,例如将amq端点替换为direct端点。然后,您可以使用生产者模板在测试中触发路径。

@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
    }
}

HTH。

答案 2 :(得分:0)

我创建了这样的课程

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。