概述
Bean 组件用于方法调用
实例方法调用
单例模式
默认情况下,同一个路由下,多次调用只会产生一个实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class BeanInvokeTest extends ContextTestSupport {
@Test public void testA() throws Exception { MockEndpoint mock = getMockEndpoint("mock:a");
mock.expectedBodiesReceived("Hello World"); template.sendBody("direct:a", "Hello World"); assertMockEndpointsSatisfied();
mock.reset(); mock.expectedBodiesReceived(""); template.sendBody("direct:a", ""); assertMockEndpointsSatisfied();
mock.reset(); mock.expectedMessageCount(1); mock.message(0).body().isNull(); template.sendBody("direct:a", null); assertMockEndpointsSatisfied(); }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:a").bean(BeanInvokeTest.class, "doSomething").to("mock:a"); } }; }
public String doSomething(String s) { return s; } }
|
Prototype 模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public class BeanNoCacheTest extends ContextTestSupport {
private static final AtomicInteger COUNTER = new AtomicInteger();
@Test public void testBeanRefNoCache() throws Exception { getMockEndpoint("mock:result").expectedBodiesReceived("Hello1", "Bye2", "Camel3");
template.sendBody("direct:start", "Hello"); template.sendBody("direct:start", "Bye"); template.sendBody("direct:start", "Camel");
assertMockEndpointsSatisfied(); }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").bean(MyCoolBean.class, "doSomething", BeanScope.Prototype).to("mock:result"); } }; }
public static class MyCoolBean {
private final int count;
public MyCoolBean() { count = COUNTER.incrementAndGet(); }
public int getCount() { return count; }
public String doSomething(String s) { return s + count; } } }
|
静态方法调用
跟实例方法调用一致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class BeanInvokeStaticTest extends ContextTestSupport {
@Test public void testA() throws Exception { context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:a").bean(MyStaticClass.class, "changeSomething").to("mock:a"); } }); context.start();
MockEndpoint mock = getMockEndpoint("mock:a"); mock.expectedBodiesReceived("Bye World");
template.sendBody("direct:a", "Hello World");
assertMockEndpointsSatisfied(); }
}
public final class MyStaticClass {
private MyStaticClass() { }
public static String changeSomething(String s) { if ("Hello World".equals(s)) { return "Bye World"; } return null; }
}
|
指定绑定实例方法调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class BeanMapPutTest extends ContextTestSupport {
private Map<String, String> myMap = new HashMap<>();
@Override protected Registry createRegistry() throws Exception { Registry jndi = super.createRegistry(); jndi.bind("myMap", myMap); return jndi; }
@Test public void testMapPut() throws Exception { assertEquals(0, myMap.size());
template.sendBody("direct:start", "Hello World");
assertEquals(1, myMap.size()); assertEquals("true", myMap.get("isMaster")); }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").bean("myMap", "put('isMaster','true')"); } }; } }
|
使用路由 url 方式调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class BeanInvokeWithNullBodyTest extends ContextTestSupport {
@Test public void testWithHelloWorld() throws Exception { getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied(); }
@Override protected Registry createRegistry() throws Exception { Registry jndi = super.createRegistry(); jndi.bind("foo", new MyNullFooBean()); return jndi; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").to("bean:foo").to("mock:result"); } }; }
@SuppressWarnings("unused") private static class MyNullFooBean {
public String doSomething(String s) { return s; } } }
|