分享

再谈AXIS2模块,和部署方式

 chanvy 2008-11-13

一、Axis2 中可用的部署方法
在 Axis2 中,可采用三种主要方式部署服务:
o 将服务存档文件放入存储库中。
o 使用存档文件以编程方式创建服务。
o 将服务作为传统 Java 对象(Plain Old Java Object,POJO)部署。
在 Axis2 中,部署服务的最常用方法是直接将服务存档文件复制或放置到存储库中(services 目录)。如果使用基于 Axis2 WAR 文件的分发版本,则有两个选择:
o 手动将存档文件放置到存储库中。
o 使用 Web 控制台上载服务。
以编程方式部署并非用户需求,而是模块创建者的需求,因为某些模块要求 Web 服务的部署提供模块的全部功能。若要以编程方式创建服务,需要使用 services.xml、类加载器(可用于加载您的类文件)和 AxisConfiguration。此方法的优势在于,您并不需要将服务存档文件复制到存储库中,而且仅在运行时服务才可见。清单 1 可帮助您形成对编程服务部署方法的基本认识。

清单 1. 编程服务部署
AxisConfiguration axisConfig;
// you need to have reference to AxisConfiguration
File file = new File("Location of the file"");
ClassLoader clsLoader = new URLClassLoader(new URL[]{file.toURL()});
InputStream in = new FileInputStream("location of service.xml");
AxisService service = DeploymentEngine.buildService(in, clsLoader, axisConfig);
使用 Java 类部署服务是 Axis2 中提供的一项使用非常方便的功能,在这种情况下没有必要生成服务存档文件或 services.xml。唯一的要求是,必须在创建服务前将 Java 类放入类路径中。在运行时,可以由模块或服务创建新服务并进行部署。在 Axis2 中部署 POJO 仅需要三行代码,如清单 2 中所示。

清单 2. 在 Axis2 中部署 POJO
AxisService service = AxisService.createService(
MyService.class.getName(), axisConfig, RPCMessageReceiver.class);
axisConfig.addService(service);

 


二、Axis2模块使用方法:
1.模块
Axis2为模块提供一个延伸的支持。我们现在自定义一个模块并将其部署到我们先前创建的MyService。为一个给定的Web Service部署一个自定义的模块,其步骤如下:
1)建立Module Implementation。
2)创建Handlers。
3)修改"axis2.xml"。
4)修改"services.xml",使你的模块在部署期生效。
5)将其打包为一个".mar"(Module Archive)。
6)在Axis2上部署这个模块。

2.为MyService增加一个日志模块
现在我们在我们的例子程序中增加一个日志模块。这个模块包含一个handle,用来记录所有传递给它的信息。Axis2使用". mar" (Module Archive)来部署模块。下图给出了需要被打包为".mar"文档的文件结构。


步骤一:日志模块类
日志模块是Axis2模块的实现类。Axis2模块应该实现"org.apache.axis2.modules.Module"接口中的如下方法。

public void init(ConfigurationContext configContext, AxisModule module)
throws AxisFault;//Initialize the module
public void shutdown(AxisConfiguration axisSystem)
throws AxisFault;//End of module processing
public void engageNotify(AxisDescription axisDescription) throws AxisFault;
这些方法可以用来控制模块的初始化和终止。通过参数AxisConfiguration,可提供给用户完整的配置层次。模块设计者可以使用它来很好的控制模块的所有可能的操作。就这个简单的日志服务的例子而言,我们可以空实现这些类。
LoggingModule.java

package userguide.loggingmodule;

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;

public class LoggingModule implements Module {
    // initialize the module
    public void init(ConfigurationContext configContext, AxisModule module) throws AxisFault {}
    public void engageNotify(AxisDescription axisDescription) throws AxisFault {}
    // shutdown the module
    public void shutdown(ConfigurationContext configurationContext) throws AxisFault {}
    public String[] getPolicyNamespaces() {
    return null;
    }
}

步骤二:LogHandler
Axis2中的模块可以包含一个或多个handlers用来在不同的阶段执行不同的SOAP头处理。创建一个handler,应该实现org.apache.axis2.engine.Handler。但是为简单起见,org.apache.axis2.handlers.AbstractHandler提供了一个对Handler接口的抽象的实现。针对本例日志模块,我们将创建一个handler包含以下方法:
1)"public void invoke(MessageContext ctx);"//当控制权转到handler时,由Axis2引擎调用。
2)"public void revoke(MessageContext ctx);"//当handlers被Axis2引擎撤销时调用。

package userguide.loggingmodule;

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;
import javax.xml.namespace.QName;
@SuppressWarnings("serial")
public class LogHandler extends AbstractHandler implements Handler {
private static final Log log = LogFactory.getLog(LogHandler.class);
    private QName name;
    public QName getName() {
        return name;
    }
    public void invoke(MessageContext msgContext) throws AxisFault {
        log.info(msgContext.getEnvelope().toString());
    }
    public void revoke(MessageContext msgContext) {
        log.info(msgContext.getEnvelope().toString());
    }
    public void setName(QName name) {
        this.name = name;
    }
}

步骤三:module.xml
"module.xml"包含了每一个特定的模块的部署配置信息。它应该包含的细节有一个实现模块的类(本例中是"LoggingModule"和各种各样的将在不同阶段运行的handlers)。本例中配置日志模块的"module.xml"如下:

<module name="logging" class="userguide.loggingmodule.LoggingModule ">
   <inflow>
        <handler name="InFlowLogHandler" class="userguide.loggingmodule.LogHandler">
        <order phase="loggingPhase" />
        </handler>
   </inflow>
   <outflow>
        <handler name="OutFlowLogHandler" class="userguide.loggingmodule.LogHandler">
        <order phase="loggingPhase"/>
        </handler>
   </outflow>
   <Outfaultflow>
        <handler name="FaultOutFlowLogHandler"
                  class="userguide.loggingmodule.LogHandler">
        <order phase="loggingPhase"/>
        </handler>
   </Outfaultflow>
   <INfaultflow>
        <handler name="FaultInFlowLogHandler"
