用Tuscany、Axis、groovy來發布和調試Web Service
Posted on 2007-09-11 14:10 YanHua 閱讀(1309) 評論(0) 編輯 收藏 所屬分類: SOA AND BPM Tuscany--是一個符合SCA標準的開源實現,他能夠很容易地將一個服務綁定為一個Web Service:
要使服務發布成功,要保證有以下幾個文件:
* helloworld.wsdl
* HelloWorldService.java
* HelloWorldImpl.java
這樣我們就可以寫一個類來創建一個SCA中的Domain,并由它來創建組件并發布為一個web服務。
運行這個類,也就啟動了一個SCA的Domain,打開瀏覽器輸入http://localhost:8085/HelloWorldService? wsdl(注意這個是以binding.ws的uri為準的,和wsdl文件中的wsdlsoap:address的location無關,當然在這里它 們倆個是一致的),就可以看到發布的服務的wsdl描述,應該和前面的helloworld.wsdl的內容一樣。
下面我們寫一個groovy的客戶端來測試一下這個web服務,代碼大概象這樣(更多的信息可以參考http://docs.codehaus.org/display/GROOVY/Groovy+SOAP ,要運行下面的代碼需要先安裝一個groovy的用來除了soap的類庫):
運行它,在控制臺可以看到hello yanhua了嗎?
有時候我們想查看一下客戶端和服務器之間來往的soap消息,我們可以用AXIS提供的一個叫TCPMonitor的工具。先下載AXIS,解壓后在AXIS的目錄下運行如下的命令:java -classpath ./lib/axis.jar org.apache.axis.utils.tcpmon,這樣會打開TCPMonitor這個工具。我們新建一個Listener,設置如下圖:

我們把剛才的groovy代碼改一下:
運行它,在TCPMonitor中就可以看到往返的soap消息了,對這個例子來說分別是:
發送到服務器的soap消息:
返回給客戶端的soap消息:
<composite?xmlns="http://www.osoa.org/xmlns/sca/1.0"?name="Employee">
????<service?name="HelloWorldService"
????????promote="HelloWorldServiceComponent">
????????<interface.wsdl
????????????interface="http://helloworld#wsdl.interface(HelloWorld)"?/>
????????<binding.ws?uri="http://localhost:8085/HelloWorldService"?/>
????</service>
????<component?name="HelloWorldServiceComponent">
????????<implementation.java?class="helloworld.HelloWorldImpl"?/>
????</component>
</composite>
????<service?name="HelloWorldService"
????????promote="HelloWorldServiceComponent">
????????<interface.wsdl
????????????interface="http://helloworld#wsdl.interface(HelloWorld)"?/>
????????<binding.ws?uri="http://localhost:8085/HelloWorldService"?/>
????</service>
????<component?name="HelloWorldServiceComponent">
????????<implementation.java?class="helloworld.HelloWorldImpl"?/>
????</component>
</composite>
要使服務發布成功,要保證有以下幾個文件:
- 一個預定義的wsdl文件,只要保證這個文件在classpath下就可以了。
- 一個java接口。
- 一個java類。
* helloworld.wsdl
<wsdl:definitions?targetNamespace="http://helloworld"
xmlns:tns="http://helloworld"?xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
????name="helloworld">
????<wsdl:types>
????????<schema?elementFormDefault="qualified"?targetNamespace="http://helloworld"
???????? xmlns="http://www.w3.org/2001/XMLSchema">
????????????<element?name="getGreetings">
????????????????<complexType>
????????????????????<sequence>
????????????????????????<element?name="name"?type="xsd:string"/>
????????????????????</sequence>
????????????????</complexType>
????????????</element>
????????????<element?name="getGreetingsResponse">
????????????????<complexType>
????????????????????<sequence>
????????????????????????<element?name="getGreetingsReturn"?type="xsd:string"/>
????????????????????</sequence>
????????????????</complexType>
????????????</element>
??????????
????????</schema>
????</wsdl:types>
????<wsdl:message?name="getGreetingsRequest">
????????<wsdl:part?element="tns:getGreetings"?name="parameters"/>
????</wsdl:message>
????<wsdl:message?name="getGreetingsResponse">
????????<wsdl:part?element="tns:getGreetingsResponse"?name="parameters"/>
????</wsdl:message>
????<wsdl:portType?name="HelloWorld">
????????<wsdl:operation?name="getGreetings">
????????????<wsdl:input?message="tns:getGreetingsRequest"?name="getGreetingsRequest"/>
????????????<wsdl:output?message="tns:getGreetingsResponse"?name="getGreetingsResponse"/>
????????</wsdl:operation>
????</wsdl:portType>
????<wsdl:binding?name="HelloWorldSoapBinding"?type="tns:HelloWorld">
????????<wsdlsoap:binding?style="document"?transport="http://schemas.xmlsoap.org/soap/http"/>
????????<wsdl:operation?name="getGreetings">
????????????<wsdlsoap:operation?soapAction=""/>
????????????<wsdl:input?name="getGreetingsRequest">
????????????????<wsdlsoap:body?use="literal"/>
????????????</wsdl:input>
????????????<wsdl:output?name="getGreetingsResponse">
????????????????<wsdlsoap:body?use="literal"/>
????????????</wsdl:output>
????????</wsdl:operation>
????</wsdl:binding>
????<wsdl:service?name="HelloWorldService">
????????<wsdl:port?binding="tns:HelloWorldSoapBinding"?name="HelloWorldSoapPort">
????????????<wsdlsoap:address?location="http://localhost:8085/HelloWorldService"/>
????????</wsdl:port>
????</wsdl:service>
</wsdl:definitions>
xmlns:tns="http://helloworld"?xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
????name="helloworld">
????<wsdl:types>
????????<schema?elementFormDefault="qualified"?targetNamespace="http://helloworld"
???????? xmlns="http://www.w3.org/2001/XMLSchema">
????????????<element?name="getGreetings">
????????????????<complexType>
????????????????????<sequence>
????????????????????????<element?name="name"?type="xsd:string"/>
????????????????????</sequence>
????????????????</complexType>
????????????</element>
????????????<element?name="getGreetingsResponse">
????????????????<complexType>
????????????????????<sequence>
????????????????????????<element?name="getGreetingsReturn"?type="xsd:string"/>
????????????????????</sequence>
????????????????</complexType>
????????????</element>
??????????
????????</schema>
????</wsdl:types>
????<wsdl:message?name="getGreetingsRequest">
????????<wsdl:part?element="tns:getGreetings"?name="parameters"/>
????</wsdl:message>
????<wsdl:message?name="getGreetingsResponse">
????????<wsdl:part?element="tns:getGreetingsResponse"?name="parameters"/>
????</wsdl:message>
????<wsdl:portType?name="HelloWorld">
????????<wsdl:operation?name="getGreetings">
????????????<wsdl:input?message="tns:getGreetingsRequest"?name="getGreetingsRequest"/>
????????????<wsdl:output?message="tns:getGreetingsResponse"?name="getGreetingsResponse"/>
????????</wsdl:operation>
????</wsdl:portType>
????<wsdl:binding?name="HelloWorldSoapBinding"?type="tns:HelloWorld">
????????<wsdlsoap:binding?style="document"?transport="http://schemas.xmlsoap.org/soap/http"/>
????????<wsdl:operation?name="getGreetings">
????????????<wsdlsoap:operation?soapAction=""/>
????????????<wsdl:input?name="getGreetingsRequest">
????????????????<wsdlsoap:body?use="literal"/>
????????????</wsdl:input>
????????????<wsdl:output?name="getGreetingsResponse">
????????????????<wsdlsoap:body?use="literal"/>
????????????</wsdl:output>
????????</wsdl:operation>
????</wsdl:binding>
????<wsdl:service?name="HelloWorldService">
????????<wsdl:port?binding="tns:HelloWorldSoapBinding"?name="HelloWorldSoapPort">
????????????<wsdlsoap:address?location="http://localhost:8085/HelloWorldService"/>
????????</wsdl:port>
????</wsdl:service>
</wsdl:definitions>
* HelloWorldService.java
package?helloworld;
import?org.osoa.sca.annotations.Remotable;
@Remotable
public?interface?HelloWorldService?{
????public?String?getGreetings(String?name);
}
import?org.osoa.sca.annotations.Remotable;
@Remotable
public?interface?HelloWorldService?{
????public?String?getGreetings(String?name);
}
* HelloWorldImpl.java
package?helloworld;
import?org.osoa.sca.annotations.Service;
@Service(HelloWorldService.class)
public?class?HelloWorldImpl?implements?HelloWorldService?{
????public?String?getGreetings(String?name)?{
????????return?"hello?"+name;
????}
}
import?org.osoa.sca.annotations.Service;
@Service(HelloWorldService.class)
public?class?HelloWorldImpl?implements?HelloWorldService?{
????public?String?getGreetings(String?name)?{
????????return?"hello?"+name;
????}
}
這樣我們就可以寫一個類來創建一個SCA中的Domain,并由它來創建組件并發布為一個web服務。
public?class?HelloWorldServer?{
????public?static?void?main(String[]?args)?{
????????SCADomain?scaDomain?=?SCADomain.newInstance("","","Calculator.composite","Employee.composite");
????????try?{
????????????System.out.println("HelloWorld?server?started?(press?enter?to?shutdown)");
????????????System.in.read();
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????????scaDomain.close();
????????System.out.println("HelloWorld?server?stopped");
????}
}
????public?static?void?main(String[]?args)?{
????????SCADomain?scaDomain?=?SCADomain.newInstance("","","Calculator.composite","Employee.composite");
????????try?{
????????????System.out.println("HelloWorld?server?started?(press?enter?to?shutdown)");
????????????System.in.read();
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????????scaDomain.close();
????????System.out.println("HelloWorld?server?stopped");
????}
}
運行這個類,也就啟動了一個SCA的Domain,打開瀏覽器輸入http://localhost:8085/HelloWorldService? wsdl(注意這個是以binding.ws的uri為準的,和wsdl文件中的wsdlsoap:address的location無關,當然在這里它 們倆個是一致的),就可以看到發布的服務的wsdl描述,應該和前面的helloworld.wsdl的內容一樣。
下面我們寫一個groovy的客戶端來測試一下這個web服務,代碼大概象這樣(更多的信息可以參考http://docs.codehaus.org/display/GROOVY/Groovy+SOAP ,要運行下面的代碼需要先安裝一個groovy的用來除了soap的類庫):
import?groovy.net.soap.SoapClient
def?proxy?=?new?SoapClient("http://localhost:8085/HelloWorldService?wsdl")
println?proxy.getGreetings("yanhua")
def?proxy?=?new?SoapClient("http://localhost:8085/HelloWorldService?wsdl")
println?proxy.getGreetings("yanhua")
運行它,在控制臺可以看到hello yanhua了嗎?
有時候我們想查看一下客戶端和服務器之間來往的soap消息,我們可以用AXIS提供的一個叫TCPMonitor的工具。先下載AXIS,解壓后在AXIS的目錄下運行如下的命令:java -classpath ./lib/axis.jar org.apache.axis.utils.tcpmon,這樣會打開TCPMonitor這個工具。我們新建一個Listener,設置如下圖:
我們把剛才的groovy代碼改一下:
import?groovy.net.soap.SoapClient
def?proxy?=?new?SoapClient("http://localhost:8081/HelloWorldService?wsdl")
println?proxy.getGreetings("yanhua")
def?proxy?=?new?SoapClient("http://localhost:8081/HelloWorldService?wsdl")
println?proxy.getGreetings("yanhua")
運行它,在TCPMonitor中就可以看到往返的soap消息了,對這個例子來說分別是:
發送到服務器的soap消息:
POST?/HelloWorldService?HTTP/1.1
SOAPAction:?""
Content-Type:?text/xml;?charset=UTF-8
User-Agent:?Mozilla/4.0
(compatible;?MSIE?6.0;?Windows?NT?5.0;?XFire?Client?+http://xfire.codehaus.org)
Host:?127.0.0.1:8081
Expect:?100-continue
Content-Length:?308
<soap:Envelope?xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
? <getGreetings?xmlns="http://helloworld">
??? <name?xmlns="http://helloworld">yanhua</name>
? </getGreetings>
</soap:Body>
</soap:Envelope>
SOAPAction:?""
Content-Type:?text/xml;?charset=UTF-8
User-Agent:?Mozilla/4.0
(compatible;?MSIE?6.0;?Windows?NT?5.0;?XFire?Client?+http://xfire.codehaus.org)
Host:?127.0.0.1:8081
Expect:?100-continue
Content-Length:?308
<soap:Envelope?xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
? <getGreetings?xmlns="http://helloworld">
??? <name?xmlns="http://helloworld">yanhua</name>
? </getGreetings>
</soap:Body>
</soap:Envelope>
返回給客戶端的soap消息:
POST?/HelloWorldService?HTTP/1.1
SOAPAction:?""
Content-Type:?text/xml;?charset=UTF-8
User-Agent:?Mozilla/4.0?(compatible;?MSIE?6.0;?Windows?NT?5.0;?XFire?Client?+http://xfire.codehaus.org)
Host:?127.0.0.1:8081
Expect:?100-continue
Content-Length:?308
<soap:Envelope?xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
? <getGreetings?xmlns="http://helloworld">
??? <name?xmlns="http://helloworld">yanhua</name>
?? </getGreetings>
</soap:Body>
</soap:Envelope>
SOAPAction:?""
Content-Type:?text/xml;?charset=UTF-8
User-Agent:?Mozilla/4.0?(compatible;?MSIE?6.0;?Windows?NT?5.0;?XFire?Client?+http://xfire.codehaus.org)
Host:?127.0.0.1:8081
Expect:?100-continue
Content-Length:?308
<soap:Envelope?xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
? <getGreetings?xmlns="http://helloworld">
??? <name?xmlns="http://helloworld">yanhua</name>
?? </getGreetings>
</soap:Body>
</soap:Envelope>