版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、一 demo工程1.下面給出一個從from到to有中間流程process處理的例子public class FileMoveWithCamel public static void main(String args) throws Exception CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() /將d:/temp/inbox/下的文件經(jīng)過process處理移到d:/temp/outbox public void configure() from("file
2、:d:/temp/inbox?noop=true").process(new FileConvertProcessor().to("file:d:/temp/outbox"); ); context.start(); boolean loop =true; while(loop) Thread.sleep(9000); context.stop(); class FileConvertProcessor implements Processor Override public void process(Exchange exchange) throws Excep
3、tion try InputStream body = exchange.getIn().getBody(InputStream.class); BufferedReader in = new BufferedReader(new InputStreamReader(body); StringBuffer strbf = new StringBuffer(""); String str = null; str = in.readLine(); while (str != null) System.out.println(str); strbf.append(str + &q
4、uot; "); str = in.readLine(); exchange.getOut().setHeader(Exchange.FILE_NAME, "converted.txt"); exchange.getOut().setBody(strbf.toString(); catch (IOException e) e.printStackTrace(); 2.下面這個例子是每隔10秒訪問一次http請求,并將訪問到的內(nèi)容寫入文件 /* *1 Create a CamelContext. 2 Optionally, configure components
5、or endpoints. 3 Add whatever routing rules you wish using the DSL and RouteBuilder or using Xml Configuration. 4 Start the context. */public class HttpPollWithQuartzCamel public static void main(String args) throws Exception CamelContext context = new DefaultCamelContext(); context.addRoutes(new Rou
6、teBuilder() public void configure() from("quartz:/report?cron=10 * * * * ?&stateful=true") .to("") .to("file:d:/temp/outbox?fileName=httpindex2.csv"); ); context.start(); boolean loop = true; while (loop) Thread.sleep(25000); context.stop(); 3. 在做系統(tǒng)集成的時候,必不可少的任務就是將數(shù)
7、據(jù)從一種格式轉(zhuǎn)換為另一種格式,再把轉(zhuǎn)換后的格式發(fā)到目標系統(tǒng),在此用實例介紹一下Camel中利用Freemarker做數(shù)據(jù)轉(zhuǎn)換.a :Freemarker的模板如下:<?xml version="1.0" encoding="UTF-8"?><people xmlns:h="/TR/html4/"> <#escape x as x?xml> <#list body.peopleLi
8、st as p> <person id="000001" age="20"> <name> <family>$p.fname</family> <given>$
9、p.gname</given> </name> <email>$p.email</email> <link manager="$p.manager" /> <#if p.level = "L1"&g
10、t; <l1tag>xxx</l1tag> </#if> </person> </#list> </#escape></people>b :與之對應的Java對象如下:每一個person節(jié)點對應一個ValueObject放在XMLTemplateParamet
11、er的peopleList里面.public class XMLTemplateParameter private String fileName; private List<ValueObject> peopleList = new ArrayList<ValueObject>(); public List<ValueObject> getPeopleList()
12、0; return peopleList; public void setPeopleList(List<ValueObject> peopleList) this.peopleList = peopleList; public String getFileName()
13、160;return fileName; public void setFileName(String fileName) this.fileName = fileName; public class ValueObject private String fname; private String gname; &
14、#160;private String email; private String manager; private String level;c :Route代碼如下:public class CamelFreemarkerRoute extends RouteBuilder public void configure() throws Exception from("quartz:/report?
15、cron=10 * * * * ?&stateful=true") .beanRef("fmBean","prepareFMValues") .to("freemarker:com/test/camel/freemarker/test.ftl") .to("file:d:/te
16、mp/outbox?fileName=fm.xml"); d :Route里用到的bean如下:xmlTemplateParameter做為頂級對象放在body里面,Freemarker里取數(shù)據(jù)的body.peopleList就對應于xmlTemplateParameter.peopleListpublic class FmProcessorBean public void prepareFMValues(Exchange exchan
17、ge) XMLTemplateParameter xmlTemplateParameter = new XMLTemplateParameter(); ValueObject val = null; for(int i=0;i<3;i+) val = new
18、ValueObject(); val.setFname("Yao"); val.setGname("Yorker" +i); val.setEmai
19、l("test"); val.setManager("m&an<ager"); val.setLevel("L" + i); xmlTemplateParameter.getPeopleLi
20、st().add(val); exchange.getIn().setBody(xmlTemplateParameter); e :Spring的配置文件如下:<beans xml
21、ns="/schema/beans" xmlns:xsi="/2001/XMLSchema-instance" xmlns:camel="/schema/spring" xsi:schemaLocation="/schema/beans /schem
22、a/beans/spring-beans-3.0.xsd /schema/spring /schema/spring/camel-spring.xsd" default-autowire="byName" default-init-method="init"> <bean id="fmBean" class="com.test.camel.freemarker.
23、FmProcessorBean"/> <camelContext id="testCamelContext" xmlns="/schema/spring"> <package>com.test.camel.freemarker</package> </camelContext> &
24、#160;</beans>f :啟動Spring,在D:tempoutbox文件夾下,每隔10秒鐘,會根據(jù)freemarker模板生成一個fm.xml文件. ApplicationContext ac = new ClassPathXmlApplicationContext("config/camelFreemarker.xml"); while (true)
25、60; Thread.sleep(2000); 對本例beanRef("fmBean","prepareFMValues")的解釋:其意思是調(diào)用fmBean的prepareFMValues方法,Camel會負責將message的body綁定到要調(diào)用方法的第一個參數(shù)上面,其中可能做相應的類型轉(zhuǎn)換.(本例中的方法的第一個參數(shù)為Exchange,沒有轉(zhuǎn)換的過程 ),這里給一個如下示例圖解釋這個綁定轉(zhuǎn)換的過程:Camel將Exchange的
26、的input message(exchange.getIn()轉(zhuǎn)換為String,綁定到mtd方法的name參數(shù)上.4下面是一個路由選擇的例子 public class EIP /* * param * return * 方法描述 * author CaiWen * throws Exception * createTime 2014-11-19 * 接口名 */public static void main(String args) throws Exception ConnectionFactory conn = new ActiveMQConnectionFactory("vm
27、:/localhost");CamelContext context = new DefaultCamelContext(); context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(conn); context.addRoutes(new RouteBuilder() public void configure() from("file:d:/temp/inbox?noop=true").to("jms:incomingOrders"); f
28、rom("jms:incomingOrders").choice().when(header("CamelFileName").endsWith(".xml") .to("jms:xmlOrders").when(header("CamelFileName").endsWith(".csv") .to("jms:csvOrders"); from("jms:csvOrders").process(new Processor() Over
29、ride public void process(Exchange arg0) throws Exception System.out.println("Recive xml order :"+arg0.getIn().getHeader("CamelFileName"); InputStream is = arg0.getIn().getBody(InputStream.class); BufferedReader br = new BufferedReader(new InputStreamReader(is); String str = br.re
30、adLine(); while(str!=null) System.out.println(str); str = br.readLine(); ); from("jms:xmlOrders").process(new Processor() Override public void process(Exchange arg0) throws Exception System.out.println("Recive xml order :" + arg0.getIn().getHeader("CamelFileName"); ); )
31、; context.start(); boolean loop =true; while(loop) Thread.sleep(9000); context.stop(); 5. 附加pom.xml <?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schema
32、Location="/POM/4.0.0 /maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.apache.camel</groupId><artifactId>examples</artifactId><version>2.14.0</version></parent>
33、;<artifactId>camel-example-activemq-tomcat</artifactId><name>Camel : Example : ActiveMQ : Tomcat</name><description>An example using ActiveMQ Broker and Camel with Apache Tomcat</description><packaging>war</packaging><build><plugins><plu
34、gin><groupId>org.codehaus.mojo</groupId><artifactId>tomcat-maven-plugin</artifactId><configuration><server>myTomcat</server><url>$tomcat.url</url><path>/$project.build.finalName</path></configuration></plugin><plugin
35、><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><configuration><mainClass>org.apache.camel.example.cxf.CamelRouteClient</mainClass><includePluginDependencies>false</includePluginDependencies><systemProperties&
36、gt;<property><key>java.util.logging.config.file</key><value>perties</value></property></systemProperties></configuration></plugin></plugins><!- Name of the generated WAR file -><finalName>camel-example-activemq-tomcat
37、</finalName></build><dependencies><!- camel -><dependency><groupId>org.apache.camel</groupId><artifactId>camel-core</artifactId></dependency><dependency><groupId>org.apache.camel</groupId><artifactId>camel-spring<
38、/artifactId></dependency><dependency><groupId>org.apache.camel</groupId><artifactId>camel-stream</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId></dependency>
39、;<!- camel jms and activemq -><dependency><groupId>org.apache.camel</groupId><artifactId>camel-jms</artifactId></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-camel</artifactId></dependency
40、><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-broker</artifactId></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-spring</artifactId></dependency><dependency>
41、<groupId>org.apache.activemq</groupId><artifactId>activemq-kahadb-store</artifactId></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-client</artifactId></dependency><dependency><groupId>o
42、rg.apache.activemq</groupId><artifactId>activemq-pool</artifactId></dependency><!- xbean is required for ActiveMQ broker configuration in the spring xml file -><dependency><groupId>org.apache.xbean</groupId><artifactId>xbean-spring</artifactId
43、></dependency><dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-quartz</artifactId></dependency><dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-http</artifactId></dependency><depen
44、dency> <groupId>org.apache.camel</groupId> <artifactId>camel-freemarker</artifactId></dependency><!- logging -><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId></dependency><dependency><groupId>or
45、g.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></dependency></dependencies><profiles><profile><id>Tomcat7</id><activation><activeByDefault>true</activeByDefault></activation><properties><tomcat.url>http
46、:/localhost:8080/manager/text</tomcat.url></properties></profile><profile><id>Tomcat6</id><properties><tomcat.url>http:/localhost:8080/manager</tomcat.url></properties></profile></profiles></project>二應用場景及項目應用 Enterpr
47、ise Application Integration 企業(yè)應用集成(EAI)企業(yè)應用集成是必要的,幾乎每家公司都有很多新產(chǎn)品及應用,如何集成這些應用程序是一個問題。來每十年誕生一個新范式,例如客戶端/服務器通信,面向服務的架構(gòu)(SOA)或云計算。此外,不同的接口或協(xié)議和技術的出現(xiàn)。過去數(shù)據(jù)存儲在文件,SQL數(shù)據(jù)庫在今天很通用。有時,還需要NoSQL數(shù)據(jù)庫。同步遠程過程調(diào)用RPC或異步消息是通過如RMI,SOAP的Web服務,REST或JMS進行通信的。很多軟件筒倉還存在。Enterprise Integration Patterns 企業(yè)集成模式(EIP)當然,你可以推倒重來,然后再寫一些意大利面條代碼,讓應用程序協(xié)同工作。不幸的是,你的管理者不會喜歡這個缺少長遠眼光的解決方案。企業(yè)集成模式()幫助碎片的問題,并使用標準化的方法來集成應用程序。使用相同的概念路由消息來改造。因此,每次有問題時重新發(fā)明輪子不是個好主意。集成的替代方案解決方案1 :自定義解決方案實現(xiàn)一個單獨的解決方案,適用于您的問題還沒有分離切成碎片。此工程可能是最快的替代的小型用例。你必須自己編寫所有。維護成本可能會高
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2021年中考化學考卷(蘇州專用)(原卷版)
- 河北白酒行業(yè)消費者調(diào)查問卷
- SOR-07-003-00 倉庫庫存盤點記錄
- 七年級生物下冊 第四單元 生物圈中的人 第四章 人體內(nèi)物質(zhì)的運輸 2 血流的管道─血管教案 新人教版
- 高中政治 第一單元 公民的政治生活 1.3 政治生活:自覺參與教案 新人教版必修2
- 七年級地理上冊 第一章 第三節(jié) 地圖的閱讀教學設計 新人教版
- 高中語文 第5課 蘇軾詞兩首教案1 新人教版必修4
- 江蘇省丹陽市七年級歷史下冊 10 蒙古族的興起與元朝的建立教案 新人教版
- 八年級物理下冊 第六章 物質(zhì)的物理屬性 2 測量物體的質(zhì)量教案設計(pdf)(新版)蘇科版
- 叉車以租代售合同模板
- 健康宣教之低血糖與運動有何關系
- 2023年中考語文一輪復習專題13-記敘文閱讀之句子的理解與賞析習題
- 成語故事《半途而廢》
- 抖音運營介紹
- 南開大學有機化學筆記-王積濤版
- 花椒豐產(chǎn)栽培技術講課
- 2023育嬰師初級操作技能考核評分記錄表1
- Unit5SectionB(1a-1d)課件人教版八年級英語上冊
- 智算產(chǎn)業(yè)發(fā)展白皮書
- 初中英語八年級上冊Unit 4 Whats the best movie theater Section B 2a-2e 教學課件
- 癥瘕(子宮肌瘤)圍手術期中醫(yī)診療方案
評論
0/150
提交評論