Apache Camel框架之Freemarker做数据转换
来源:互联网 发布:北京域名备案的时间 编辑:程序博客网 时间:2024/05/16 17:14
在做系统集成的时候,必不可少的任务就是将数据从一种格式转换为另一种格式,再把转换后的格式发到目标系统,在此用实例介绍一下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
- Apache Camel框架之Freemarker做数据转换
- Apache Camel框架之Freemarker做数据转换
- Apache Camel框架之Freemarker做数据转换
- Apache Camel框架之Freemarker做数据转换
- Apache Camel之数据转换
- apache Camel之数据转换
- Apache Camel框架之FTP路由
- Apache Camel框架之JMS路由
- Apache Camel框架之HTTP路由
- Apache Camel框架之Error handling
- Apache Camel框架之事务控制
- Apache Camel框架之FTP路由
- Apache Camel框架之JMS路由
- Apache Camel框架之HTTP路由
- Apache Camel框架之Error handling
- Apache Camel框架之HTTP路由
- Apache Camel框架之FTP路由
- Apache Camel框架之事务控制
- Eclipse下J2ME开发环境的配置
- Apache Camel框架之FTP路由
- 数据结构——线性表(二)
- Servlet单实例以及线程安全小结
- Apache Camel框架之事务控制
- Apache Camel框架之Freemarker做数据转换
- oracle_导致实例逐出的五大问题
- md5加密方法
- Apache Camel Route节点的消息载体Exchange
- 字符设备驱动模型
- Oracle RAC 11.2.0.3 节点CRS无法启动报:no network hb 故障解决方法
- sybase IQ 12.7 安装截图
- Apache Camel简介与入门
- JBuilder特点