公司有需求是这样的:一台服务器上用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
关于上面一些方法的含意可以看这里:
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下。
发布服务:
写一个deploy.wsdd
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
生成:
用java写个client测试:
输出:you id is:182 you name is:李世民
oracle调用:
输出:you id is:22 you name is:lui
数据库是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
- CREATEORREPLACEFUNCTIONget_city_from_zipcode(ssidININTEGER,ssnameINVARCHAR2)
- RETURNVARCHAR2
- AS
- l_serviceUTL_DBWS.service;
- l_callUTL_DBWS.call;
- l_resultANYDATA;
- l_wsdl_urlVARCHAR2(32767);
- l_namespaceVARCHAR2(32767);
- l_service_qnameUTL_DBWS.qname;
- l_port_qnameUTL_DBWS.qname;
- l_operation_qnameUTL_DBWS.qname;
- l_input_paramsUTL_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);
- RETURNANYDATA.AccessVarchar2(l_result);
- END;
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下。
- packagegjs;
- publicclassOracleSay{
- publicStringtest(intid,Stringname){
- return"youidis:"+id+""+"younameis:"+name+"";
- }
- }
package gjs;
public class OracleSay {
public String test(int id,String name){
return "you id is:"+id+" "+"you name is:"+name+"";
}
}
发布服务:
写一个deploy.wsdd
- <deploymentxmlns="http://xml.apache.org/axis/wsdd/"xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
- <servicename="OracleSay"provider="java:RPC">
- <parametername="allowedMethods"value="test"/>
- <parametername="className"value="gjs.OracleSay"/>
- </service>
- </deployment>
<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:definitionstargetNamespace="http://localhost:8888/axis/services/OracleSay">
- <!--
- WSDLcreatedbyApacheAxisversion:1.4
- BuiltonApr22,2006(06:55:48PDT)
- -->
- <wsdl:messagename="testResponse">
- <wsdl:partname="testReturn"type="xsd:string"/>
- </wsdl:message>
- <wsdl:messagename="testRequest">
- <wsdl:partname="id"type="xsd:int"/>
- <wsdl:partname="name"type="xsd:string"/>
- </wsdl:message>
- <wsdl:portTypename="OracleSay">
- <wsdl:operationname="test"parameterOrder="idname">
- <wsdl:inputmessage="impl:testRequest"name="testRequest"/>
- <wsdl:outputmessage="impl:testResponse"name="testResponse"/>
- </wsdl:operation>
- </wsdl:portType>
- <wsdl:bindingname="OracleSaySoapBinding"type="impl:OracleSay">
- <wsdlsoap:bindingstyle="rpc"transport="http://schemas.xmlsoap.org/soap/http"/>
- <wsdl:operationname="test">
- <wsdlsoap:operationsoapAction=""/>
- <wsdl:inputname="testRequest">
- <wsdlsoap:bodyencodingStyle="http://schemas.xmlsoap.org/soap/encoding/"namespace="http://gjs"use="encoded"/>
- </wsdl:input>
- <wsdl:outputname="testResponse">
- <wsdlsoap:bodyencodingStyle="http://schemas.xmlsoap.org/soap/encoding/"namespace="http://localhost:8888/axis/services/OracleSay"use="encoded"/>
- </wsdl:output>
- </wsdl:operation>
- </wsdl:binding>
- <wsdl:servicename="OracleSayService">
- <wsdl:portbinding="impl:OracleSaySoapBinding"name="OracleSay">
- <wsdlsoap:addresslocation="http://localhost:8888/axis/services/OracleSay"/>
- </wsdl:port>
- </wsdl:service>
- </wsdl:definitions>
<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测试:
- publicstaticvoidmain(String[]args)throwsServiceException,MalformedURLException,RemoteException{
- StringendPoint="http://localhost:8888/axis/services/OracleSay";
- Serviceservice=newService();
- Callcall=null;
- call=(Call)service.createCall();
- call.setOperationName(newQName(endPoint,"test"));
- call.setTargetEndpointAddress(newjava.net.URL(endPoint));
- Stringstr=(String)call.invoke(newObject[]{182,"李世民"});
- System.out.println(str);
- }
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调用:
- SELECTget_city_from_zipcode(22,'lui')FROMdual;
SELECT get_city_from_zipcode(22,'lui') FROM dual;
输出:you id is:22 you name is:lui
本文介绍如何使用Oracle 10g通过WebService实现数据同步。具体步骤包括导入必要的工具包、创建WebService客户端和服务端、以及通过SQL调用WebService进行数据交互。
2442

被折叠的 条评论
为什么被折叠?



