Camel In Action 读书笔记 (7)

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

就不详细介绍了。ide

Camel在单元测试上作了不少工做,咱们能够很方便的进行单元测试。单元测试

要测试路由的话:咱们能够继承两个类一个是CamelTestSupport:测试

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

public class DefaultErrorHandlerTest extends CamelTestSupport {this

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

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

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

        assertMockEndpointsSatisfied();
    }
ci

       @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后面会专门作一章来介绍。

相关文章
相关标签/搜索