Apache Camel框架之Freemarker做数据转换

24 篇文章 0 订阅

在做系统集成的时候,必不可少的任务就是将数据从一种格式转换为另一种格式,再把转换后的格式发到目标系统,在此用实例介绍一下Camel中利用Freemarker做数据转换.

1,Freemarker的模板如下:

<?xml version="1.0" encoding="UTF-8"?>
<people  xmlns:h="http://www.w3.org/TR/html4/">
    <#escape x as x?xml>
    <#list body.peopleList as p>
    <person id="000001" age="20">
        <name>
            <family>${p.fname}</family>
            <given>${p.gname}</given>
        </name>
        <email>${p.email}</email>
        <link manager="${p.manager}" />
        <#if p.level == "L1">
        <l1tag>xxx</l1tag>
        </#if>
    </person>
    </#list>
    </#escape>
</people>

2,与之对应的Java对象如下:每一个person节点对应一个ValueObject放在XMLTemplateParameter的peopleList里面.

public class XMLTemplateParameter {
    private String fileName;    
    private List<ValueObject> peopleList = new ArrayList<ValueObject>();

    public List<ValueObject> getPeopleList() {
        return peopleList;
    }
    public void setPeopleList(List<ValueObject> peopleList) {
        this.peopleList = peopleList;
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}
public class ValueObject {
    private String fname;
    private String gname;
    private String email;
    private String manager;
    private String level;

3,Route代码如下:

public class CamelFreemarkerRoute extends RouteBuilder {
    public void configure() throws Exception {
        from("quartz://report?cron=10 * * * * ?&stateful=true")
        .beanRef("fmBean","prepareFMValues") 
        .to("freemarker:com/test/camel/freemarker/test.ftl")
        .to("file:d:/temp/outbox?fileName=fm.xml");    
    }
}

4,Route里用到的bean如下:xmlTemplateParameter做为顶级对象放在body里面,Freemarker里取数据的body.peopleList就对应于xmlTemplateParameter.peopleList

public class FmProcessorBean {    
    public void prepareFMValues(Exchange exchange){
        XMLTemplateParameter xmlTemplateParameter = new XMLTemplateParameter();

        ValueObject val = null;
        for(int i=0;i<3;i++){
            val = new ValueObject();        
            val.setFname("Yao");
            val.setGname("Yorker" +i);
            val.setEmail("test@mail.com");
            val.setManager("m&an<ager");
            val.setLevel("L" + i);
            xmlTemplateParameter.getPeopleList().add(val);            
        }        
        exchange.getIn().setBody(xmlTemplateParameter);
    }    
}

5,Spring的配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:camel="http://camel.apache.org/schema/spring"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"
 default-autowire="byName" default-init-method="init">
    <bean id="fmBean" class="com.test.camel.freemarker.FmProcessorBean"/>
    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
        <package>com.test.camel.freemarker</package>
    </camelContext>    
</beans>

6,启动Spring,在D:\temp\outbox文件夹下,每隔10秒钟,会根据freemarker模板生成一个fm.xml文件.

        ApplicationContext ac = new ClassPathXmlApplicationContext("config/camelFreemarker.xml");
        while (true) {
            Thread.sleep(2000);
        }

对本例beanRef("fmBean","prepareFMValues")的解释:

其意思是调用fmBean的prepareFMValues方法,Camel会负责将message的body绑定到要调用方法的第一个参数上面,其中可能做相应的类型转换.(本例中的方法的第一个参数为Exchange,没有转换的过程 ),这里给一个如下示例图解释这个绑定转换的过程:Camel将Exchange的的input message(exchange.getIn())转换为String,绑定到mtd方法的name参数上.(图片来源于Camel in Action)




原文链接: http://blog.csdn.net/kkdelta/article/details/7242961

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值