SOC基本实现研究

1.  Java:AXIS2 + Tomcat

** 安装后的目录如下所示

** 配置好相关的环境(包括Tomcat,AXIS2, 以及AXIS2中的Lib路径)

** 发布服务,调用服务

*** 发布

//Server: Calculator.java
public class Calculator
{
public double add(double x, double y)
{
return x + y;
}
public double sub(double x, double y)
{
return x - y;
}
public double mul(double x, double y)
{
return x * y;
}
public double div(double x, double y)
{
return x / y;
}
}

>>> javac Calculator.java
然后将生成的 .class 文件放到pojo文件夹(如果没有可以手动创建)中, 此时已经默认发布为 web service。

启动Tomcat,在浏览器中输入:http://localhost:8080/axis2/

如果成功则web服务已经成功发布。

*** 调用

// Client: Calculator.java
//此处用到的一些包是axis2中的
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class client {
public static void main(String[] args) throws Exception {

// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();

// 调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/Calculator");

options.setTo(targetEPR);

// add方法的参数值
Object[] add_args = new Object[] {1.0, 2.0 };

// 指定add方法返回值的数据类型
Class[] add_return = new Class[] { double.class };

// 指定要调用的add方法及WSDL文件的命名空间
QName add_method = new QName("http://ws.apache.org/axis2", "add");

// 调用add方法并输出该方法的返回值
System.out.println("1 + 2 = "+serviceClient.invokeBlocking(add_method, add_args,
add_return)[0]);

}
}

2. Python:SOAPpy模块

** 访问刚才用AXIS2发布的web服务(体现与编程环境的独立性,以及Web Service的松散耦合性)

import SOAPpy
#local client that calls local web service
url = "http://localhost:8080/axis2/services/Calculator?wsdl"
namespace = "http://ws.apache.org/axis2"
server = SOAPpy.SOAPProxy(url, namespace)

##if you want to Show SOAP exchange message, uncomment the following lines
#
proxy = SOAPpy.WSDL.Proxy(url)
#
proxy.soapproxy.config.dumpHeadersIn = 1
#
proxy.soapproxy.config.dumpHeadersOut = 1
#
proxy.soapproxy.config.dumpSOAPIn = 1
#
proxy.soapproxy.config.dumpSOAPOut = 1
#
proxy.soapproxy.config.debug = 9

print server.add(1,2)

** 访问远程的Web Service

import SOAPpy

#web service: wsdl url
#
content: country's and region's infomation
url = 'http://www.mobilefish.com/services/web_service/countries.php?wsdl'

server = SOAPpy.WSDL.Proxy(url)

# get all available methods
for method in server.methods:
print method

# get all countries' info: IANA(缩写形式), country_name
for res in server.getIANAInfo():
print res

# get a country's info through IANA
print server.countryInfoByIana('cn')

# get a country's all regions' info: latitude, longitude, ianacode, region_name
for res in server.regionsInfoByIana('cn'):
print res




posted @ 2011-11-28 22:31  Let it be!  阅读(403)  评论(0编辑  收藏  举报