Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          1、在%TOMCAT_HOME%\webapps中放入axis2.war,這個可以在www.apache.org下載
          2、啟動Tomcat,將該war包解開
          3、服務端代碼
            
          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(
          "參數:");
                  
          for (Object param : params) {
                      System.out.println(param.getClass());
                  }

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

                  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""得到的的""埃擔罰埃擔罰",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、啟動Tomcat
          8、測試訪問:http://localhost:8080/axis2/services/WSServer?wsdl
          9、執行客戶端代碼

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

          評論

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

          主站蜘蛛池模板: 基隆市| 汉源县| 湘西| 新巴尔虎左旗| 新建县| 鞍山市| 新和县| 都匀市| 南陵县| 读书| 睢宁县| 拜泉县| 大兴区| 苏州市| 宁武县| 闻喜县| 酉阳| 东乌珠穆沁旗| 固镇县| 迁安市| 肥城市| 休宁县| 芷江| 喀什市| 青浦区| 贡觉县| 北票市| 庄浪县| 商城县| 彩票| 邳州市| 定日县| 贡山| 五大连池市| 湄潭县| 房产| 神农架林区| 财经| 河西区| 维西| 九龙城区|