备忘录

记录学习过、研究过、使用过和总结过的内容,以备不时之需

统计

留言簿(4)

积分与排名

其它

技术网站

牛人博客

阅读排行榜

评论排行榜

实战Mule:利用Mule调用XFire发布的Web服务(转)

下载和安装XFire和Mule
参考http://www.blogjava.net/103335460/articles/227980.html和http://www.blogjava.net/103335460/articles/227979.html对XFire和Mule的介绍
本文例子也以上述两篇文章的例子为背景。

利用XFire发布一个Web服务BookService
在Eclipse里新建项目webservice,目录结构如下:
Java代码 复制代码
  1. webservice   
  2.   src-service   
  3.     cn.hidetoishandsome.xfire.model   
  4.       Book.java   
  5.     cn.hidetoishandsome.xfire.service   
  6.       IBookService.java   
  7.     cn.hidetoishandsome.xfire.service.impl   
  8.       BookService.java   
  9.   src-conf   
  10.     META-INF   
  11.       xfire   
  12.         services.xml   
  13.   web   
  14.     WEB-INF   
  15.       lib   
  16.       web.xml  

其中web.xml:
Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>     
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"     
  3.          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">     
  4.      
  5.     <servlet>     
  6.         <servlet-name>xfire</servlet-name>     
  7.         <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>     
  8.     </servlet>     
  9.      
  10.     <servlet-mapping>     
  11.         <servlet-name>xfire</servlet-name>     
  12.         <url-pattern>/services/*</url-pattern>     
  13.     </servlet-mapping>     
  14.      
  15. </web-app>  

以及services.xml:
Java代码 复制代码
  1. <beans xmlns="http://xfire.codehaus.org/config/1.0">     
  2.     <service>     
  3.         <name>BookService</name>     
  4.         <namespace>http://localhost:9090/webservice/services/BookService</namespace>     
  5.         <serviceClass>cn.hidetoishandsome.xfire.service.IBookService</serviceClass>     
  6.         <implementationClass>cn.hidetoishandsome.xfire.service.impl.BookService</implementationClass>     
  7.     </service>     
  8. </beans>  

我们发布BookService类的findBookByISBN方法,通过传入book的isbn返回查询到的book的title
注意services.xml中我们把BookService的namespace设置为http://localhost:9090/webservice/services/BookService,这是为了在同一机器上同时
启动两个Tomcat实例来测试我们的demo,我们将启动一个Tomcat来host我们发布的BookService,并且port设置为9090,而启动的第二个Tomcat用来
host我们的Mule ESB,以及前台页面调用测试。
好了,现在我们已经可以启动第一个Tomcat实例来发布BookService了,访问http://localhost:9090/webservice/services/BookService?wsdl可以看
到XFire自动生成的WSDL文档。

利用Mule构建我们的ESB中心
在Eclipse里创建新项目esb,目录结构如下:
Java代码 复制代码
  1. esb   
  2.   web   
  3.     WEB-INF   
  4.       lib   
  5.         mule-services-config.xml   
  6.         web.xml   
  7.     index.jsp  

其中web.xml:
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-services-config.xml,</param-value>   
  10.     </context-param>   
  11.      
  12.     <listener>   
  13.         <listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class>   
  14.     </listener>   
  15.   
  16.     <welcome-file-list>   
  17.         <welcome-file>index.jsp</welcome-file>   
  18.     </welcome-file-list>   
  19. </web-app>  

以及mule-services-config.xml:
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.   
  8.     <mule-descriptor name="BookService" inboundEndpoint="vm://bookservice" implementation="org.mule.components.simple.BridgeComponent">   
  9.   
  10.         <outbound-router>   
  11.             <router className="org.mule.routing.outbound.OutboundPassThroughRouter">   
  12.                 <endpoint address="wsdl-xfire:http://localhost:9090/webservice/services/BookService?wsdl&method=findBookByISBN"/>   
  13.             </router>   
  14.         </outbound-router>   
  15.   
  16.     </mule-descriptor>   
  17.   
  18. </mule-configuration>  

这里我们配置了我们要调用的BookService的outbound router endpoint的address为:
wsdl-xfire:http://localhost:9090/webservice/services/BookService?wsdl&method=findBookByISBN
好了,我们的Mule ESB已经构建好了,并且我们在自己的ESB中注入了一个Web服务BookService,我们不用担心底层的实现,我们只需按照接口简单调用即可。
下面我们写前端调用代码index.jsp:
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("isbn");      
  11.     if(s!=null) {      
  12.         MuleClient client = new MuleClient();      
  13.         UMOMessage message = client.send("vm://bookservice", s, null);        
  14. %>     
  15. <h3>The book with isbn "<%=s%>" is : <<<%=message.getPayload()%>>></h3>     
  16.      <%}%>     
  17. Please enter the isbn of book:      
  18. <form method="POST" name="submitISBM" action="">     
  19.     <table>     
  20.         <tr><td>     
  21.             <input type="text" name="isbn"/></td><td><input type="submit" name="Go" value=" Go " />     
  22.         </td></tr>     
  23.     </table>     
  24. </form>     
  25. <p/>     
  26. </body>     
  27. </html>    

现在让我们启动第二个Tomcat实例,然后访问http://localhost:8080/esb,输入isbn号码“123456”,提交来查看返回的Book的Title。

源代码
将源代码打包提供如下,WEB-INF/lib下面的jar包都删除了,请参考http://www.blogjava.net/103335460/articles/227980.html和http://www.blogjava.net/103335460/articles/227979.html来
添加jar包。

posted on 2008-09-09 16:25 雪山飞狐 阅读(549) 评论(0)  编辑  收藏 所属分类: 开源框架


只有注册用户登录后才能发表评论。


网站导航: