接下來兩章是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後面會專門作一章來介紹。