class="userguide.loggingmodule.LogHandler">
        <order phase="loggingPhase"/>
        </handler>
   </INfaultflow>
</module>
从这个文件中,我们可以看到"module.xml"定义了4个阶段:
1)inflow-表示当一个消息到来时,这个handler链将运行。
2)outflow-表示当一个消息发出时,这个handler链将运行。
3)Outfaultflow-表示当有一个错误并且这个错蠼⒊鍪保飧鰄andler链将运行。
4)INfalutflow-表示当有一个错误并且这个错误将到来时,这个handler链将运行。
下面的标签设置描述了handler的名字,handler类和该handler将运行的阶段。

<handler name="InFlowLogHandler" class="userguide.loggingmodule.LogHandler">
<order phase="loggingPhase" />
</handler>

步骤四:修改"axis2.xml"
在这个handler中,阶段"loggingPhase"是由这个模块的设计者定义的。这不是一个预定义的handler阶段,因此该模块的设计者应该将它在"axis2.xml"中声明。只有这样,Axis2引擎才能知道将这个handler放置在哪些“流”中(InFlow, OutFlow,等)。下面的xml定义展示了需要将日志模块部署到Axis2引擎而对axis2.xml作的修改。(This is an extract of the phase section of the "axis2.xml".)

<!-- ================================================= -->
<!-- Phases -->
<!-- ================================================= -->

<phaseOrder type="inflow">
        <!-- System pre defined phases       -->
        <phase name="TransportIn"/>
        <phase name="PreDispatch"/>
        <phase name="Dispatch" class="org.apache.axis2.engine.DispatchPhase">
            <handler name="AddressingBasedDispatcher"
                     class="org.apache.axis2.engine.AddressingBasedDispatcher">
                <order phase="Dispatch"/>
            </handler>

            <handler name="RequestURIBasedDispatcher"
                     class="org.apache.axis2.engine.RequestURIBasedDispatcher">
                <order phase="Dispatch"/>
            </handler>

            <handler name="SOAPActionBasedDispatcher"
                     class="org.apache.axis2.engine.SOAPActionBasedDispatcher">
                <order phase="Dispatch"/>
            </handler>

            <handler name="SOAPMessageBodyBasedDispatcher"
                     class="org.apache.axis2.engine.SOAPMessageBodyBasedDispatcher">
                <order phase="Dispatch"/>
            </handler>
            <handler name="InstanceDispatcher"
                     class="org.apache.axis2.engine.InstanceDispatcher">
                <order phase="PostDispatch"/>
            </handler>
        </phase>
        <!-- System pre defined phases       -->
        <!--   After Postdispatch phase module author or or service author can add any phase he want      -->
        <phase name="OperationInPhase"/>
        <phase name="loggingPhase"/>
    </phaseOrder>
    <phaseOrder type="outflow">
        <!--      user can add his own phases to this area -->
        <phase name="OperationOutPhase"/>
        <phase name="loggingPhase"/>
        <!--system predefined phase-->
        <!--these phase will run irrespective of the service-->
        <phase name="PolicyDetermination"/>
        <phase name="MessageOut"/>
    </phaseOrder/>
    <phaseOrder type="INfaultflow">
        <!--      user can add his own phases to this area -->
        <phase name="OperationInFaultPhase"/>
        <phase name="loggingPhase"/>
    </phaseOrder>
    <phaseOrder type="Outfaultflow">
        <!--      user can add his own phases to this area -->
        <phase name="OperationOutFaultPhase"/>
        <phase name="loggingPhase"/>
        <phase name="PolicyDetermination"/>
        <phase name="MessageOut"/>
    </phaseOrder>
自定义的阶段"loggingPhase"在所有的流中都放置了,因此这个状态将会被所有的消息流调用。既然我们的模块与这个状态相联系,在这个模块中的LogHandler将会在这个状态被执行。

步骤五:修改"services.xml"
到目前为止,我们已经为这个日志模块创建了所需的类和配置文件。下一步就是在我们的services中使用这个模块。我们就在MyService中使用此模块作演示。因此,我们需要修改MyService的"services.xml",以使得该模块起作用。对"services.xml"的修改如下

<service name="MyServiceWithModule">
<description>
This is a sample Web Service with a logging module engaged.
</description>
<module ref="logging"/>
<parameter name="ServiceClass" locked="false">
userguide.example1.MyService
</parameter>
<operation name="echo">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="ping">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>我们在"services.xml"加入了一行"<module ref="logging"/>"。这行将告知Axis2引擎,这个日志模块可以被这个service使用。在这个模块中的handler将在各自的状态被执行(依据"module.xml"中的描述)。并将服务重新打包为MyServiceWithModule.aar。

步骤六:打包
在部署这个模块之前,我们需要将这个模块打包为一个".mar"文件。使用jar命令可以完成,将其打包为"logging.mar"。

步骤七:在Axis2上部署这个模块
为了在Axis2上部署模块,用户必须自己新建一个目录,取名为"modules",它的父目录为servlet容器中的"webapps/axis2/WEB-INF",再将".mar"文件复制到此目录下。本例中,为"logging.mar"。
注:为了看到日志,用户必须将"log4j.properties"设置为log INFO。该配置文件在servlet容器下的"webapps\axis2\WEB-INF\classes"目录。将"log4j.rootCategory= ERROR, LOGFILE"替换为"log4j.rootCategory=INFO, ERROR, LOGFILE"。
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/25593

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多