现在的位置: 首页 > 综合 > 正文

oracle webservice axis

2018年08月11日 ⁄ 综合 ⁄ 共 9211字 ⁄ 字号 评论关闭
公司有需求是这样的:一台服务器上用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

Sql代码 复制代码
  1. CREATE OR REPLACE FUNCTION get_city_from_zipcode (ssid  IN  INTEGER,ssname IN VARCHAR2)   
  2.   RETURN VARCHAR2   
  3. AS  
  4.   l_service  UTL_DBWS.service;   
  5.   l_call     UTL_DBWS.call;   
  6.   l_result   ANYDATA;   
  7.      
  8.   l_wsdl_url         VARCHAR2(32767);   
  9.   l_namespace        VARCHAR2(32767);   
  10.   l_service_qname    UTL_DBWS.qname;   
  11.   l_port_qname       UTL_DBWS.qname;   
  12.   l_operation_qname  UTL_DBWS.qname;   
  13.   l_input_params     UTL_DBWS.anydata_list;   
  14. BEGIN  
  15.   l_wsdl_url        := 'http://localhost:8888/axis/services/OracleSay?wsdl';   
  16.   l_namespace       := 'http://localhost:8888/axis/services/OracleSay';   
  17.   
  18.   l_service_qname   := UTL_DBWS.to_qname(l_namespace, 'OracleSayService');   
  19.   l_port_qname      := UTL_DBWS.to_qname(l_namespace, 'OracleSay');   
  20.   l_operation_qname := UTL_DBWS.to_qname(l_namespace, 'test');   
  21.   
  22.   l_service := UTL_DBWS.create_service (   
  23.     wsdl_document_location => URIFACTORY.getURI(l_wsdl_url),   
  24.     service_name           => l_service_qname);   
  25.   
  26.   l_call := UTL_DBWS.create_call (   
  27.     service_handle => l_service,   
  28.     port_name      => l_port_qname,   
  29.     operation_name => l_operation_qname);   
  30.   
  31.   l_input_params(0) := ANYDATA.CONVERTNUMBER(ssid);   
  32.   l_input_params(1) := ANYdATA.CONVERTVARCHAR2(ssname);   
  33.   l_result := UTL_DBWS.invoke (    
  34.     call_handle  => l_call,   
  35.     input_params => l_input_params);   
  36.   
  37.   UTL_DBWS.release_call (call_handle => l_call);   
  38.   UTL_DBWS.release_service (service_handle => l_service);   
  39.   
  40.   RETURN ANYDATA.AccessVarchar2(l_result);   
  41. 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下。

Java代码 复制代码
  1. package gjs;   
  2.   
  3. public class OracleSay {   
  4.     public String test(int id,String name){   
  5.         return "you id is:"+id+" "+"you name is:"+name+"";   
  6.     }   
  7. }  

发布服务:
写一个deploy.wsdd

Xml代码 复制代码
  1. <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">  
  2.  <service name="OracleSay" provider="java:RPC">  
  3.   <parameter name="allowedMethods" value="test"/>  
  4.   <parameter name="className" value="gjs.OracleSay"/>  
  5.  </service>  
  6. </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
生成:

Xml代码 复制代码
  1. <wsdl:definitions targetNamespace="http://localhost:8888/axis/services/OracleSay">  
  2.   
  3. <!--   
  4. WSDL created by Apache Axis version: 1.4   
  5. Built on Apr 22, 2006 (06:55:48 PDT)   
  6. -->  
  7. <wsdl:message name="testResponse">  
  8. <wsdl:part name="testReturn" type="xsd:string"/>  
  9. </wsdl:message>  
  10. <wsdl:message name="testRequest">  
  11. <wsdl:part name="id" type="xsd:int"/>  
  12. <wsdl:part name="name" type="xsd:string"/>  
  13. </wsdl:message>  
  14. <wsdl:portType name="OracleSay">  
  15. <wsdl:operation name="test" parameterOrder="id name">  
  16. <wsdl:input message="impl:testRequest" name="testRequest"/>  
  17. <wsdl:output message="impl:testResponse" name="testResponse"/>  
  18. </wsdl:operation>  
  19. </wsdl:portType>  
  20. <wsdl:binding name="OracleSaySoapBinding" type="impl:OracleSay">  
  21. <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>  
  22. <wsdl:operation name="test">  
  23. <wsdlsoap:operation soapAction=""/>  
  24. <wsdl:input name="testRequest">  
  25. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://gjs" use="encoded"/>  
  26. </wsdl:input>  
  27. <wsdl:output name="testResponse">  
  28. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8888/axis/services/OracleSay" use="encoded"/>  
  29. </wsdl:output>  
  30. </wsdl:operation>  
  31. </wsdl:binding>  
  32. <wsdl:service name="OracleSayService">  
  33. <wsdl:port binding="impl:OracleSaySoapBinding" name="OracleSay">  
  34. <wsdlsoap:address location="http://localhost:8888/axis/services/OracleSay"/>  
  35. </wsdl:port>  
  36. </wsdl:service>  
  37. </wsdl:definitions>  

用java写个client测试:

Java代码 复制代码
  1. public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException {   
  2.     String endPoint="http://localhost:8888/axis/services/OracleSay";   
  3.     Service service=new Service();   
  4.     Call call=null;   
  5.     call=(Call)service.createCall();   
  6.     call.setOperationName(new QName(endPoint,"test"));   
  7.     call.setTargetEndpointAddress(new java.net.URL(endPoint));   
  8.     String str = (String) call.invoke(new Object[]{182,"李世民"});   
  9.     System.out.println(str);   
  10.   
  11. }  

输出:you id is:182 you name is:李世民
oracle调用:

Sql代码 复制代码
  1. SELECT get_city_from_zipcode(22,'lui'FROM dual;  

输出:you id is:22 you name is:lui

抱歉!评论已关闭.