在做系统集成的时候,必不可少的任务就是将数据从一种格式转换为另一种格式,再把转换后的格式发到目标系统,在此用实例介绍一下Camel中利用Freemarker做数据转换.
1,Freemarker的模板如下:
[html] view plain copy print ?
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>
<?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里面.
[java] view plain copy print ?
1. public class
2. private
3. private List<ValueObject> peopleList = new
4.
5. public
6. return
7. }
8. public void
9. this.peopleList = peopleList;
10. }
11. public
12. return
13. }
14. public void
15. this.fileName = fileName;
16. }
17. }
18. public class
19. private
20. private
21. private
22. private
23. private
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代码如下:
[java] view plain copy print ?
1. public class CamelFreemarkerRoute extends
2. public void configure() throws
3. from("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");
7. }
8. }
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
[java] view plain copy print ?
1. public class
2. public void
3. XMLTemplateParameter xmlTemplateParameter = new
4.
5. ValueObject val = null;
6. for(int i=0;i<3;i++){
7. val = new
8. val.setFname("Yao");
9. val.setGname("Yorker"
10. val.setEmail("test@mail.com");
11. val.setManager("m&an<ager");
12. val.setLevel("L"
13. xmlTemplateParameter.getPeopleList().add(val);
14. }
15. exchange.getIn().setBody(xmlTemplateParameter);
16. }
17. }
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的配置文件如下:
[html] view plain copy print ?
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.xsd
5. 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>
<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)
在做系统集成的时候,必不可少的任务就是将数据从一种格式转换为另一种格式,再把转换后的格式发到目标系统,在此用实例介绍一下Camel中利用Freemarker做数据转换.
1,Freemarker的模板如下:
[html] view plain copy print ?
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>
<?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里面.
[java] view plain copy print ?
1. public class
2. private
3. private List<ValueObject> peopleList = new
4.
5. public
6. return
7. }
8. public void
9. this.peopleList = peopleList;
10. }
11. public
12. return
13. }
14. public void
15. this.fileName = fileName;
16. }
17. }
18. public class
19. private
20. private
21. private
22. private
23. private
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代码如下:
[java] view plain copy print ?
1. public class CamelFreemarkerRoute extends
2. public void configure() throws
3. from("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");
7. }
8. }
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
[java] view plain copy print ?
1. public class
2. public void
3. XMLTemplateParameter xmlTemplateParameter = new
4.
5. ValueObject val = null;
6. for(int i=0;i<3;i++){
7. val = new
8. val.setFname("Yao");
9. val.setGname("Yorker"
10. val.setEmail("test@mail.com");
11. val.setManager("m&an<ager");
12. val.setLevel("L"
13. xmlTemplateParameter.getPeopleList().add(val);
14. }
15. exchange.getIn().setBody(xmlTemplateParameter);
16. }
17. }
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的配置文件如下:
[html] view plain copy print ?
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.xsd
5. 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>
<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)