Apache Camel框架之Freemarker做数据转换_freamaker实现报文转换.docx
-
资源ID:97090198
资源大小:24.59KB
全文页数:4页
- 资源格式: DOCX
下载积分:5金币
快捷下载
![游客一键下载](/images/hot.gif)
会员登录下载
微信登录下载
三方登录下载:
微信扫一扫登录
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
|
Apache Camel框架之Freemarker做数据转换_freamaker实现报文转换.docx
Apache Camel框架之Freemarker做数据转换在做系统集成的时候,必不可少的任务就是将数据从一种格式转换为另一种格式,再把转换后的格式发到目标系统,在此用实例介绍一下Camel中利用Freemarker做数据转换.1,Freemarker的模板如下:1 <?xml version="1.0" encoding="UTF-8"?>2 <people xmlns:h="http:/www.w3.org/TR/html4/">3 <#escape x as x?xml>4 <#list body.peopleList as p>5 <person id="000001" age="20">6 <name>7 <family>$p.fname</family>8 <given>$p.gname</given>9 </name>10 <email>$p.email</email>11 <link manager="$p.manager" />12 <#if p.level = "L1">13 <l1tag>xxx</l1tag>14 </#if>15 </person>16 </#list>17 </#escape>18 </people>2,与之对应的Java对象如下:每一个person节点对应一个ValueObject放在XMLTemplateParameter的peopleList里面.1 public class XMLTemplateParameter 2 private String fileName;3 private List<ValueObject> peopleList = new ArrayList<ValueObject>(); 45 public List<ValueObject> getPeopleList() 76 return peopleList;891011121314151617 public void setPeopleList(List<ValueObject> peopleList) this.peopleList = peopleList;public String getFileName() return fileName;public void setFileName(String fileName) this.fileName = fileName;18 public class ValueObject 19 private String fname;20 private String gname;21 private String email;22 private String manager;23 private String level;3,Route代码如下:12public class CamelFreemarkerRoute extends RouteBuilder public void configure() throws Exception 3from("quartz:/report?cron=10 * * * * ?&stateful=true")4.beanRef("fmBean","prepareFMValues")5.to("freemarker:com/test/camel/freemarker/test.ftl")6.to("file:d:/temp/outbox?fileName=fm.xml");784,Route里用到的bean如下:xmlTemplateParameter做为顶级对象放在body里面,Freemarker里取数据的body.peopleList就对应于 xmlTemplateParameter.peopleList12public class FmProcessorBean public void prepareFMValues(Exchange exchange)3XMLTemplateParameter xmlTemplateParameter = new XMLTemplateParameter();45ValueObject val = null;6for(int i=0;i<3;i+)7val = new ValueObject();8val.setFname("Yao");9val.setGname("Yorker" +i);10val.setEmail("test");11val.setManager("m&an<ager");12val.setLevel("L" + i);13xmlTemplateParameter.getPeopleList().add(val);1415exchange.getIn().setBody(xmlTemplateParameter);16175,Spring的配置文件如下:1 <beans xmlns="http:/www.springframework.org/schema/beans"2 xmlns:xsi="http:/www.w3.org/2001/XMLSchema-instance"3 xmlns:camel="http:/camel.apache.org/schema/spring"4 xsi:schemaLocation="http:/www.springframework.org/schema/beans http:/www.springframework.org/schema/beans/spring-beans-3.0.xsd5 http:/camel.apache.org/schema/spring http:/camel.apache.org/schema/spring/camel-spring.xsd"6 default-autowire="byName" default-init-method="init">7 <bean id="fmBean" class="com.test.camel.freemarker.FmProcessorBean"/>8 <camelContext id="testCamelContext" xmlns="http:/camel.apache.org/schema/spring">9 <package>com.test.camel.freemarker</package>10 </camelContext>11 </beans>6,启动Spring,在D:tempoutbox文件夹下,每隔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)