Apache Camel框架之Freemarker做数据转换

在做系统集成的时候,必不可少的任务就是将数据从一种格式转换为另一种格式,再把转换后的格式发到目标系统,在此用实例介绍一下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("[email protected]");
            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)

Apache Camel框架之Freemarker做数据转换_第1张图片


你可能感兴趣的:(apache,框架,freemarker,String,manager,Exchange)