当前位置:编程学习 > 网站相关 >>

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

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

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

   

 

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

[java]
package module; 
 
public class MyService 

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

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

第1步:编写LoggingModule类

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

[java]
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接口,代码如下:

[java] 
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文件,内容如下:

[html] 
<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"> 
        

补充:Web开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,