camel系列-表达式

BEAN METHOD

Bean 语言用于调用现有 Java bean 上的方法。

已注册方法,使用ref 引用实例名

1
2
3
4
5
6
7
<route>
<from uri="activemq:topic:OrdersTopic"/>
<filter>
<method ref="org.apache.camel.MyBean" method="isGoldCustomer"/>
<to uri="activemq:BigSpendersQueue"/>
</filter>
</route>

未注册实例

1
2
3
4
5
6
7
<route>
<from uri="activemq:topic:OrdersTopic"/>
<filter>
<method beanType="com.myBean" method="isGoldCustomer"/>
<to uri="activemq:BigSpendersQueue"/>
</filter>
</route>

CONSTANT

常量表达式语言实际上只是一种使用常量值或对象的方法。

1
2
3
4
5
6
7
<route>
<from uri="seda:a"/>
<setHeader name="zipCode">
<constant resultType="int">90210</constant>
</setHeader>
<to uri="mock:b"/>
</route>

REF

The Ref Expression Language is really just a way to lookup a custom Expression or Predicate from the Registry.

  1. Expression
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
43
44
45
46
47
48
49
public class SplitRefCustomExpressionTest extends ContextTestSupport {

@Override
protected Registry createRegistry() throws Exception {
Registry jndi = super.createRegistry();
jndi.bind("myCustomExpression", new MyCustomExpression());
return jndi;
}

@Test
public void testSplitCustomExpression() throws Exception {
getMockEndpoint("mock:split").expectedBodiesReceived("A", "B", "C");

template.sendBody("direct:start", "A,B,C");

assertMockEndpointsSatisfied();
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.split()
.ref("myCustomExpression")
.to("mock:split");
}
};
}

public static class MyCustomExpression implements Expression {

@Override
@SuppressWarnings("unchecked")
public <T> T evaluate(Exchange exchange, Class<T> type) {
final String body = exchange.getIn().getBody(String.class);

// just split the body by comma
String[] parts = body.split(",");
List<String> list = new ArrayList<>();
for (String part : parts) {
list.add(part);
}

return (T) list.iterator();
}
}
}
  1. Filter
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 FilterTest extends ContextTestSupport {

@Test
public void testSendMatchingMessage() throws Exception {
MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
resultEndpoint.expectedMessageCount(1);

template.sendBodyAndHeader("direct:start2", "<matched/>", "foo", "bar");

resultEndpoint.assertIsSatisfied();
}

@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {

from("direct:start").filter().ref("myCustomExpression").to("mock:result");
}
};
}

@Override
protected Registry createRegistry() throws Exception {
Registry jndi = super.createRegistry();
jndi.bind("myCustomExpression", new FilterTest.MyCustomPredicate());
return jndi;
}

public static class MyCustomPredicate implements Predicate {
@Override
public boolean matches(Exchange exchange) {
return false;
}
}
}

HEADER

The Header Expression Language allows you to extract values of named headers.

1
2
3
4
5
6
<route>
<from uri="direct:a" />
<recipientList>
<header>myHeader</header>
</recipientList>
</route>

EXCHANGEPROPERTY

The ExchangeProperty Expression Language allows you to extract values of named exchange properties.

1
2
3
4
5
6
<route>
<from uri="direct:a" />
<recipientList>
<exchangeProperty>myProperty</exchangeProperty>
</recipientList>
</route>

`