Camel In Action 读书笔记 (7)

接下来两章是Testing with Camel 和Understanding components

就不详细介绍了。

Camel在单元测试上做了很多工作,我们可以很方便的进行单元测试。

要测试路由的话:我们可以继承两个类一个是CamelTestSupport:

重写createRouteBuilder方法定义路由。如下:

public class DefaultErrorHandlerTest extends CamelTestSupport {

    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry jndi = super.createRegistry();
        jndi.bind("orderService", new OrderService());
        return jndi;
    }

    @Test
    public void testOrderOk() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:queue.order");
        mock.expectedBodiesReceived("amount=1,name=Camel in Action,id=123,status=OK");

        template.sendBody("seda:queue.inbox","amount=1,name=Camel in Action");

        assertMockEndpointsSatisfied();
    }

       @Override
   protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                // context.setTracing(true);

                errorHandler(defaultErrorHandler()
                    .maximumRedeliveries(2)
                    .redeliveryDelay(1000)
                    .retryAttemptedLogLevel(LoggingLevel.WARN));

                from("seda:queue.inbox")
                    .beanRef("orderService", "validate")
                    .beanRef("orderService", "enrich")
                    .log("Received order ${body}")
                    .to("mock:queue.order");
            }
        };
    }

}

如果是Spring定义的路由我们可以继承CamelSpringTestSupport 重写createApplicationContext读取xml配置的路由

如下:

public class SpringRouteScopeTest extends CamelSpringTestSupport {

@Override

public void setUp() throws Exception {

        deleteDirectory("target/orders");

super.setUp();

}

@Override

protected AbstractXmlApplicationContext createApplicationContext() {

// see this file for the route in Spring XML

return new ClassPathXmlApplicationContext("camelinaction/RouteScopeTest.xml");

}

@Test

public void testOrderOk() throws Exception {

// we expect the file to be converted to csv and routed to the 2nd route

MockEndpoint file = getMockEndpoint("mock:file");

        file.expectedMessageCount(1);

// we expect the 2nd route to complete

MockEndpoint mock = getMockEndpoint("mock:queue.order");

        mock.expectedBodiesReceived("amount=1,name=Camel in Action,id=123,status=OK");

        template.sendBodyAndHeader("file://target/orders", "amount=1#name=Camel in Action", Exchange.FILE_NAME, "order.txt");

        assertMockEndpointsSatisfied();

}

@Test

public void testOrderActiveMQ() throws Exception {

// we expect the file to be converted to csv and routed to the 2nd route

MockEndpoint file = getMockEndpoint("mock:file");

        file.expectedMessageCount(1);

// we do not expect the 2nd route to complete

MockEndpoint mock = getMockEndpoint("mock:queue.order");

        mock.expectedMessageCount(0);

        template.sendBodyAndHeader("file://target/orders", "amount=1#name=ActiveMQ in Action", Exchange.FILE_NAME, "order.txt");

// wait 10 seconds to let this test run

Thread.sleep(10000);

        assertMockEndpointsSatisfied();

}

@Test

public void testXmlOrderFail() throws Exception {

// we do not expect the file to be converted to csv

MockEndpoint file = getMockEndpoint("mock:file");

        file.expectedMessageCount(0);

// and therefore no messages in the 2nd route

MockEndpoint mock = getMockEndpoint("mock:queue.order");

        mock.expectedMessageCount(0);

        template.sendBodyAndHeader("file://target/orders", "<?xml version=\"1.0\"?><order>"

+ "<amount>1</amount><name>Camel in Action</name></order>", Exchange.FILE_NAME, "order2.xml");

// wait 5 seconds to let this test run

Thread.sleep(5000);

        assertMockEndpointsSatisfied();

}

}

同时Camel还实现了一个mock的组建我们可以将消息发送到mock:*****来判断消息是否正确。

关于常用component的就不介绍了。这个如果需要的话请查询camel的手册。这里只讲下最近遇到的问题:

direct与direct-vm他们都是接到到生产者产生的消息后调用消费者;区别是direct只在同一个CamelContext下有效;direct-vm可以定义在不同的CamelContext只要是同一个jvm就可以。

seda与vm也是同一区别。

这个好处是我们在用BAM监控路由时,BAM的路由可以完全独立出来与业务路由,完全解耦。关于BAM后面会专门做一章来介绍。

转载于:https://my.oschina.net/u/574870/blog/137720

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值