Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學(xué);靜其心,可悟天下之理;恒其心,可成天下之業(yè)。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          關(guān)鍵字: ESB SOA WebService Mule
          Mule is the leading open source ESB (Enterprise Service Bus) and integration platform. It is a scalable, highly distributable
          object broker that can seamlessly handle interactions with services and applications using disparate transport and messaging
          technologies。

          在這里我們簡單看看如何用Mule發(fā)布和調(diào)用Web服務(wù),體驗一下Mule的簡潔和高效。

          安裝Mule
          Mule官方下載頁下載最新的Full版zip文件,解壓到一個目錄。
          運行一下MULE_HOME\examples\maven\echo目錄下的echo.bat,則mule會自動下載合適版本的activation.jar和mail.jar到MULE_HOME\lib\user目錄
          echo.bat示例了一個command prompt下的echo程序,該程序會echo你在命令行輸入的字符串。

          創(chuàng)建Eclipse項目
          我們在Eclipse下新建命名為mule的Java project,其中包結(jié)構(gòu)如下:
          Java代碼 復(fù)制代碼
          1. mule ??
          2. ??src-demo ??
          3. ????cn.hidetoishandsome.mule.demo ??
          4. ??????EchoService.java ??
          5. ??????IEchoService.java ??
          6. ??images ??
          7. ????mule-banner.gif ??
          8. ??WEB-INF ??
          9. ????lib ??
          10. ????mule-echo-config.xml ??
          11. ????web.xml ??
          12. ??contents.html ??
          13. ??echo.jsp ??
          14. ??header.html ??
          15. ??index.html ??
          16. ??welcome.html??

          將MULE_HOME\lib\mule目錄和MULE_HOME\lib\opt目錄下所有的jar包以及MULE_HOME\lib\user目錄下的activation.jar和mail.jar統(tǒng)統(tǒng)扔到WEB-INF\lib目錄下。
          其中mule-banner.gif、contents.html、header.html、index.html、welcome.html等來自將MULE_HOME\examples\ant\webapp目錄下的build.xml用ant
          build后在build目錄下生成的mule-example-webapp.war中的文件。
          在這里我們修改該mule-example-webapp.war工程來demo一下使用Mule發(fā)布和調(diào)用Web服務(wù)。

          寫我們要發(fā)布的服務(wù)
          上面列出的IEchoService.java為我們要發(fā)布的服務(wù)的接口,該接口約束了我們要發(fā)布的服務(wù):
          Java代碼 復(fù)制代碼
          1. package?cn.hidetoishandsome.mule.demo; ??
          2. ??
          3. public?interface?IEchoService?{ ??
          4. ????String?echo(String?s); ??
          5. ??
          6. ????String?haha(String?s); ??
          7. }??

          如上,我們將使用該接口發(fā)布echo和haha這兩個服務(wù)。現(xiàn)在寫我們的服務(wù)實現(xiàn)類EchoService.java(系統(tǒng)集成時可能實現(xiàn)已經(jīng)存在,我們只需抽離要
          發(fā)布的服務(wù)的窄接口即可):
          Java代碼 復(fù)制代碼
          1. package?cn.hidetoishandsome.mule.demo; ??
          2. ??
          3. public?class?EchoService?implements?IEchoService?{ ??
          4. ??
          5. ????public?String?echo(String?s)?{ ??
          6. ????????return?"Hello,?"?+?s?+?"!"; ??
          7. ????} ??
          8. ??
          9. ????public?String?haha(String?s)?{ ??
          10. ????????return?"haha"; ??
          11. ????} ??
          12. ??
          13. ????public?String?hehe(String?s)?{ ??
          14. ????????return?"hehe"; ??
          15. ????} ??
          16. }??

          可以看到,雖然我們的EchoService提供了echo/haha/hehe三個服務(wù),但我們使用IEchoService來約束它,從而只發(fā)布其中的echo和haha這兩個服務(wù)。

          配置使我們的服務(wù)以Web Service發(fā)布
          首先我們修改web.xml來讓servlet容器listen和mapping一些東西:
          Java代碼 復(fù)制代碼
          1. <?xml?version="1.0"?encoding="UTF-8"?> ??
          2. <!DOCTYPE?web-app?PUBLIC?"-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN"?"http://java.sun.com/dtd/web-app_2_3.dtd"> ??
          3. <web-app> ??
          4. ????<display-name>Mule</display-name> ??
          5. ????<description>Mule?Demo</description> ??
          6. ??
          7. ????<context-param> ??
          8. ????????<param-name>org.mule.config</param-name> ??
          9. ????????<param-value>/WEB-INF/mule-echo-config.xml,</param-value> ??
          10. ????</context-param> ??
          11. ??
          12. ????<listener> ??
          13. ????????<listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class> ??
          14. ????</listener> ??
          15. ??
          16. ????<servlet> ??
          17. ????????<servlet-name>muleServlet</servlet-name> ??
          18. ????????<servlet-class>org.mule.providers.http.servlet.MuleReceiverServlet</servlet-class> ??
          19. ????????<load-on-startup/> ??
          20. ????</servlet> ??
          21. ??
          22. ????<servlet-mapping> ??
          23. ????????<servlet-name>muleServlet</servlet-name> ??
          24. ????????<url-pattern>/services/*</url-pattern> ??
          25. ????</servlet-mapping> ??
          26. ??
          27. ????<welcome-file-list> ??
          28. ????????<welcome-file>index.html</welcome-file> ??
          29. ????</welcome-file-list> ??
          30. </web-app>??

          然后我們配置mule-echo-config.xml來以Web Service方式發(fā)布我們的服務(wù):
          Java代碼 復(fù)制代碼
          1. <?xml?version="1.0"?encoding="UTF-8"?> ??
          2. ??
          3. <!DOCTYPE?mule-configuration?PUBLIC?"-//MuleSource?//DTD?mule-configuration?XML?V1.0//EN"??
          4. ????????????????????????????????"http://mule.mulesource.org/dtds/mule-configuration.dtd"> ??
          5. ??
          6. <mule-configuration?id="Mule_Demo"?version="1.0"> ??
          7. ????<mule-descriptor?name="echoService"?implementation="cn.hidetoishandsome.mule.demo.EchoService"> ??
          8. ????????<inbound-router> ??
          9. ????????????<endpoint?address="xfire:http://localhost:8181/services"/> ??
          10. ????????</inbound-router> ??
          11. ????????<properties>?? ??
          12. ????????????<list?name="serviceInterfaces">??? ??
          13. ????????????????<entry?value="cn.hidetoishandsome.mule.demo.IEchoService"/>??? ??
          14. ????????????</list>?? ??
          15. ????????</properties>?? ??
          16. ???</mule-descriptor> ??
          17. </mule-configuration>??

          這里我們的echoService實現(xiàn)為cn.hidetoishandsome.mule.demo.EchoService,inbound-router中<endpoint address="xfire:http://localhost:8181/services"/>表示我們以xfire發(fā)布我們的服務(wù)到本機(jī)該端口的services這個context下,而serviceInterfaces的entry表示我們使用IEchoService
          來約束我們要發(fā)布的服務(wù)接口。

          運行和調(diào)用服務(wù)
          讓我們將該mule項目在Tomcat中跑起來看看效果吧。
          例如配置Tomcat的server.xml,在<Host>標(biāo)簽中加入<Context path="/mule" docBase="D:\project\mule" reloadable="true" />
          啟動Tomcat,打開瀏覽器訪問http://localhost:8181/services/echoService?wsdl可以看到Mule通過xfire自動生成的wsdl文檔,其中
          我們可以看到Mule只暴露了EchoService和echo和haha方法,而沒有暴露hehe方法。
          現(xiàn)在我們在echo.jsp中利用Mule的UMO(Universal Message Object)API寫對我們剛發(fā)布的Web服務(wù)的客戶端調(diào)用:
          Java代碼 復(fù)制代碼
          1. <%@?page?import="org.mule.extras.client.MuleClient,?org.mule.umo.UMOMessage"%> ??
          2. <%@?page?language="java"?contentType="text/html;?charset=UTF-8"?%> ??
          3. ??
          4. <html> ??
          5. <head> ??
          6. <title>Mule?Echo?Example</title> ??
          7. </head> ??
          8. <body> ??
          9. <% ??
          10. ????String?s?=?request.getParameter("name"); ??
          11. ????if(s!=null)?{ ??
          12. ????????MuleClient?client?=?new?MuleClient(); ??
          13. ????????UMOMessage?message?=?client.send("xfire:http://localhost:8181/services/echoService?method=echo",?s,?null);?? ??
          14. %> ??
          15. <h3><%=message.getPayload()%></h3> ??
          16. ?????<%}%> ??
          17. Please?enter?your?name: ??
          18. <form?method="POST"?name="submitEcho"?action=""> ??
          19. ????<table> ??
          20. ????????<tr><td> ??
          21. ????????????<input?type="text"?name="name"/></td><td><input?type="submit"?name="Go"?value="?Go?"?/> ??
          22. ????????</td></tr> ??
          23. ????</table> ??
          24. </form> ??
          25. <p/> ??
          26. </body> ??
          27. </html>??

          好了,用瀏覽器訪問http://localhost:8080/mule并點擊左側(cè)菜單的Echo鏈接,或者直接訪問http://localhost:8080/mule/echo.jsp,然后在input框輸入一些文本內(nèi)容如"Hideto",點擊Go,則你會看到頁面返回"Hello, Hideto!"。
          現(xiàn)在讓我們修改echo.jsp,將UMOMessage message = client.send("xfire:http://localhost:8181/services/echoService?method=echo", s, null);
          這段代碼中的method改為haha,即:
          Java代碼 復(fù)制代碼
          1. UMOMessage?message?=?client.send("xfire:http://localhost:8181/services/echoService?method=haha",?s,?null);??

          然后刷新一下瀏覽器頁面,再輸入一些文本內(nèi)容,看看頁面是不是返回"haha"字樣了?
          posted on 2009-09-19 22:20 禮物 閱讀(1699) 評論(0)  編輯  收藏 所屬分類: ESBESB

          只有注冊用戶登錄后才能發(fā)表評論。

          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 阜康市| 义马市| 阿拉善盟| 青神县| 墨竹工卡县| 高青县| 射阳县| 临猗县| 武鸣县| 远安县| 浦城县| 利川市| 湛江市| 临洮县| 七台河市| 大庆市| 汾西县| 瓦房店市| 高尔夫| 湟源县| 博爱县| 林甸县| 黔东| 长寿区| 新密市| 长宁区| 黄大仙区| 喜德县| 宜丰县| 清苑县| 柳州市| 山西省| 万源市| 林西县| 巨野县| 九寨沟县| 宾川县| 鞍山市| 保定市| 锡林郭勒盟| 锦屏县|