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,其中包结构如下:
- 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统统扔到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为我们要发布的服务的接口,该接口约束了我们要发布的服务:
- 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);
}
如上,我们将使用该接口发布echo和haha这两个服务。现在写我们的服务实现类EchoService.java(系统集成时可能实现已经存在,我们只需抽离要
发布的服务的窄接口即可):
- 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来约束它,从而只发布其中的echo和haha这两个服务。
配置使我们的服务以Web Service发布首先我们修改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方式发布我们的服务:
- <?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实现为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服务的客户端调用:
- <%@ 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并点击左侧菜单的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,即:
- 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);
然后刷新一下浏览器页面,再输入一些文本内容,看看页面是不是返回"haha"字样了?