axis2开发webservice之编写Axis2模块(Module)

来源:互联网 发布:h3c网络工程师证书知乎 编辑:程序博客网 时间:2024/05/20 04:30

  axis2中的模块化开发,可以让开发人员自由的添加自己所需的模块,提高开发效率,降低开发的难度。

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目录中。    
先看一下在axis2中什么是module和handler,如下是官方解释

    

先来编写一个WebService类,代码如下:

[java] view plain copy
  1. package module;  
  2.   
  3. public class MyService  
  4. {  
  5.     public String getGreeting(String name)  
  6.     {  
  7.         return "您好 " + name;  
  8.     }  
  9. }  

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

1步:编写LoggingModule

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

[java] view plain copy
  1. package module;  
  2.   
  3. import org.apache.axis2.AxisFault;  
  4. import org.apache.axis2.context.ConfigurationContext;  
  5. import org.apache.axis2.description.AxisDescription;  
  6. import org.apache.axis2.description.AxisModule;  
  7. import org.apache.axis2.modules.Module;  
  8. import org.apache.neethi.Assertion;  
  9. import org.apache.neethi.Policy;  
  10.   
  11. public class LoggingModule implements Module  
  12. {  
  13.     // initialize the module  
  14.     public void init(ConfigurationContext configContext, AxisModule module)  
  15.             throws AxisFault  
  16.     {  
  17.         System.out.println("init");  
  18.     }  
  19.     public void engageNotify(AxisDescription axisDescription) throws AxisFault  
  20.     {  
  21.     }  
  22.     // shutdown the module  
  23.     public void shutdown(ConfigurationContext configurationContext)  
  24.             throws AxisFault  
  25.     {  
  26.         System.out.println("shutdown");  
  27.     }  
  28.     public String[] getPolicyNamespaces()  
  29.     {  
  30.         return null;  
  31.     }  
  32.     public void applyPolicy(Policy policy, AxisDescription axisDescription)  
  33.             throws AxisFault  
  34.     {  
  35.     }  
  36.     public boolean canSupportAssertion(Assertion assertion)  
  37.     {  
  38.         return true;  
  39.     }  
  40. }  

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

2步:编写LogHandler

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

[java] view plain copy
  1. package module;  
  2.   
  3.   
  4. import org.apache.axis2.AxisFault;  
  5. import org.apache.axis2.context.MessageContext;  
  6. import org.apache.axis2.engine.Handler;  
  7. import org.apache.axis2.handlers.AbstractHandler;  
  8. import org.apache.commons.logging.Log;  
  9. import org.apache.commons.logging.LogFactory;  
  10.   
  11. public class LogHandler extends AbstractHandler implements Handler  
  12. {  
  13.     private static final Log log = LogFactory.getLog(LogHandler.class);  
  14.     private String name;  
  15.     public String getName()  
  16.     {  
  17.         return name;  
  18.     }  
  19.     public InvocationResponse invoke(MessageContext msgContext)  
  20.             throws AxisFault  
  21.     {  
  22.         //  向Tomcat控制台输出请求和响应SOAP消息  
  23.         log.info(msgContext.getEnvelope().toString());  
  24.         return InvocationResponse.CONTINUE;  
  25.     }  
  26.     public void revoke(MessageContext msgContext)  
  27.     {  
  28.         log.info(msgContext.getEnvelope().toString());  
  29.     }  
  30.     public void setName(String name)  
  31.     {  
  32.         this.name = name;  
  33.     }  
  34. }  

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

3步:编写module.xml文件    

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

[html] view plain copy
  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.   
  13.     <OutFaultFlow>  
  14.         <handler name="FaultOutFlowLogHandler" class="module.LogHandler">  
  15.             <order phase="loggingPhase"/>  
  16.         </handler>  
  17.     </OutFaultFlow>  
  18.     <InFaultFlow>  
  19.         <handler name="FaultInFlowLogHandler" class="module.LogHandler">  
  20.             <order phase="loggingPhase"/>  
  21.         </handler>  
  22.     </InFaultFlow>  
  23. </module>  

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

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

[html] view plain copy
  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文件的内容如下:

[html] view plain copy
  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.marservice.aar。跟我们之前发布的.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模块,该文件的内容如下:

    如果modules目录中不包含modules.list文件,则Axis2会装载modules文件中的所有Axis2模块。

    现在启动Tomcat,结果如下

可以看到,logging已经初始化了。

接下来就是用wsdl2java方法生成客户端代码,再去调用webservice,如下

[java] view plain copy
  1. package module;  
  2.   
  3. import java.rmi.RemoteException;  
  4.   
  5. public class MyServiceStubClient {  
  6.   
  7.     public static void main(String[] args) throws RemoteException {  
  8.         ModuleServiceStub mss = new ModuleServiceStub();  
  9.           
  10.         ModuleServiceStub.GetGreeting gg = new ModuleServiceStub.GetGreeting();  
  11.         gg.setName("thinkpad,今天任务完成的不错,加油!");  
  12.         System.out.println(mss.getGreeting(gg).get_return());  
  13.     }  
  14. }  


在这里需要强调的一点就是,System.out.println(mss.getGreeting(gg).get_return());这里一定要get_return()一下,虽然在Java SE中,这时不需要的,但是这里是webservice,是需要从服务器端来获取。否则的话,打印出来的就是返回值的内存地址,如果不注意的话,很容易认为是没有重写toString()方法的原因。

运行结果

但是始终没有在控制台输出相应的请求和响应SOAP消息,没办法,只好再仔细检查了一遍,没发现错误,还以为是tomcat需要重启一下才可以呢,结果在stop时,发现tomcat输出了soap信息,如下

完整的结果如下


0 0
原创粉丝点击