公司有需求是这样的:一台服务器上用MemCached存一了些数据,另一台服务器的数据库上也存了数据,MemCached是数据库的映象,所以问题是数据同步。
数据库是oracle10g,暂考虑以webservice实现,oracle10g中可以使用webservice。
测试例子:
需要导入dbws-callout-utility-10131.zip地址:
http://download.oracle.com/technology/sample_code/tech/java/jsp/dbws-callout-utility-10131.zip
导入方法是这样的:
The jar file can be loaded into the SYS schema for everyone to access, or into an individual schema that needs access to the web client.
# Load into the SYS schema.
export PATH=/u01/app/oracle/product/10.2.0/db_1/bin:$PATH
cd /u01/app/oracle/product/10.2.0/db_1/sqlj/lib
# 10gR2
loadjava -u sys/password -r -v -f -genmissing -s -grant public dbwsclientws.jar dbwsclientdb102.jar
# 11g
loadjava -u sys/password -r -v -f -genmissing -s -grant public dbwsclientws.jar dbwsclientdb11.jar
# Load into an individual schema.
export PATH=/u01/app/oracle/product/10.2.0/db_1/bin:$PATH
cd /u01/app/oracle/product/10.2.0/db_1/sqlj/lib
# 10gR2
loadjava -u scott/tiger -r -v -f -genmissing dbwsclientws.jar dbwsclientdb102.jar
# 11g
loadjava -u scott/tiger -r -v -f -genmissing dbwsclientws.jar dbwsclientdb11.jar
导入完成后写如下SQL
CREATE OR REPLACE FUNCTION get_city_from_zipcode (ssid IN INTEGER,ssname IN VARCHAR2)
RETURN VARCHAR2
AS
l_service UTL_DBWS.service;
l_call UTL_DBWS.call;
l_result ANYDATA;
l_wsdl_url VARCHAR2(32767);
l_namespace VARCHAR2(32767);
l_service_qname UTL_DBWS.qname;
l_port_qname UTL_DBWS.qname;
l_operation_qname UTL_DBWS.qname;
l_input_params UTL_DBWS.anydata_list;
BEGIN
l_wsdl_url := 'http://localhost:8888/axis/services/OracleSay?wsdl';
l_namespace := 'http://localhost:8888/axis/services/OracleSay';
l_service_qname := UTL_DBWS.to_qname(l_namespace, 'OracleSayService');
l_port_qname := UTL_DBWS.to_qname(l_namespace, 'OracleSay');
l_operation_qname := UTL_DBWS.to_qname(l_namespace, 'test');
l_service := UTL_DBWS.create_service (
wsdl_document_location => URIFACTORY.getURI(l_wsdl_url),
service_name => l_service_qname);
l_call := UTL_DBWS.create_call (
service_handle => l_service,
port_name => l_port_qname,
operation_name => l_operation_qname);
l_input_params(0) := ANYDATA.CONVERTNUMBER(ssid);
l_input_params(1) := ANYdATA.CONVERTVARCHAR2(ssname);
l_result := UTL_DBWS.invoke (
call_handle => l_call,
input_params => l_input_params);
UTL_DBWS.release_call (call_handle => l_call);
UTL_DBWS.release_service (service_handle => l_service);
RETURN ANYDATA.AccessVarchar2(l_result);
END;
关于上面一些方法的含意可以看这里:
http://download-east.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_dbws.htm#i1001229
接下来是server端,使用tomcat6+axis1.4实现,activation.jar,mail.jar,xerces.jar放到tomcat的lib下,建一个web项目然后把axis包中webapps\axis下的东西拷到WebRoot下。
package gjs;
public class OracleSay {
public String test(int id,String name){
return "you id is:"+id+" "+"you name is:"+name+"";
}
}
发布服务:
写一个deploy.wsdd
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="OracleSay" provider="java:RPC">
<parameter name="allowedMethods" value="test"/>
<parameter name="className" value="gjs.OracleSay"/>
</service>
</deployment>
axis.jar里有org.apache.axis.client.AdminClient,run它带参数-lhttp://localhost:8888/axis/services/ src/deploy.wsdd
会在WEB-INF下生成一个server-config.wsdd,如果已经有这个的就直接在
server-config.wsdd中加入:
<service name="OracleSay" provider="java:RPC">
<parameter name="allowedMethods" value="test"/>
<parameter name="className" value="gjs.OracleSay"/>
</service>
想看生成的wsdl在浏览器里http://localhost:8888/axis/services/OracleSay?wsdl
生成:
<wsdl:definitions targetNamespace="http://localhost:8888/axis/services/OracleSay">
<!--
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
-->
<wsdl:message name="testResponse">
<wsdl:part name="testReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="testRequest">
<wsdl:part name="id" type="xsd:int"/>
<wsdl:part name="name" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="OracleSay">
<wsdl:operation name="test" parameterOrder="id name">
<wsdl:input message="impl:testRequest" name="testRequest"/>
<wsdl:output message="impl:testResponse" name="testResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="OracleSaySoapBinding" type="impl:OracleSay">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="test">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="testRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://gjs" use="encoded"/>
</wsdl:input>
<wsdl:output name="testResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8888/axis/services/OracleSay" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="OracleSayService">
<wsdl:port binding="impl:OracleSaySoapBinding" name="OracleSay">
<wsdlsoap:address location="http://localhost:8888/axis/services/OracleSay"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
用java写个client测试:
public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException {
String endPoint="http://localhost:8888/axis/services/OracleSay";
Service service=new Service();
Call call=null;
call=(Call)service.createCall();
call.setOperationName(new QName(endPoint,"test"));
call.setTargetEndpointAddress(new java.net.URL(endPoint));
String str = (String) call.invoke(new Object[]{182,"李世民"});
System.out.println(str);
}
输出:you id is:182 you name is:李世民
oracle调用:
SELECT get_city_from_zipcode(22,'lui') FROM dual;
输出:you id is:22 you name is:lui