昨天开始研究mule,首先从网上上的几个例子开始,照着网页做了几个竟然没一个能跑通很头痛,今天也弄了半天还好调通了一个。下面我来写下我调好的例子。
一、 先用xfire 整个webservice。
打开eclipse创建一个web工程(我用的是myeclipse插件)。
创建javabean
book.java
服务接口:
实现类:
大家看到了这个服务包括两个方法:
findBookByISBN (根据ISBN查询图书的title)和
testPass(返回一个测试通过的字符串)
配置web.xml
接下来在src下建一个xfire用的配置文件目录。
META-INF
|-----xfire
然后在xfire目录下创建一个配置文件 services.xml
ok 现在把xfire的包考进来,我用xfire版本是1.2.6的。
启动tomcat 测试下http://localhost:8080/TestXfire/services/BookService?wsdl 访问成功应该能看到这个webservice的wsdl。
二、配置mule
webservice好了现在把刚才的webservice集成到ESB中去。
创建一个web工程,把mule的包全都copy进去。
配置web.xml
然后在 web-inf 下创建一个mule的配置文件
mule-services-config.xml
注意因为咱们用xfire配置的webservice 在写 endpoint 的时候 在地址的前面加上 wsdl-xfire:
我当时就是被这个问题郁闷了好久,上面的地址是刚才部署的那个webservice的地址
接下来建立一个jsp测试文件 index.jsp
里面的 client.send("vm://bookservice",s, null) ,发送的地址 就是刚才在配置文件中 inboundEndpoint
好的现在已经完成了,启动这个服务的tomcat 然后打开IE 输入 123456 看看返回结果是否正确。
xfire1.2.6的下载地址
mule 1.3.3的下载地址
一、 先用xfire 整个webservice。
打开eclipse创建一个web工程(我用的是myeclipse插件)。
创建javabean
book.java
package
com.test.xfire;
public class Book ... {
private String title;
private String isbn;
public String getIsbn() ...{
return isbn;
}
public void setIsbn(String isbn) ...{
this.isbn = isbn;
}
public String getTitle() ...{
return title;
}
public void setTitle(String title) ...{
this.title = title;
}
}
public class Book ... {
private String title;
private String isbn;
public String getIsbn() ...{
return isbn;
}
public void setIsbn(String isbn) ...{
this.isbn = isbn;
}
public String getTitle() ...{
return title;
}
public void setTitle(String title) ...{
this.title = title;
}
}
服务接口:
package
com.test.xfire;
public interface BookService ... {
String findBookByISBN(String isbn);
String testPass();
}
public interface BookService ... {
String findBookByISBN(String isbn);
String testPass();
}
package
com.test.xfire;
public class BookServiceImpl implements BookService ... {
private Book book;
public BookServiceImpl() ...{
book = new Book();
book.setTitle("XFire Quick Start");
book.setIsbn("123456");
}
public String findBookByISBN(String isbn) ...{
System.out.println("Common this webservice!" + isbn);
if (book.getIsbn().equals(isbn)) ...{
System.out.println("this ok");
return book.getTitle();
}
throw new RuntimeException("Can't find book");
}
public String testPass() ...{
return "helloworld";
}
}
public class BookServiceImpl implements BookService ... {
private Book book;
public BookServiceImpl() ...{
book = new Book();
book.setTitle("XFire Quick Start");
book.setIsbn("123456");
}
public String findBookByISBN(String isbn) ...{
System.out.println("Common this webservice!" + isbn);
if (book.getIsbn().equals(isbn)) ...{
System.out.println("this ok");
return book.getTitle();
}
throw new RuntimeException("Can't find book");
}
public String testPass() ...{
return "helloworld";
}
}
配置web.xml
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< servlet >
< servlet-name > xfire </ servlet-name >
< servlet-class >
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > xfire </ servlet-name >
< url-pattern > /services/* </ url-pattern >
</ servlet-mapping >
</ web-app >
< web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< servlet >
< servlet-name > xfire </ servlet-name >
< servlet-class >
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > xfire </ servlet-name >
< url-pattern > /services/* </ url-pattern >
</ servlet-mapping >
</ web-app >
接下来在src下建一个xfire用的配置文件目录。
META-INF
|-----xfire
然后在xfire目录下创建一个配置文件 services.xml
<?
xml version="1.0" encoding="UTF-8"
?>
< beans xmlns ="http://xfire.codehaus.org/config/1.0" >
< service >
< name > BookService </ name >
< namespace >
http://localhost:8080/xfire/services/BookService
</ namespace >
< serviceClass >
com.test.xfire.BookService
</ serviceClass >
< implementationClass >
com.test.xfire.BookServiceImpl
</ implementationClass >
</ service >
</ beans >
< beans xmlns ="http://xfire.codehaus.org/config/1.0" >
< service >
< name > BookService </ name >
< namespace >
http://localhost:8080/xfire/services/BookService
</ namespace >
< serviceClass >
com.test.xfire.BookService
</ serviceClass >
< implementationClass >
com.test.xfire.BookServiceImpl
</ implementationClass >
</ service >
</ beans >
启动tomcat 测试下http://localhost:8080/TestXfire/services/BookService?wsdl 访问成功应该能看到这个webservice的wsdl。
二、配置mule
webservice好了现在把刚才的webservice集成到ESB中去。
创建一个web工程,把mule的包全都copy进去。
配置web.xml
<?
xml version="1.0" encoding="UTF-8"
?>
< 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-services-config.xml, </ param-value >
</ context-param >
< listener >
< listener-class >
org.mule.config.builders.MuleXmlBuilderContextListener
</ listener-class >
</ listener >
< welcome-file-list >
< welcome-file > index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
< 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-services-config.xml, </ param-value >
</ context-param >
< listener >
< listener-class >
org.mule.config.builders.MuleXmlBuilderContextListener
</ listener-class >
</ listener >
< welcome-file-list >
< welcome-file > index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
<?
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 ="BookService" inboundEndpoint ="vm://bookservice" implementation ="org.mule.components.simple.BridgeComponent" >
< outbound-router >
< router className ="org.mule.routing.outbound.OutboundPassThroughRouter" >
< endpoint address ="wsdl-xfire:http://140.100.100.35:8080/TestXfire/services/BookService?wsdl&method=findBookByISBN" />
</ router >
</ outbound-router >
</ mule-descriptor >
< mule-descriptor name ="BookServiceImpl" inboundEndpoint ="vm://passservice" implementation ="org.mule.components.simple.BridgeComponent" >
< outbound-router >
< router className ="org.mule.routing.outbound.OutboundPassThroughRouter" >
< endpoint address ="wsdl-xfire:http://140.100.100.35:8080/TestXfire/services/BookService?wsdl&method=testPass" />
</ router >
</ outbound-router >
</ mule-descriptor >
</ mule-configuration >
<! 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 ="BookService" inboundEndpoint ="vm://bookservice" implementation ="org.mule.components.simple.BridgeComponent" >
< outbound-router >
< router className ="org.mule.routing.outbound.OutboundPassThroughRouter" >
< endpoint address ="wsdl-xfire:http://140.100.100.35:8080/TestXfire/services/BookService?wsdl&method=findBookByISBN" />
</ router >
</ outbound-router >
</ mule-descriptor >
< mule-descriptor name ="BookServiceImpl" inboundEndpoint ="vm://passservice" implementation ="org.mule.components.simple.BridgeComponent" >
< outbound-router >
< router className ="org.mule.routing.outbound.OutboundPassThroughRouter" >
< endpoint address ="wsdl-xfire:http://140.100.100.35:8080/TestXfire/services/BookService?wsdl&method=testPass" />
</ router >
</ outbound-router >
</ mule-descriptor >
</ mule-configuration >
注意因为咱们用xfire配置的webservice 在写 endpoint 的时候 在地址的前面加上 wsdl-xfire:
我当时就是被这个问题郁闷了好久,上面的地址是刚才部署的那个webservice的地址
接下来建立一个jsp测试文件 index.jsp
<?
xml version="1.0" encoding="UTF-8"
?>
<% ... @ page import="org.mule.extras.client.MuleClient, org.mule.umo.UMOMessage" %>
<% ...
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
</ head >
< body >
<% ... String s = request.getParameter("isbn");
if(s!=null) {
System.out.println("sadsadsadsadsa");
MuleClient client = new MuleClient();
UMOMessage message =
%>
< h3 > The book with isbn " <% = s %> " is : <% = message.getPayload() %> </ h3 >
<% } %>
Please enter the isbn of book:
< form method ="POST" name ="submitEcho" action ="" >
< table >
< tr >< td >
< input type ="text" name ="isbn" /></ td >< td >
< input type ="submit" name ="Go" value =" Go " />
</ td ></ tr >
</ table >
</ form >
</ body >
</ html >
<% ... @ page import="org.mule.extras.client.MuleClient, org.mule.umo.UMOMessage" %>
<% ...
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
</ head >
< body >
<% ... String s = request.getParameter("isbn");
if(s!=null) {
System.out.println("sadsadsadsadsa");
MuleClient client = new MuleClient();
UMOMessage message =
%>
< h3 > The book with isbn " <% = s %> " is : <% = message.getPayload() %> </ h3 >
<% } %>
Please enter the isbn of book:
< form method ="POST" name ="submitEcho" action ="" >
< table >
< tr >< td >
< input type ="text" name ="isbn" /></ td >< td >
< input type ="submit" name ="Go" value =" Go " />
</ td ></ tr >
</ table >
</ form >
</ body >
</ html >
里面的 client.send("vm://bookservice",s, null) ,发送的地址 就是刚才在配置文件中 inboundEndpoint
好的现在已经完成了,启动这个服务的tomcat 然后打开IE 输入 123456 看看返回结果是否正确。
xfire1.2.6的下载地址
mule 1.3.3的下载地址