Camel Content Router与velocity组合使用。

需求:根据body的内容,到达不同的端点,body原始内容为json结构。

实现思路:

  • 1 把json转换为HashMap
  • 2 使用内容路由器和simple表达式把消息发送到不同的端点
  • 3 对于不满足条件的消息使用velocity模板重新构造消息

实现方法

路由配置

 <camel:route>
            <camel:from uri="direct:start" />
            <camel:unmarshal>
                <camel:json library="Gson" unmarshalTypeName="java.util.HashMap"/>
            </camel:unmarshal>
            <camel:choice>
                <camel:when>
                    <camel:simple>${body[name]} == "mars"</camel:simple>
                    <camel:setBody>
                        <camel:simple>hello ${body[name]}</camel:simple>
                    </camel:setBody>
                    <camel:log message="${body}"></camel:log>
                </camel:when>
                <camel:when>
                    <camel:simple>${body[name]} != "mars"</camel:simple>
                    <camel:to uri="velocity:classpath:mail.vm"/>
                    <camel:log message="${body}"></camel:log>
                </camel:when>
            </camel:choice>
            <camel:to uri="mock:result"></camel:to>
  </camel:route>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

依赖


        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jackson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-gson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-velocity</artifactId>
        </dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13

velocity模板

please reply to $body.get("email")
1

测试用例

@RunWith(SpringRunner.class)
@SpringBootTest
public class CamelJsonVelocityTests {

    @Produce(uri="direct:start")
    private ProducerTemplate template;

    @EndpointInject(uri="mock:result")
    private  MockEndpoint mockEndpoint;


    @Test
    public void test1() throws InterruptedException {
        mockEndpoint.expectedBodiesReceived("hello mars");
        String msg = "{\n" +
            "    \"name\": \"mars\",\n" +
            "    \"email\": \"marszhang@gmail.com\",\n" +
            "    \"age\": 23,\n" +
            "    \"hobby\":[\n" +
            "      \"writing\",\n" +
            "      \"riding\",\n" +
            "      \"drawing\"\n" +
            "    ]\n" +
            "  }\n";
        template.sendBody(msg);
        mockEndpoint.assertIsSatisfied();
    }

    @Test
    public void test2() throws InterruptedException {
        mockEndpoint.expectedBodiesReceived("please reply to marszhang@gmail.com");
        String msg = "{\n" +
            "    \"name\": \"mars1\",\n" +
            "    \"email\": \"marszhang@gmail.com\",\n" +
            "    \"age\": 23,\n" +
            "    \"hobby\":[\n" +
            "      \"writing\",\n" +
            "      \"riding\",\n" +
            "      \"drawing\"\n" +
            "    ]\n" +
            "  }\n";
        template.sendBody(msg);
        mockEndpoint.assertIsSatisfied();
    }

}
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