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 >