隨筆 - 251  文章 - 504  trackbacks - 0
          <2006年8月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          本博客系個(gè)人收集材料及學(xué)習(xí)記錄之用,各類(lèi)“大俠”勿擾!

          留言簿(14)

          隨筆分類(lèi)

          收藏夾

          My Favorite Web Sites

          名Bloger

          非著名Bloger

          搜索

          •  

          積分與排名

          • 積分 - 204405
          • 排名 - 283

          最新評(píng)論

          目前看到的有三種比較簡(jiǎn)單的方法:
          1使用靜態(tài)的stub
          通過(guò)wsdl2java工具,處理相應(yīng)ws的wsdl文件,我們可以得到遠(yuǎn)程ws的stub?直接調(diào)用這
          個(gè)stub即可
          AXIS提供的wsdl2java工具,如下:
          java?org.apache.axis.wsdl.WSDL2Java?(WSDL-file-URL)
          我們直接調(diào)用stub即可
          eclipse也有相應(yīng)的插件可以直接import?wsdl來(lái)產(chǎn)生stub,如果你安裝了EMF?all?in?one版本的eclipse?3.1,就可以在視圖里把Web?Service的相關(guān)視圖打開(kāi),接著就可以通過(guò)Web?Service的Wizard來(lái)創(chuàng)建出相關(guān)的Web?Service?Client或者Server

          2?Dynamic?Proxy
          根據(jù)遠(yuǎn)程wsdl,利用javax.xml.rpc.Service的getPort函數(shù),可以得到遠(yuǎn)程ws的一個(gè)?D
          ynamic?Proxy
          編寫(xiě)代理接口

          public?interface?HelloClientInterface
          extends?Java.rmi.Remote
          {
          ????public?String?getName(String?name)
          ????????throws?Java.rmi.RemoteException;
          }
          ?

          客戶(hù)端程序TestHelloClient.Java?

          import?Javax.xml.rpc.Service;
          import?Javax.xml.rpc.ServiceFactory;
          import?Java.net.URL;
          import?Javax.xml.namespace.QName;

          public?class?TestHelloClient?
          {
          ????public?static?void?main(String[]?args)
          ????????{
          ????????try
          ????????{
          ???String?wsdlUrl?=?"http://localhost:8080/axis/HelloClient.jws?wsdl";
          ???String?nameSpaceUri?=?"http://localhost:8080/axis/HelloClient.jws";
          String?serviceName?=?"HelloClientService";
          String?portName?=?"HelloClient";
          ServiceFactory?serviceFactory?=?ServiceFactory.newInstance();
          Service?afService?=?serviceFactory.createService(new?URL(wsdlUrl),
          ???????????new?QName(nameSpaceUri,?serviceName));
          ?HelloClientInterface?proxy?=?(HelloClientInterface)afService.getPort(new?QName(
          ??????????????nameSpaceUri,?portName),?HelloClientInterface.class);
          ?System.out.println("return?value?is?"+proxy.getName("john")?)?;
          ????????}catch(Exception?ex)
          ????????{
          ????????????ex.printStackTrace()?;
          ????????}
          ????}
          }
          ?

          3?DII
          Dynamic?Invocation?Interface?這個(gè)最好理解,比如你動(dòng)態(tài)獲得了一個(gè)類(lèi),只知道類(lèi)的
          名字,你要調(diào)用他的一個(gè)方法,只好使用reflection得到你要調(diào)用的類(lèi),相應(yīng)的參數(shù)信
          息,然后調(diào)用
          使用DII調(diào)用WS的時(shí)候,你知道的只是一個(gè)WSDL的地址,通過(guò)解析wsdl,你可以得到相應(yīng)
          的ws?endpoint的信息,然后通過(guò)javax.xml.rpc.Call的setOperationName,?addParam
          eter等函數(shù)來(lái)指定要調(diào)用的函數(shù),指定參數(shù),然后調(diào)用
          提供DII調(diào)用的原因是,我們有可能使用程序自動(dòng)的去動(dòng)態(tài)調(diào)用網(wǎng)絡(luò)上的WS,而這個(gè)WS的
          一切信息都是來(lái)自其WSDL
          ,只有通過(guò)DII,我們才有可能動(dòng)態(tài)的去調(diào)用這個(gè)ws
          例子:
          服務(wù)器端程序

          public?class?HelloClient
          {
          ????public?String?getName(String?name)
          ????{
          ????????return?"hello?"+name;
          ????}
          }
          把源碼拷貝到AXIS_HOME下,并改名為?HelloClient.jws

          客戶(hù)端程序
          import?org.apache.Axis.client.Call;
          import?org.apache.Axis.client.Service;
          import?Javax.xml.namespace.QName;
          import?Javax.xml.rpc.ServiceException;
          import?Java.net.MalformedURLException;
          import?Java.rmi.RemoteException;

          public?class?SayHelloClient2
          {
          ????public?static?void?main(String[]?args)
          ????????{
          ????????try
          ????????????????{
          ??String?endpoint?=??"http://localhost:8080/axis/HelloClient.jws";

          ????Service?service?=?new?Service();
          ????????????Call?call?=?null;

          ????????????call?=?(Call)?service.createCall();

          ????????????call.setOperationName
          ???(new?QName("http://localhost:8080/axis/HelloClient.jws",?"getName"));
          ????????????call.setTargetEndpointAddress(new?Java.net.URL(endpoint));

          ?String?ret?=?
          ?(String)?call.invoke(new?Object[]?
          ?????{"zhangsan"});
          ??System.out.println
          ??("return?value?is?"?+?ret);
          ????????}?
          ????????????????catch?(Exception?ex)
          ????????????????{
          ???????ex.printStackTrace();
          ????????}
          ????}
          }


          posted on 2006-08-11 09:21 matthew 閱讀(1079) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Web Services and SOA
          主站蜘蛛池模板: 牡丹江市| 陆丰市| 九龙城区| 清新县| 石柱| 淮阳县| 内江市| 五原县| 麻城市| 尼勒克县| 伊宁市| 嘉兴市| 万盛区| 远安县| 宜阳县| 竹山县| 含山县| 财经| 福泉市| 汕尾市| 万全县| 紫云| 惠来县| 买车| 垫江县| 长子县| 瑞丽市| 昌图县| 茶陵县| 礼泉县| 汽车| 松阳县| 奉新县| 米易县| 台中市| 阿瓦提县| 上林县| 镇康县| 邻水| 武穴市| 简阳市|