Tuscany--是一個(gè)符合SCA標(biāo)準(zhǔn)的開源實(shí)現(xiàn),他能夠很容易地將一個(gè)服務(wù)綁定為一個(gè)Web Service:
          <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>

          要使服務(wù)發(fā)布成功,要保證有以下幾個(gè)文件:
          1. 一個(gè)預(yù)定義的wsdl文件,只要保證這個(gè)文件在classpath下就可以了。
          2. 一個(gè)java接口。
          3. 一個(gè)java類。
          下面列依次列出這幾個(gè)文件:
          * 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>


          * HelloWorldService.java
          package?helloworld;
          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;
          ????}
          }


          這樣我們就可以寫一個(gè)類來創(chuàng)建一個(gè)SCA中的Domain,并由它來創(chuàng)建組件并發(fā)布為一個(gè)web服務(wù)。
          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");
          ????}
          }

          運(yùn)行這個(gè)類,也就啟動(dòng)了一個(gè)SCA的Domain,打開瀏覽器輸入http://localhost:8085/HelloWorldService? wsdl(注意這個(gè)是以binding.ws的uri為準(zhǔn)的,和wsdl文件中的wsdlsoap:address的location無關(guān),當(dāng)然在這里它 們倆個(gè)是一致的),就可以看到發(fā)布的服務(wù)的wsdl描述,應(yīng)該和前面的helloworld.wsdl的內(nèi)容一樣。

          下面我們寫一個(gè)groovy的客戶端來測試一下這個(gè)web服務(wù),代碼大概象這樣(更多的信息可以參考http://docs.codehaus.org/display/GROOVY/Groovy+SOAP ,要運(yùn)行下面的代碼需要先安裝一個(gè)groovy的用來除了soap的類庫):

          import?groovy.net.soap.SoapClient
          def?proxy?
          =?new?SoapClient("http://localhost:8085/HelloWorldService?wsdl")
          println?proxy.getGreetings(
          "yanhua")


          運(yùn)行它,在控制臺(tái)可以看到hello yanhua了嗎?

          有時(shí)候我們想查看一下客戶端和服務(wù)器之間來往的soap消息,我們可以用AXIS提供的一個(gè)叫TCPMonitor的工具。先下載AXIS,解壓后在AXIS的目錄下運(yùn)行如下的命令:java -classpath ./lib/axis.jar org.apache.axis.utils.tcpmon,這樣會(huì)打開TCPMonitor這個(gè)工具。我們新建一個(gè)Listener,設(shè)置如下圖:


          我們把剛才的groovy代碼改一下:
          import?groovy.net.soap.SoapClient
          def?proxy?
          =?new?SoapClient("http://localhost:8081/HelloWorldService?wsdl")
          println?proxy.getGreetings(
          "yanhua")


          運(yùn)行它,在TCPMonitor中就可以看到往返的soap消息了,對這個(gè)例子來說分別是:
          發(fā)送到服務(wù)器的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>


          返回給客戶端的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>


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 永仁县| 杭锦后旗| 开远市| 苍南县| 巴彦县| 石嘴山市| 安宁市| 宜春市| 弥渡县| 疏附县| 郸城县| 广宁县| 新化县| 巴南区| 叙永县| 盐亭县| 郸城县| 靖江市| 浑源县| 沙田区| 类乌齐县| 珠海市| 怀集县| 和龙市| 神池县| 鹤庆县| 太仓市| 营山县| 承德市| 荆门市| 永寿县| 新沂市| 娄底市| 虞城县| 民乐县| 克东县| 洪泽县| 池州市| 广丰县| 乳山市| 孝感市|