Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          關鍵字: 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發布和調用Web服務,體驗一下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你在命令行輸入的字符串。

          創建Eclipse項目
          我們在Eclipse下新建命名為mule的Java project,其中包結構如下:
          Java代碼 復制代碼
          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統統扔到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發布和調用Web服務。

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

          如上,我們將使用該接口發布echo和haha這兩個服務。現在寫我們的服務實現類EchoService.java(系統集成時可能實現已經存在,我們只需抽離要
          發布的服務的窄接口即可):
          Java代碼 復制代碼
          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三個服務,但我們使用IEchoService來約束它,從而只發布其中的echo和haha這兩個服務。

          配置使我們的服務以Web Service發布
          首先我們修改web.xml來讓servlet容器listen和mapping一些東西:
          Java代碼 復制代碼
          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方式發布我們的服務:
          Java代碼 復制代碼
          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實現為cn.hidetoishandsome.mule.demo.EchoService,inbound-router中<endpoint address="xfire:http://localhost:8181/services"/>表示我們以xfire發布我們的服務到本機該端口的services這個context下,而serviceInterfaces的entry表示我們使用IEchoService
          來約束我們要發布的服務接口。

          運行和調用服務
          讓我們將該mule項目在Tomcat中跑起來看看效果吧。
          例如配置Tomcat的server.xml,在<Host>標簽中加入<Context path="/mule" docBase="D:\project\mule" reloadable="true" />
          啟動Tomcat,打開瀏覽器訪問http://localhost:8181/services/echoService?wsdl可以看到Mule通過xfire自動生成的wsdl文檔,其中
          我們可以看到Mule只暴露了EchoService和echo和haha方法,而沒有暴露hehe方法。
          現在我們在echo.jsp中利用Mule的UMO(Universal Message Object)API寫對我們剛發布的Web服務的客戶端調用:
          Java代碼 復制代碼
          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并點擊左側菜單的Echo鏈接,或者直接訪問http://localhost:8080/mule/echo.jsp,然后在input框輸入一些文本內容如"Hideto",點擊Go,則你會看到頁面返回"Hello, Hideto!"。
          現在讓我們修改echo.jsp,將UMOMessage message = client.send("xfire:http://localhost:8181/services/echoService?method=echo", s, null);
          這段代碼中的method改為haha,即:
          Java代碼 復制代碼
          1. UMOMessage?message?=?client.send("xfire:http://localhost:8181/services/echoService?method=haha",?s,?null);??

          然后刷新一下瀏覽器頁面,再輸入一些文本內容,看看頁面是不是返回"haha"字樣了?
          posted on 2009-09-19 22:20 禮物 閱讀(1697) 評論(0)  編輯  收藏 所屬分類: ESB 、ESB
          主站蜘蛛池模板: 威宁| 晋中市| 阿克苏市| 海丰县| 勐海县| 商丘市| 门头沟区| 林州市| 惠水县| 玉龙| 环江| 昌吉市| 阳曲县| 开原市| 巴林左旗| 兴仁县| 德江县| 江北区| 长寿区| 金阳县| 临邑县| 乌拉特中旗| 潍坊市| 香河县| 金昌市| 安阳县| 贵德县| 镇平县| 香港| 定安县| 永安市| 嘉禾县| 乌拉特后旗| 静海县| 舞钢市| 堆龙德庆县| 江都市| 汨罗市| 高要市| 会理县| 诸城市|