Terry.Li-彬

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

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          首先確定已經安裝好axis2 并把相應的war包放到servlet容器下,我用的是 tomcat.
              容器自動部署war包,可以看到下面的目錄結構:
          axis2

               我們做的服務放在services目錄下就可以了。

          第一步:創建服務 StockQuoteService.java
          package samples.quickstart.service.pojo;

          import java.util.HashMap;

          public class StockQuoteService {
              
          private HashMap map = new HashMap();

              
          public double getPrice(String symbol) {
                  Double price 
          = (Double) map.get(symbol);
                  
          if (price != null{
                      
          return price.doubleValue();
                  }

                  
          return 42.00;
              }


              
          public void update(String symbol, double price) {
                  map.put(symbol, 
          new Double(price));
              }

              
              
          public String echo(String name){
                  
          return "Hello,"+name+"!";
              }

          }

           
             這個服務是一個POJO,不再解釋。

          第二步:創建服務描述 services.xml
          <service name="StockQuoteService" scope="application" targetNamespace="http://quickstart.samples/">
              
          <description>
                  Stock Quote Service
              
          </description>
              
          <messageReceivers>
                  
          <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
                                   class
          ="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
                  
          <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                                   class
          ="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
              
          </messageReceivers>
              
          <schema schemaNamespace="http://quickstart.samples/xsd"/>
              
          <parameter name="ServiceClass">samples.quickstart.service.pojo.StockQuoteService</parameter>
          </service>

          在服務描述 services.xml 中定義了幾個屬性值。

          第三步:組裝成axis2服務的文件目錄格式:
          StockQuoteService
             - META-INF
                - services.xml
             - samples
                - quickstart
                  - service
                    - pojo
                      - StockQuoteService.class

          文件夾名字為StockQuoteService,下面有兩個子文件夾META-INF,samples分別存放服務描述XML和服務的CLASS。

          第四步:將
          StockQuoteService文件夾拷到%TOMCAT_HOME%\webapps\axis2\WEB-INF\services下并啟動TOMCAT,
          這時訪問:http://localhost:8080/axis2/rest/StockQuoteService/echo?name=World 看看是不是得到:

          <ns:echoResponse>
          <ns:return>Hello,World!</ns:return>
          </ns:echoResponse>

          yes! 說明服務已經配置成功了,接下來創建一個客戶端訪問服務:

          客戶端需要兩個jar文件,可以在axis2安裝目錄的lib下找到,這兩個是: axiom-api-1.2.jar,axis2-kernel-1.1.jar

          AXIOMClient.java
          package samples.quickstart.clients;

          //axiom-api-1.2.jar
          import org.apache.axiom.om.OMAbstractFactory;
          import org.apache.axiom.om.OMElement;
          import org.apache.axiom.om.OMFactory;
          import org.apache.axiom.om.OMNamespace;
          //axis2-kernel-1.1.jar
          import org.apache.axis2.Constants;
          import org.apache.axis2.addressing.EndpointReference;
          import org.apache.axis2.client.Options;
          import org.apache.axis2.client.ServiceClient;

          public class AXIOMClient {

              
          private static EndpointReference targetEPR = 
                  
          new EndpointReference(
                                        
          "http://localhost:8080/axis2/services/StockQuoteService");

              
          public static OMElement getPricePayload(String symbol) {
                  OMFactory fac 
          = OMAbstractFactory.getOMFactory();
                  OMNamespace omNs 
          = fac.createOMNamespace(
                                                           
          "http://quickstart.samples/xsd""tns");

                  OMElement method 
          = fac.createOMElement("getPrice", omNs);
                  OMElement value 
          = fac.createOMElement("symbol", omNs);
                  value.addChild(fac.createOMText(value, symbol));
                  method.addChild(value);
                  
          return method;
              }


              
          public static OMElement echoServie(String name){
                  OMFactory fac 
          = OMAbstractFactory.getOMFactory();
                  OMNamespace omNs 
          = fac.createOMNamespace(
                          
          "http://quickstart.samples/xsd""tns");
                  
                  OMElement method 
          = fac.createOMElement("echo", omNs);
                  OMElement value 
          = fac.createOMElement("name", omNs);
                  value.addChild(fac.createOMText(value, name));
                  method.addChild(value);
                  
          return method;
              }

              
          public static OMElement updatePayload(String symbol, double price) {
                  OMFactory fac 
          = OMAbstractFactory.getOMFactory();
                  OMNamespace omNs 
          = fac.createOMNamespace(
                                                           
          "http://quickstart.samples/xsd""tns");

                  OMElement method 
          = fac.createOMElement("update", omNs);

                  OMElement value1 
          = fac.createOMElement("symbol", omNs);
                  value1.addChild(fac.createOMText(value1, symbol));
                  method.addChild(value1);

                  OMElement value2 
          = fac.createOMElement("price", omNs);
                  value2.addChild(fac.createOMText(value2,
                                                   Double.toString(price)));
                  method.addChild(value2);
                  
          return method;
              }


              
          public static void main(String[] args) {
                  
          try {
                      OMElement echoServie 
          = echoServie("Bene.Wu");
                      OMElement getPricePayload 
          = getPricePayload("WSO");
                      OMElement updatePayload 
          = updatePayload("WSO"128.42);
                      Options options 
          = new Options();
                      options.setTo(targetEPR);
                      options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

                      ServiceClient sender 
          = new ServiceClient();
                      sender.setOptions(options);

                      OMElement resultEcho 
          = sender.sendReceive(echoServie);
                      String responseEcho 
          = resultEcho.getFirstElement().getText();
                      System.err.println(responseEcho);
                      
                      sender.fireAndForget(updatePayload);
                      System.err.println(
          "done");
                      
                      OMElement result 
          = sender.sendReceive(getPricePayload);
                      String response 
          = result.getFirstElement().getText();
                      System.err.println(
          "Current price of WSO: " + response);

                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }

              
          }



          運行一下,看看結果是不是:
          Hello,Bene.Wu!
          done
          Current price of WSO: 128.42

              哈,bingo!  axis2一共有五種創建服務的方式和四種創建客戶端的方式,我覺得只要選擇自己適合的方式就可以了,沒有必要糾纏在技術細節上。接下來我打算繼續學習和研究axis2的一些實現細節以及OSGi插件在axis2中的應用。

              axis2和OSGi一樣 帶來的不僅僅是服務實現技術,而是對軟件架構的沖擊。
          我覺得SOA時代已經到來,你準備好了嗎?
          posted on 2007-11-21 18:02 禮物 閱讀(1249) 評論(0)  編輯  收藏 所屬分類: web service
          主站蜘蛛池模板: 静乐县| 南靖县| 宜春市| 利津县| 新乡市| 哈尔滨市| 铜山县| 贵德县| 尤溪县| 喜德县| 增城市| 双柏县| 乌兰察布市| 临颍县| 化州市| 正镶白旗| 翁源县| 鸡西市| 宾阳县| 南澳县| 黔南| 苏尼特左旗| 郧西县| 江永县| 长宁区| 教育| 苏尼特右旗| 万全县| 社旗县| 博湖县| 通辽市| 延边| 安庆市| 洪江市| 麦盖提县| 汶上县| 兴城市| 诸城市| 中阳县| 铜梁县| 淮北市|