隨筆 - 312, 文章 - 14, 評論 - 1393, 引用 - 0
          數據加載中……

          WebService大講堂之Axis2(9):編寫Axis2模塊(Module)

          本文為原創,如需轉載,請注明作者和出處,謝謝!

          上一篇:WebService大講堂之Axis2(8):異步調用WebService

              Axis2可以通過模塊(Module)進行擴展。Axis2模塊至少需要有兩個類,這兩個類分別實現了ModuleHandler接口。開發和使用一個Axis2模塊的步驟如下:

          1. 編寫實現Module接口的類。Axis2模塊在進行初始化、銷毀等動作時會調用該類中相應的方法)。

          2. 編寫實現Handler接口的類。該類是Axis2模塊的業務處理類。

          3. 編寫module.xml文件。該文件放在META-INF目錄中,用于配置Axis2模塊。

          4. axis2.xml文件中配置Axis2模塊。

          5. services.xml文件中配置Axis2模塊。每一個Axis2模塊都需要使用<module>元素引用才能使用。

          6. 發布Axis2模塊。需要使用jar命令將Axis2模塊壓縮成.mar包(文件擴展名必須是.mar),然后將.mar文件放在

          <Tomcat安裝目錄>\webapps\axis2\WEB-INF\modules目錄中。   
              先來編寫一個WebService類,代碼如下:

          package service;

          public class MyService
          {
              
          public String getGreeting(String name)
              {
                  
          return "您好 " + name;
              }
          }

              下面我們來編寫一個記錄請求和響應SOAP消息的Axis2模塊。當客戶端調用WebService方法時,該Axis2模塊會將請求和響應SOAP消息輸出到Tomcat控制臺上。

          1步:編寫LoggingModule

              LoggingModule類實現了Module接口,代碼如下:

          package module;

          import org.apache.axis2.AxisFault;
          import org.apache.axis2.context.ConfigurationContext;
          import org.apache.axis2.description.AxisDescription;
          import org.apache.axis2.description.AxisModule;
          import org.apache.axis2.modules.Module;
          import org.apache.neethi.Assertion;
          import org.apache.neethi.Policy;

          public class LoggingModule implements Module
          {
              
          // initialize the module
              public void init(ConfigurationContext configContext, AxisModule module)
                      
          throws AxisFault
              {
                  System.out.println(
          "init");
              }
              
          public void engageNotify(AxisDescription axisDescription) throws AxisFault
              {
              }
              
          // shutdown the module
              public void shutdown(ConfigurationContext configurationContext)
                      
          throws AxisFault
              {
                  System.out.println(
          "shutdown");
              }
              
          public String[] getPolicyNamespaces()
              {
                  
          return null;
              }
              
          public void applyPolicy(Policy policy, AxisDescription axisDescription)
                      
          throws AxisFault
              {
              }
              
          public boolean canSupportAssertion(Assertion assertion)
              {
                  
          return true;
              }
          }

              在本例中LoggingModule類并沒實現實際的功能,但該類必須存在。當Tomcat啟動時會裝載該Axis2模塊,同時會調用LoggingModule類的init方法,并在Tomcat控制臺中輸出“init”。

          2步:編寫LogHandler

              LogHandler類實現了Handler接口,代碼如下:

          package module;

          import org.apache.axis2.AxisFault;
          import org.apache.axis2.context.MessageContext;
          import org.apache.axis2.engine.Handler;
          import org.apache.axis2.handlers.AbstractHandler;
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;

          public class LogHandler extends AbstractHandler implements Handler
          {
              
          private static final Log log = LogFactory.getLog(LogHandler.class);
              
          private String name;
              
          public String getName()
              {
                  
          return name;
              }
              
          public InvocationResponse invoke(MessageContext msgContext)
                      
          throws AxisFault
              {
                  
          //  向Tomcat控制臺輸出請求和響應SOAP消息
                  log.info(msgContext.getEnvelope().toString());
                  
          return InvocationResponse.CONTINUE;
              }
              
          public void revoke(MessageContext msgContext)
              {
                  log.info(msgContext.getEnvelope().toString());
              }
              
          public void setName(String name)
              {
                  
          this.name = name;
              }
          }

              LogHandler類的核心方法是invoke,當使用該Axis2模塊的WebService的方法被調用時,LogHandler類的invoke方法被調用。   

          3步:編寫module.xml文件   

              在META-INF目錄中建立一個module.xml文件,內容如下:
          <module name="logging" class="module.LoggingModule">
              
          <InFlow>
                  
          <handler name="InFlowLogHandler" class="module.LogHandler">
                      
          <order phase="loggingPhase"/>
                  
          </handler>
              
          </InFlow>
              
          <OutFlow>
                  
          <handler name="OutFlowLogHandler" class="module.LogHandler">
                      
          <order phase="loggingPhase"/> 
                  
          </handler>
              
          </OutFlow>

              
          <OutFaultFlow>
                  
          <handler name="FaultOutFlowLogHandler" class="module.LogHandler">
                      
          <order phase="loggingPhase"/>
                  
          </handler>
              
          </OutFaultFlow>
              
          <InFaultFlow>
                  
          <handler name="FaultInFlowLogHandler" class="module.LogHandler">
                      
          <order phase="loggingPhase"/>
                  
          </handler>
              
          </InFaultFlow>
          </module>

          4步:在axis2.xml文件中配置Axis2模塊

              打開axis2.xml文件,分別在如下四個<phaseOrder>元素中加入<phase name="loggingPhase"/>

          <phaseOrder type="InFlow">
               
              <phase name="soapmonitorPhase"/>
              <phase name="loggingPhase"/>
          </phaseOrder>
          <phaseOrder type="OutFlow">
               
              
          <phase name="Security"/>
              
          <phase name="loggingPhase"/>
          </phaseOrder>
          <phaseOrder type="InFaultFlow">
               
              
          <phase name="soapmonitorPhase"/>
              
          <phase name="loggingPhase"/>
          </phaseOrder>
          <phaseOrder type="OutFaultFlow">
               
              
          <phase name="Security"/>
              
          <phase name="loggingPhase"/>
          </phaseOrder>

          5步:在services.xml文件中引用logging模塊

              services.xml文件的內容如下:

          <service name="myService">
              
          <description>
                  使用logging模塊
              
          </description>
              
          <!--  引用logging模塊  -->
              
          <module ref="logging"/>
              
          <parameter name="ServiceClass">
                  service.MyService   
              
          </parameter>
              
          <messageReceivers>
                  
          <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                      class
          ="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
              
          </messageReceivers>
          </service>

          6步:發布logging模塊

              到現在為止,我們應用可以建立兩個發行包:logging.marservice.aar。其中logging.mar文件是Axis2模塊的發行包,該包的目錄結構如下:

          logging.mar

              module\LoggingModule.class

              module\LogHandler.class

              META-INF\module.xml

              service.aar文件是本例編寫的WebService發行包,該包的目錄結構如下:

          service.aar

              service\MyService.class

              META-INF\services.xml

              logging.mar文件放在<Tomcat安裝目錄>\webapps\axis2\WEB-INF\modules目錄中,將service.aar文件放在<Tomcat安裝目錄>\webapps\axis2\WEB-INF\services目錄中。要注意的是,如果modules目錄中包含了modules.list文件,Axis2會只裝載在該文件中引用的Axis2模塊,因此,必須在該文件中引用logging模塊,該文件的內容如下:

          addressing-1.4.1.mar

          soapmonitor-1.4.1.mar

          ping-1.4.1.mar

          mex-1.4.1.mar

          axis2-scripting-1.4.1.mar

          logging.mar

              如果modules目錄中不包含modules.list文件,則Axis2會裝載modules文件中的所有Axis2模塊。

              現在啟動Tomcat,使用如下的C#代碼調用MyServicegetGreeting方法則會在Tomcat控制臺中輸出相應的請求和響應SOAP消息。

          //  async是引用MyService的服務名
          async.myService my = new WSC.asyn.myService();
          MessageBox.Show(my.getGreeting(
          "中國"));
          MessageBox.Show(
          "完成調用");

              在執行上面的代碼后,在Tomcat控制臺中輸出的信息如下圖所示。


          下一篇:
          WebService大講堂之Axis2(10):使用soapmonitor模塊監視soap請求與響應消息





          Android開發完全講義(第2版)(本書版權已輸出到臺灣)

          http://product.dangdang.com/product.aspx?product_id=22741502



          Android高薪之路:Android程序員面試寶典 http://book.360buy.com/10970314.html


          新浪微博:http://t.sina.com.cn/androidguy   昵稱:李寧_Lining

          posted on 2009-02-16 09:05 銀河使者 閱讀(12787) 評論(3)  編輯  收藏 所屬分類: java 原創webservice

          評論

          # re: WebService大講堂之Axis2(9):編寫Axis2模塊(Module)  回復  更多評論   

          感謝分享,我在生成mar時候,引用了所有axis2的jar才生成class,汗。
          但是發布到axis2的module下,啟動tomcat后報錯,module.xml not found,實際上已經放到META-INF文件夾下了,lz能不能分享下代碼文件那?
          2009-09-16 13:32 | 大力

          # re: WebService大講堂之Axis2(9):編寫Axis2模塊(Module)  回復  更多評論   

          @大力
          生成mar時好像不需要引用任何jar包的,我是寫好module和handler類后建立了一個文件夾,放入編譯后的class文件,然后建立一個META-INF目錄,拷貝一個module.xml按照自己的文件進行修改,然后通過jar 命令生成mar包,沒有任何問題,不曉得你所說的只有引用了axis2的jar才生成class是指哪個環節。下面Tomcat報錯說module.xml估計是位置沒有放正確,請參照樓主上面對mar文件的目錄結構認真檢查,我的已經按照樓主的步驟成功運行了。
          2010-01-21 16:38 | Mr.Blue

          # re: WebService大講堂之Axis2(9):編寫Axis2模塊(Module)  回復  更多評論   

          你好!發布service的時候向services.xml中添加<parameter name="ServiceTCCL">composite</parameter>即可以調用自己定義的類,那請問如果模塊引用自己的類,該怎么做呢?
          2014-11-20 15:22 | sma
          主站蜘蛛池模板: 全椒县| 博野县| 温州市| 河源市| 麟游县| 高淳县| 清苑县| 祁东县| 宁晋县| 万荣县| 延寿县| 滨州市| 石棉县| 绵竹市| 广汉市| 镇平县| 宁化县| 鹤岗市| 青龙| 龙井市| 张家界市| 老河口市| 金寨县| 克什克腾旗| 依兰县| 玛曲县| 长垣县| 陵水| 株洲市| 延安市| 闽侯县| 资阳市| 务川| 绥中县| 新宁县| 全州县| 徐闻县| 济源市| 荣昌县| 琼中| 银川市|