Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學(xué);靜其心,可悟天下之理;恒其心,可成天下之業(yè)。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          1、在%TOMCAT_HOME%\webapps中放入axis2.war,這個(gè)可以在www.apache.org下載
          2、啟動(dòng)Tomcat,將該war包解開
          3、服務(wù)端代碼
            
          import java.io.ByteArrayInputStream;
          import java.io.ByteArrayOutputStream;
          import java.io.IOException;
          import java.io.ObjectInputStream;
          import java.io.ObjectOutputStream;

          /**
           * @company      
           *
           * @description 
           *
           * 
          @author      Terry.B.Li
           *
           * @date        2009.1.7
           
          */
          public class WSServer {
              
          public byte[] invoke(String id, byte[] bytes) {
                  
                  
          byte[] result = null;

                  System.out.println(
          "id:" + id);

                  Object[] params 
          = byteConvertObj(bytes);
                  System.out.print(
          "參數(shù):");
                  
          for (Object param : params) {
                      System.out.println(param.getClass());
                  }

                  
          // 通過 Axis2 反射調(diào)用返回的對象
                  Object resultObj = "返回值";
                  
                  
          //根據(jù)功能號 ID 解析調(diào)用方法

                  ByteArrayOutputStream arrayOutputStream 
          = new ByteArrayOutputStream();
                  ObjectOutputStream objectOutputStream 
          = null;
                  
          try {
                      objectOutputStream 
          = new ObjectOutputStream(arrayOutputStream);
                      objectOutputStream.writeObject(resultObj);
                      
                      result 
          = arrayOutputStream.toByteArray();
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  } 
          finally {
                      
          try {
                          objectOutputStream.close();
                      } 
          catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
                   
                  
          return result;
              }

              
          private Object[] byteConvertObj(byte[] bytes) {
                  Object[] result 
          = null;
                  ByteArrayInputStream byteArrayInputStream 
          = null;
                  ObjectInputStream objectInputStream 
          = null;
                  
          try {
                      byteArrayInputStream 
          = new ByteArrayInputStream(bytes);
                      objectInputStream 
          = new ObjectInputStream(byteArrayInputStream);
                      result 
          = (Object[]) objectInputStream.readObject();
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  } 
          catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  }
                  
          return result;
              }
          }

          4、將該代碼編譯后放入%TOMCAT_HOME%\webapps\axis2\WEB-INF\pojo下
          5、客戶端代碼如下
          package com.newegg.lab.ws.client;

          import java.io.ByteArrayInputStream;
          import java.io.ByteArrayOutputStream;
          import java.io.IOException;
          import java.io.ObjectInputStream;
          import java.io.ObjectOutputStream;
          import java.io.Serializable;

          import javax.xml.namespace.QName;

          import org.apache.axis2.addressing.EndpointReference;
          import org.apache.axis2.client.Options;
          import org.apache.axis2.rpc.client.RPCServiceClient;
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;

          public class WSClient implements Serializable{

              
          private static final long serialVersionUID = -8513162370253557533L;

              
          private static final Log log = LogFactory.getLog(WSClient.class); 
              
              
          private static EndpointReference endpointReference = new EndpointReference("http://localhost:8080/axis2/services/WSServer");
              
              
          private static String namespace = "http://ws.apache.org/axis2";

              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  
          try {
                      log.info(invoke(
          "123456""aaa""得到的的""埃擔(dān)罰埃擔(dān)罰",34,new WSClient()));
                      } 
          catch (Exception e) {
                          e.printStackTrace();
                      }
                  log.info(
          "OK");
              }
              
              
          private static Object invoke(String id,Object params){
                  Object result 
          = null;
                  
          try {
                      RPCServiceClient serviceClient 
          = new RPCServiceClient();
                      Options options 
          = serviceClient.getOptions();
                      options.setTo(endpointReference);
                      QName qname 
          = new QName(namespace,"invoke");
                      Class[] clz 
          = new Class[]{byte[].class};
                      Object[] _params 
          = new Object[]{id,objConvertByte((Object[])params)};
                      Object[] results 
          =  serviceClient.invokeBlocking(qname, _params, clz);
                      
          byte[] bytes = (byte[]) results[0];
                      result 
          = byteConvertObj(bytes);
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                  }
                  
          return result;
              }
              
              
          private static byte[] objConvertByte(Object obj){
                  
          byte[] result = null;
                  ByteArrayOutputStream byteArrayOutputStream 
          = null;
                  ObjectOutputStream objectOutputStream 
          = null;
                  
          try {
                      byteArrayOutputStream 
          = new ByteArrayOutputStream();
                      objectOutputStream  
          = new ObjectOutputStream(byteArrayOutputStream);
                      objectOutputStream.writeObject(obj);
                      result 
          = byteArrayOutputStream.toByteArray();
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  } 
          finally {
                      
          try {
                          objectOutputStream.close();
                          byteArrayOutputStream.close();
                      } 
          catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
                  
          return result;
              }
              
              
          private static Object byteConvertObj(byte[] bytes){
                  Object result 
          = null;
                  ByteArrayInputStream byteArrayInputStream 
          = null;
                  ObjectInputStream objectInputStream 
          = null;
                  
          try {
                      byteArrayInputStream 
          = new ByteArrayInputStream(bytes);
                      objectInputStream 
          = new ObjectInputStream(byteArrayInputStream);
                      result 
          = objectInputStream.readObject();
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  } 
          catch (ClassNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  
          return result;
              }
          }

          6、將客戶端代碼打包放入%TOMCAT_HOME%\webapps\axis2\WEB-INF\lib下
          7、啟動(dòng)Tomcat
          8、測試訪問:http://localhost:8080/axis2/services/WSServer?wsdl
          9、執(zhí)行客戶端代碼

          posted on 2009-01-08 02:25 禮物 閱讀(1020) 評論(1)  編輯  收藏 所屬分類: web service

          評論

          # re: (原創(chuàng))用Axis2寫的通用WebService組件 2009-08-10 15:42 禮物
          太強(qiáng)了  回復(fù)  更多評論
            

          主站蜘蛛池模板: 龙游县| 平原县| 大庆市| 枝江市| 冕宁县| 江源县| 蓝山县| 高州市| 莎车县| 商城县| 孟连| 怀安县| 镇平县| 阿克| 华宁县| 石景山区| 东丰县| 廊坊市| 万山特区| 梅河口市| 金坛市| 抚宁县| 任丘市| 昭平县| 嘉兴市| 连山| 察隅县| 茌平县| 错那县| 儋州市| 裕民县| 昌平区| 东阳市| 增城市| 澄城县| 改则县| 怀安县| 南开区| 河东区| 赤峰市| 于都县|