`
zxs19861202
  • 浏览: 909392 次
  • 性别: Icon_minigender_1
  • 来自: 湖北—》上海
社区版块
存档分类
最新评论

Axis2 编写模块(module)

阅读更多

Axis2可以通过模块(Module)进行扩展。Axis2模块至少需要有两个类,这两个类分别实现了Module和Handler接口。开发和使用一个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类,代码如下:

  1. package service;

  2. public class MyService
  3. {
  4.     public String getGreeting(String name)
  5.     {
  6.         return "您好 " + name;
  7.     }
  8. }
复制代码

下面我们来编写一个记录请求和响应SOAP消息的Axis2模块。当客户端调用WebService方法时,该Axis2模块会将请求和响应SOAP消息输出到Tomcat控制台上。

第1步:编写LoggingModule类

    LoggingModule类实现了Module接口,代码如下:

  1. package module;

  2. import org.apache.axis2.AxisFault;
  3. import org.apache.axis2.context.ConfigurationContext;
  4. import org.apache.axis2.description.AxisDescription;
  5. import org.apache.axis2.description.AxisModule;
  6. import org.apache.axis2.modules.Module;
  7. import org.apache.neethi.Assertion;
  8. import org.apache.neethi.Policy;

  9. public class LoggingModule implements Module
  10. {
  11.     // initialize the module
  12.     public void init(ConfigurationContext configContext, AxisModule module)
  13.             throws AxisFault
  14.     {
  15.         System.out.println("init");
  16.     }
  17.     public void engageNotify(AxisDescription axisDescription) throws AxisFault
  18.     {
  19.     }
  20.     // shutdown the module
  21.     public void shutdown(ConfigurationContext configurationContext)
  22.             throws AxisFault
  23.     {
  24.         System.out.println("shutdown");
  25.     }
  26.     public String[] getPolicyNamespaces()
  27.     {
  28.         return null;
  29.     }
  30.     public void applyPolicy(Policy policy, AxisDescription axisDescription)
  31.             throws AxisFault
  32.     {
  33.     }
  34.     public boolean canSupportAssertion(Assertion assertion)
  35.     {
  36.         return true;
  37.     }
  38. }
复制代码

在本例中LoggingModule类并没实现实际的功能,但该类必须存在。当Tomcat启动时会装载该Axis2模块,同时会调用LoggingModule类的init方法,并在Tomcat控制台中输出“init”。

第2步:编写LogHandler类


    LogHandler类实现了Handler接口,代码如下:

  1. package module;

  2. import org.apache.axis2.AxisFault;
  3. import org.apache.axis2.context.MessageContext;
  4. import org.apache.axis2.engine.Handler;
  5. import org.apache.axis2.handlers.AbstractHandler;
  6. import org.apache.commons.logging.Log;
  7. import org.apache.commons.logging.LogFactory;

  8. public class LogHandler extends AbstractHandler implements Handler
  9. {
  10.     private static final Log log = LogFactory.getLog(LogHandler.class);
  11.     private String name;
  12.     public String getName()
  13.     {
  14.         return name;
  15.     }
  16.     public InvocationResponse invoke(MessageContext msgContext)
  17.             throws AxisFault
  18.     {
  19.         //  向Tomcat控制台输出请求和响应SOAP消息
  20.         log.info(msgContext.getEnvelope().toString());
  21.         return InvocationResponse.CONTINUE;
  22.     }
  23.     public void revoke(MessageContext msgContext)
  24.     {
  25.         log.info(msgContext.getEnvelope().toString());
  26.     }
  27.     public void setName(String name)
  28.     {
  29.         this.name = name;
  30.     }
  31. }
复制代码

LogHandler类的核心方法是invoke,当使用该Axis2模块的WebService的方法被调用时,LogHandler类的invoke方法被调用。   

第3步:编写module.xml文件   


    在META-INF目录中建立一个module.xml文件,内容如下:

  1. <module name="logging" class="module.LoggingModule">
  2.     <InFlow>
  3.         <handler name="InFlowLogHandler" class="module.LogHandler">
  4.             <order phase="loggingPhase"/>
  5.         </handler>
  6.     </InFlow>
  7.     <OutFlow>
  8.         <handler name="OutFlowLogHandler" class="module.LogHandler">
  9.             <order phase="loggingPhase"/>
  10.         </handler>
  11.     </OutFlow>

  12.     <OutFaultFlow>
  13.         <handler name="FaultOutFlowLogHandler" class="module.LogHandler">
  14.             <order phase="loggingPhase"/>
  15.         </handler>
  16.     </OutFaultFlow>
  17.     <InFaultFlow>
  18.         <handler name="FaultInFlowLogHandler" class="module.LogHandler">
  19.             <order phase="loggingPhase"/>
  20.         </handler>
  21.     </InFaultFlow>
  22. </module>
复制代码

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

    打开axis2.xml文件,分别在如下四个<phaseOrder>元素中加入<phase name="loggingPhase"/>:

  1. <phaseOrder type="InFlow">
  2.    
  3.     <phase name="soapmonitorPhase"/>
  4.     <phase name="loggingPhase"/>
  5. </phaseOrder>
  6. <phaseOrder type="OutFlow">
  7.    
  8.     <phase name="Security"/>
  9.     <phase name="loggingPhase"/>
  10. </phaseOrder>
  11. <phaseOrder type="InFaultFlow">
  12.    
  13.     <phase name="soapmonitorPhase"/>
  14.     <phase name="loggingPhase"/>
  15. </phaseOrder>
  16. <phaseOrder type="OutFaultFlow">
  17.    
  18.     <phase name="Security"/>
  19.     <phase name="loggingPhase"/>
  20. </phaseOrder>
复制代码

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

    services.xml文件的内容如下:

  1. <service name="myService">
  2.     <description>
  3.         使用logging模块
  4.     </description>
  5.     <!--  引用logging模块  -->
  6.     <module ref="logging"/>
  7.     <parameter name="ServiceClass">
  8.         service.MyService 
  9.     </parameter>
  10.     <messageReceivers>
  11.         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
  12.             class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  13.     </messageReceivers>
  14. </service>
复制代码

第6步:发布logging模块

    到现在为止,我们应用可以建立两个发行包:logging.mar和service.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#代码调用MyService的getGreeting方法则会在Tomcat控制台中输出相应的请求和响应SOAP消息。

  1. //  async是引用MyService的服务名
  2. async.myService my = new WSC.asyn.myService();
  3. MessageBox.Show(my.getGreeting("中国"));
  4. MessageBox.Show("完成调用");
复制代码

在执行上面的代码后,在Tomcat控制台中输出的信息如下图所示。

分享到:
评论

相关推荐

    webservice9 编写axis模块

    webservice9 编写axis模块webservice9 编写axis模块

    axis2-1.6.1

    支持开发 Axis2 的动力是探寻模块化更强、灵活性更高和更有效的体系结构,这种体系结构可以很容易地插入到其他相关 Web 服务标准和协议(如 WS-Security、WS-ReliableMessaging 等)的实现中。 Apache Axis2 是Axis...

    axis2-1.6.2

    Axis2是下一代 Apache ...支持开发 Axis2 的动力是探寻模块化更强、灵活性更高和更有效的体系结构,这种体系结构可以很容易地插入到其他相关 Web 服务标准和协议(如 WS-Security、WS-ReliableMessaging 等)的实现中。

    axis2例子 webservice axis2 示例

    axis2例子 webservice axis2 示例axis2例子 webservice axis2 示例axis2例子 webservice axis2 示例axis2例子 webservice axis2 示例axis2例子 webservice axis2 示例

    完整的axis2 jar包包含实例.zip

    axis2 webservice 服务端jar包: --&gt;axis2-kernel-1.6.1.jar --&gt;axis2-spring-1.6.1.jar --&gt;axis2-transport-http-1.6.1.jar --&gt;XmlSchema-1.4.7.jar --&gt;wsdl4j-1.6.2.jar --&gt;axiom-api-1.2.12.jar --&gt;axiom...

    axis2-1.6.2.zip

    axis2-1.6.2.zip, windows axis2工具,根据 WSDL生成java文件。 1、axis2客户端下载地址:http://mirror.esocc.com/apache//axis/axis2/java/core/1.6.2/axis2-1.6.2-bin.zip; 2、下载解压在D:\Work_Program_...

    axis2相关的jar包

    axis2-adb-1.5.4.jar axis2-adb-codegen-1.5.4.jar axis2-codegen-1.5.4.jar axis2-corba-1.5.4.jar axis2-fastinfoset-1.5.4.jar axis2-java2wsdl-1.5.4.jar axis2-jaxbri-1.5.4.jar axis2-jaxws-1.5.4.jar axis2-...

    axis2-1.7.8.zip

    axis2-1.7.8.zip客户端。2、下载解压在D:\Work_Program_Files目录;在D:\Work_Program_Files\axis2-1.6.2\bin目录有一个wsdl2java.bat文件; 3、设置环境变量,加入AXIS2_HOME=Axis2客户端安装目录,path中追加;%...

    axis2方式开发webservice

    myeclipse安装axis2.txt文件:详细说明了myeclipse如何安装axis2插件,以及编写简单的服务端代码,以及axis2客户端访问服务端的几种方式。 axisdemo是一个普通的javaweb工程,里面有一个简单的接口,在此工程的基础...

    axis2用户身份认证扩展模块

    基于axis2的webservice用户身份认证扩展模块源码

    axis2所需要的包

    Axis2是下一代 Apache ...支持开发 Axis2 的动力是探寻模块化更强、灵活性更高和更有效的体系结构,这种体系结构可以很容易地插入到其他相关 Web 服务标准和协议(如 WS-Security、WS-ReliableMessaging 等)的实现中

    axis2发布webservice和调用axis2服务接口

    本案例实现使用Axis2生成.arr包发布WebService服务,并使用Axis2生成客户端代码方式调用WebService

    axis2-162-war和axis2-162-bin

    Axis2 是 Apache 提供的一款第三方 Web Services 引擎,与其前身 Apache Axis 相比, axis2 更加高效、模块化,也更面向于 XML 的组织。经过精心的设计, axis2 提供了更加便利的模块添加功能。 基于新的体系结构...

    axis1.4和axis2相关jar文件

    axis1.4和axis2相关jar文件,axis1.4和axis2相关jar文件

    Axis2源代码压缩包

    支持开发 Axis2 的动力是探寻模块化更强、灵活性更高和更有效的体系结构,这种体系结构可以很容易地插入到其他相关 Web 服务标准和协议(如 WS-Security、WS-ReliableMessaging 等)的实现中。 Apache Axis2 是Axis...

    axis2开发webservice(二)

    myeclipse安装axis2.txt文件:详细说明了myeclipse如何安装axis2插件,以及编写简单的服务端代码,以及axis2客户端访问服务端的几种方式。 axisdemo是一个普通的javaweb工程,里面有一个简单的接口,在此工程的基础...

    axis2的API,axis2 API,axis2帮助文档

    axis2的API,axis2 API,axis2帮助文档

    axis2 教程_个人备用文档

    1. 用POJO实现0配置的WebService 2. 复合类型数据的传递 3. 使用services.xml文件发布WebService 4.... 5.... 6.... 7.... 8.... 9. 编写Axis2模块(Module) 10. 使用soapmonitor模块监视soap请求与响应消息

    axis2-1.4.1及教程

    1):用POJO实现0配置的WebService 2):复合类型数据的传递 3):使用services.xml文件发布WebService 4):二进制文件传输 ...9):编写Axis2模块(Module) 10):使用soapmonitor模块监视soap请求与响应消息

    webservice-axis2引擎-快速入门资料

    支持开发 Axis2 的动力是探寻模块化更强、灵活性更高和更有效的体系结构,这种体系结构可以很容易地插入到其他相关 Web 服务标准和协议(如 WS-Security、WS-ReliableMessaging 等)的实现中。 Apache Axis2 是Axis...

Global site tag (gtag.js) - Google Analytics