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 礼物 阅读(1663) 评论(0)  编辑  收藏 所属分类: ESBESB