zoukankan      html  css  js  c++  java
  • [Camel Basics]

    Define routes:

    Either using Spring xml or Java DSL.

    Spring xml:

    <camelContext>

      <routeBuilder ref="myBuilder" />   //to load the Java DSL routes defined in MyRouteBuilder class

      <routeContextRef> //to load the routes in <routeContext> defined in other xml file

        <route> 

        <from> <process> <to>

      </route>

    </....>

    <bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/>

    Java DSL:

    extends RouteBuilder, implement the abstract method configure().

    @Override
    public void configure() throws Exception {

      Endpoint orderUpdates = endpoint("pubsub://batman-order-updates");
      from("pubsub://adv-updates").process(galaxyAdvCache.input(header(Ref.INBOX)));

    }

    Apache Camel Test

    In the separate integration module:

    main/java: BGIntegration.java[RouteBuilder]

    main/resources: integration-b-g.xml(Only used to include the java DSL route in BGIntegration into Camel context).

    test/java: BMock.java[RouteBuilder], GMock.java[RouteBuilder], BGIntegrationTest.java

    test/resources: mock-b-g.xml

    Only the BGIntegration.java is used for the tests in test project, all the other 3 are for unit test.

    In BGIntegration:

    matcher.request("b://pubsub://b-order-updates").uniqueCorrelationId(orderId())
        .reply("g:pubsub://tibrv-incoming-suggestion").size(1).correlationId(gSourceOrderID())
        .timeout(10000)
        .enrich(inheritInbox)
        .to("g:first-matched-suggestion");

    In BGIntegrationTest:

    @Produce(context = "b", uri = "direct://mock-order")     
    private ProducerTemplate bOrderTemplate;        //用于给b提供原始order

    @Produce(context = "g", uri = "direct://mock-order")    //用于给g提供原始msg
    private ProducerTemplate gOrderTemplate;

    bOrderTemplate.sendBodyAndHeader(bOrder(orderId), Ref.INBOX, ref.id());   //bOrder和gOrder都是新构建的,只需set好用于match的field即可。
    gOrderTemplate.sendBodyAndHeader(gOrder, Ref.INBOX, ref.id());

    In BMock:

    from("direct://mock-order").to(report.request(orderLabel())
                  .in(content(OrderDataContract.class)), endpoint("pubsub://b-order-updates"));

    //从direct://mock-order把order传给真正route里的b://pubsub://b-order-updates。

    In GMock:

    Samilar route from direct://mock-order to g:pubsub://tibrv-incoming-suggestion.

    Another route from g:first-matched-suggestion to mock://matched-suggestion. mock component会存储msg来做future assertions.

    Back to BGIntegrationTest:

    @EndpointInject(context = "g", uri = "mock://matched-suggestion")
    MockEndpoint matchedOrderMock;

    matchedOrderMock.reset();
    matchedOrderMock.expectedBodiesReceived(galaxyOrder);  //Mock的测试条件

    在往初始endpoint produce完msg以后,最后要测试matcher是否将两个msg match成功并存入mock://matched-suggestion.

    matchedOrderMock.assertIsSatisfied();

    http://camel.apache.org/testing.html

    Mock component: 声明期待,确认期待是否满足。期待包括:msg count, msg payload, msg header, msgs order...

    The Mock component provides a powerful declarative testing mechanism, which is similar to jMock in that it allows declarative expectations to be created on any Mock endpoint before a test begins. Then the test is run, which typically fires messages to one or more endpoints, and finally the expectations can be asserted in a test case to ensure the system worked as expected.

    Remember that Mock is designed for testing. When you add Mock endpoints to a route, each Exchange sent to the endpoint will be stored (to allow for later validation) in memory until explicitly reset or the JVM is restarted. 

    The following annotations is supported and inject by Camel's CamelBeanPostProcessor

    Annotation

    Description

    @EndpointInject

    To inject an endpoint, see more details at POJO Producing.

    @BeanInject

    Camel 2.13: To inject a bean obtained from the Registry. See Bean Injection.

    @PropertyInject

    Camel 2.12: To inject a value using property placeholder.

    @Produce

    To inject a producer to send message to an endpoint. See POJO Producing.

    @Consume

    To inject a consumer on a method. See POJO Consuming.

  • 相关阅读:
    java中synchronized关键字分析
    如何查看java的class文件
    安装jdk1.9后报 Error:java: 无效的源发行版: 1.9
    mybatis拦截器获取sql
    NOIP2015 T4 推销员 贪心+堆优化
    BZOJ 2761: [JLOI2011]不重复数字 hash哈希
    Contest-hunter 暑假送温暖 SRM01
    Contest-hunter 暑假送温暖 SRM08
    洛谷P3393逃离僵尸岛 最短路
    bzoj 1433: [ZJOI2009]假期的宿舍 最大流
  • 原文地址:https://www.cnblogs.com/chayu3/p/3900168.html
Copyright © 2011-2022 走看看