创建Module
Axis2模块至少需要有两个类,这两个类分别实现了Module和Handler接口
构造和部署 Module 分为以下几个步骤:
1. 创建 Module 的实现 --------------------Axis2模块在进行初始化、销毁等动作时会调用该类中相应的方法
2. 创建 Handlers -----------------------Axis2模块的业务处理类
3. 创建 module.xml------------------------该文件放在META-INF目录中,用于配置Axis2模块。
4. 修改 axis2.xml (如果你需要定制的语句)-----配置Axis2模块
5. 修改 services.xml 在 Axis 部署的时候使用这些 Modules -------一个Axis2模块都需要使用<module>元素引用才能使用
6. 在 Axis2 中部署这些 Modules -----------将Axis2模块压缩成.mar包,然后将.mar文件放在<Tomcat安装目录>\webapps\axis2\WEB-INF\modules目录中
现在来创建一个简单的 Logging Module ,这个 Module 包含一个 Hander ,它的作用就是纪录通过它的消息。 Axis 通过 *.mar 来部署 Modules ,下图就是这个部署包的结构
Step 1: 创建 LoggingModule Class
Logging Module 是 Axis2 Module 的实现,它必须实现 org.apache.axis2.modules.Module 接口:
Step 2: 创建 LogHandler
Axis 的一个 Module 可以包含一个或者多个 Handler 。这些 Handler 将处理 Soap 头文件中的不同 phases 。一个 Handler 必须实现 org.apache.axis2.engine.Handler 接口,或者通过另一种简单方式,继承 org.apache.axis2.handlers.AbstractHandler 类
public class LogHandler extends AbstractHandler implements Handler {
private Log log = LogFactory.getLog(getClass());
private QName name;
public QName getName() {
return name;
}
public void invoke(MessageContext msgContext) throws AxisFault {
log.info(msgContext.getEnvelope().toString());
}
public void setName(QName name) {
this.name = name;
}
}
Step 3: module.xml
这个文件包含了一个特定 Module 的部署配置。
<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>
a. inflow - Represents the handler chain that will run when a message is coming in.
b. outflow - Represents the handler chain that will run when the message is going out.
c. Outfaultflow - Represents the handler chain that will run when there is a fault and the fault is going out
d. INfaultflow - Represents the handler chain that will run when there is a fault and the fault is coming in
"<order phase="loggingPhase" />" describes the phase in which this handler runs.
还一种配置情况是 :
module.xml e中仅仅配置一些东西如下:
<module name="logging" class="userguide.loggingmodule.LoggingModule ">
</module>
但是这样比较麻烦是在axis2.xml u中的配置相对复杂些,如下
<phaseOrder type="InFlow">
<phase name="logging">
<handler name="InInterfaceLOGHandler" class="userguide.loggingmodule.LoggingModule ">
<order phase="logging"/>
</handler>
</phase>
</phaseOrder>
.....在OutFlow,InFaultFlow,OutFaultFlow的标签中都要添加完成的配置信息
Step 4: 修改 axis2.xml
在前面用的 ”loggingPhase” 不是一个 pre-defined handler phase ,因此, module 的创建者需要将它介绍给 axis2.xml 于是 Axis 引擎就可以知道在不同的 ’flow’ 中如何放那些 handlers 。
这些增加是在 axis2.xml 的 Phases 部分,在标志了 <!-- user can add his own phases to this area --> 之后加入
<phaseOrder type="InFlow">
<phase name=" loggingPhase"/> --------- 和module.xml中的 <order phase="loggingPhase"/>对应
</phaseOrder>
这样,这个 phase 将在引擎的任何消息流中调用
Step 5: 修改 service.xml
到目前为止, logging module 已经做好了,现在需要在服务中使用这个 module ,那么就要修改服务的 service.xml 。在该文件中加上了 "<module ref="logging"/>"
Step 6: 打包
将这个包打成 jar 或者 rar ,然后改后缀名为 mar 。
Step 7: 在 Axis2 中部署这个 Module
首先要在 "webapps/axis2/WEB-INF" 目录下创建一个 modules 文件夹,然后将 *.mar 文件放在这个文件夹中,然后重启 Axis 并运行服务进行测试