将Camel路由使用Beanshell来执行的话...

将Camel路由使用Beanshell来执行的话:将需要编译 的路由部分,很轻松的延迟到执行期 才解析。

很简单的一步,提供了一种思路:以一种更动态的形式构造路由

 

进而,可以编写一个动态解析"由Java DSL编写的Camel路由脚本 "并展示为图形的Eclipse插件..........

 

 

route.bsh脚本内容:

 

class MockRouteBuilder extends org.apache.camel.builder.RouteBuilder{
            public void configure() {
                // START SNIPPET: example
                from("direct:start").setBody(body().append(" World!")).to("mock:result");
                // END SNIPPET: example
            }
}

context.addRoutes(new MockRouteBuilder());

 

TestCase:

 

 

public class TransformViaDSLTest extends ContextTestSupport {
	protected MockEndpoint resultEndpoint;

	public void testSendingAMessageUsingMulticastReceivesItsOwnExchange()
			throws Exception {

		Interpreter interpreter = new Interpreter();
		interpreter.set("context", context);
		interpreter.eval("e:\\route.bsh");

		startCamelContext();

       	resultEndpoint.expectedBodiesReceived("Hello World!");
		sendBody("direct:start", "Hello");resultEndpoint.assertIsSatisfied();
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();

		resultEndpoint = getMockEndpoint("mock:result");
	}

}

 

------------------------------------------------------------------------------------------------------------------------------

 

这样也行:

		Interpreter interpreter = new Interpreter();

		interpreter.set("context", context);

		String test = "class MockRouteBuilder extends org.apache.camel.builder.RouteBuilder{"
				+ "\n"
				+ "public void configure() {"
				+ "\n"
				+ "from(\"direct:start\").setBody(body().append(\" World!\")).to(\"mock:result\");"
				+ "\n"
				+ "}"
				+ "\n"
				+ "}"
				+ "\n"
				+ "context.addRoutes(new MockRouteBuilder());";
		System.out.println(test);
		BufferedReader bufferedreader = new BufferedReader(new StringReader(
				test));
		interpreter.eval(bufferedreader);
 

你可能感兴趣的:(apache,eclipse,脚本)