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服務,體驗一下
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)如下:
- mule ??
- ??src-demo ??
- ????cn.hidetoishandsome.mule.demo ??
- ??????EchoService.java ??
- ??????IEchoService.java ??
- ??images ??
- ????mule-banner.gif ??
- ??WEB-INF ??
- ????lib ??
- ????mule-echo-config.xml ??
- ????web.xml ??
- ??contents.html ??
- ??echo.jsp ??
- ??header.html ??
- ??index.html ??
- ??welcome.html??
mule
src-demo
cn.hidetoishandsome.mule.demo
EchoService.java
IEchoService.java
images
mule-banner.gif
WEB-INF
lib
mule-echo-config.xml
web.xml
contents.html
echo.jsp
header.html
index.html
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服務。
寫我們要發(fā)布的服務上面列出的IEchoService.java為我們要發(fā)布的服務的接口,該接口約束了我們要發(fā)布的服務:
- package?cn.hidetoishandsome.mule.demo; ??
- ??
- public?interface?IEchoService?{ ??
- ????String?echo(String?s); ??
- ??
- ????String?haha(String?s); ??
- }??
package cn.hidetoishandsome.mule.demo;
public interface IEchoService {
String echo(String s);
String haha(String s);
}
如上,我們將使用該接口發(fā)布echo和haha這兩個服務?,F(xiàn)在寫我們的服務實現(xiàn)類EchoService.java(系統(tǒng)集成時可能實現(xiàn)已經(jīng)存在,我們只需抽離要
發(fā)布的服務的窄接口即可):
- package?cn.hidetoishandsome.mule.demo; ??
- ??
- public?class?EchoService?implements?IEchoService?{ ??
- ??
- ????public?String?echo(String?s)?{ ??
- ????????return?"Hello,?"?+?s?+?"!"; ??
- ????} ??
- ??
- ????public?String?haha(String?s)?{ ??
- ????????return?"haha"; ??
- ????} ??
- ??
- ????public?String?hehe(String?s)?{ ??
- ????????return?"hehe"; ??
- ????} ??
- }??
package cn.hidetoishandsome.mule.demo;
public class EchoService implements IEchoService {
public String echo(String s) {
return "Hello, " + s + "!";
}
public String haha(String s) {
return "haha";
}
public String hehe(String s) {
return "hehe";
}
}
可以看到,雖然我們的EchoService提供了echo/haha/hehe三個服務,但我們使用IEchoService來約束它,從而只發(fā)布其中的echo和haha這兩個服務。
配置使我們的服務以Web Service發(fā)布首先我們修改web.xml來讓servlet容器listen和mapping一些東西:
- <?xml?version="1.0"?encoding="UTF-8"?> ??
- <!DOCTYPE?web-app?PUBLIC?"-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN"?"http://java.sun.com/dtd/web-app_2_3.dtd"> ??
- <web-app> ??
- ????<display-name>Mule</display-name> ??
- ????<description>Mule?Demo</description> ??
- ??
- ????<context-param> ??
- ????????<param-name>org.mule.config</param-name> ??
- ????????<param-value>/WEB-INF/mule-echo-config.xml,</param-value> ??
- ????</context-param> ??
- ??
- ????<listener> ??
- ????????<listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class> ??
- ????</listener> ??
- ??
- ????<servlet> ??
- ????????<servlet-name>muleServlet</servlet-name> ??
- ????????<servlet-class>org.mule.providers.http.servlet.MuleReceiverServlet</servlet-class> ??
- ????????<load-on-startup/> ??
- ????</servlet> ??
- ??
- ????<servlet-mapping> ??
- ????????<servlet-name>muleServlet</servlet-name> ??
- ????????<url-pattern>/services/*</url-pattern> ??
- ????</servlet-mapping> ??
- ??
- ????<welcome-file-list> ??
- ????????<welcome-file>index.html</welcome-file> ??
- ????</welcome-file-list> ??
- </web-app>??
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Mule</display-name>
<description>Mule Demo</description>
<context-param>
<param-name>org.mule.config</param-name>
<param-value>/WEB-INF/mule-echo-config.xml,</param-value>
</context-param>
<listener>
<listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class>
</listener>
<servlet>
<servlet-name>muleServlet</servlet-name>
<servlet-class>org.mule.providers.http.servlet.MuleReceiverServlet</servlet-class>
<load-on-startup/>
</servlet>
<servlet-mapping>
<servlet-name>muleServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
然后我們配置
mule-echo-config.xml來以Web Service方式發(fā)布我們的服務:
- <?xml?version="1.0"?encoding="UTF-8"?> ??
- ??
- <!DOCTYPE?mule-configuration?PUBLIC?"-//MuleSource?//DTD?mule-configuration?XML?V1.0//EN"??
- ????????????????????????????????"http://mule.mulesource.org/dtds/mule-configuration.dtd"> ??
- ??
- <mule-configuration?id="Mule_Demo"?version="1.0"> ??
- ????<mule-descriptor?name="echoService"?implementation="cn.hidetoishandsome.mule.demo.EchoService"> ??
- ????????<inbound-router> ??
- ????????????<endpoint?address="xfire:http://localhost:8181/services"/> ??
- ????????</inbound-router> ??
- ????????<properties>?? ??
- ????????????<list?name="serviceInterfaces">??? ??
- ????????????????<entry?value="cn.hidetoishandsome.mule.demo.IEchoService"/>??? ??
- ????????????</list>?? ??
- ????????</properties>?? ??
- ???</mule-descriptor> ??
- </mule-configuration>??
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mule-configuration PUBLIC "-//MuleSource //DTD mule-configuration XML V1.0//EN"
"http://mule.mulesource.org/dtds/mule-configuration.dtd">
<mule-configuration id="Mule_Demo" version="1.0">
<mule-descriptor name="echoService" implementation="cn.hidetoishandsome.mule.demo.EchoService">
<inbound-router>
<endpoint address="xfire:http://localhost:8181/services"/>
</inbound-router>
<properties>
<list name="serviceInterfaces">
<entry value="cn.hidetoishandsome.mule.demo.IEchoService"/>
</list>
</properties>
</mule-descriptor>
</mule-configuration>
這里我們的echoService實現(xiàn)為cn.hidetoishandsome.
mule.demo.EchoService,inbound-router中<endpoint address="xfire:http://localhost:8181/services"/>表示我們以xfire發(fā)布我們的服務到本機該端口的services這個context下,而serviceInterfaces的entry表示我們使用IEchoService
來約束我們要發(fā)布的服務接口。
運行和調(diào)用服務讓我們將該
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方法。
現(xiàn)在我們在echo.jsp中利用
Mule的UMO(Universal Message Object)API寫對我們剛發(fā)布的Web服務的客戶端調(diào)用:
- <%@?page?import="org.mule.extras.client.MuleClient,?org.mule.umo.UMOMessage"%> ??
- <%@?page?language="java"?contentType="text/html;?charset=UTF-8"?%> ??
- ??
- <html> ??
- <head> ??
- <title>Mule?Echo?Example</title> ??
- </head> ??
- <body> ??
- <% ??
- ????String?s?=?request.getParameter("name"); ??
- ????if(s!=null)?{ ??
- ????????MuleClient?client?=?new?MuleClient(); ??
- ????????UMOMessage?message?=?client.send("xfire:http://localhost:8181/services/echoService?method=echo",?s,?null);?? ??
- %> ??
- <h3><%=message.getPayload()%></h3> ??
- ?????<%}%> ??
- Please?enter?your?name: ??
- <form?method="POST"?name="submitEcho"?action=""> ??
- ????<table> ??
- ????????<tr><td> ??
- ????????????<input?type="text"?name="name"/></td><td><input?type="submit"?name="Go"?value="?Go?"?/> ??
- ????????</td></tr> ??
- ????</table> ??
- </form> ??
- <p/> ??
- </body> ??
- </html>??
<%@ page import="org.mule.extras.client.MuleClient, org.mule.umo.UMOMessage"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Mule Echo Example</title>
</head>
<body>
<%
String s = request.getParameter("name");
if(s!=null) {
MuleClient client = new MuleClient();
UMOMessage message = client.send("xfire:http://localhost:8181/services/echoService?method=echo", s, null);
%>
<h3><%=message.getPayload()%></h3>
<%}%>
Please enter your name:
<form method="POST" name="submitEcho" action="">
<table>
<tr><td>
<input type="text" name="name"/></td><td><input type="submit" name="Go" value=" Go " />
</td></tr>
</table>
</form>
<p/>
</body>
</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,即:
- UMOMessage?message?=?client.send("xfire:http://localhost:8181/services/echoService?method=haha",?s,?null);??
UMOMessage message = client.send("xfire:http://localhost:8181/services/echoService?method=haha", s, null);
然后刷新一下瀏覽器頁面,再輸入一些文本內(nèi)容,看看頁面是不是返回"haha"字樣了?