axis2创建Module
创建Module
Axis2模块至少需要有两个类,这两个类分别实现了Module和Handler接口
?
构造和部署 Module 分为以下几个步骤:
?
现在来创建一个简单的 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" style="margin: 0cm 0cm 0pt;">?? <inflow>
??????? <handler name="InFlowLogHandler" style="margin: 0cm 0cm 0pt;">??????? <order phase="loggingPhase" />
??????? </handler>
?? </inflow>
?? <outflow>
??????? <handler name="OutFlowLogHandler" style="margin: 0cm 0cm 0pt;">??????? <order phase="loggingPhase"/>
??????? </handler>
?? </outflow>
?? <Outfaultflow>
??????? <handler name="FaultOutFlowLogHandler" style="margin: 0cm 0cm 0pt;">??????? <order phase="loggingPhase"/>
??????? </handler>
?? </Outfaultflow>
?? <INfaultflow>
??????? <handler name="FaultInFlowLogHandler" style="margin: 0cm 0cm 0pt;">??????? <order phase="loggingPhase"/>
??????? </handler>
?? </INfaultflow>
</module>
?
?
"<order phase="loggingPhase" />" describes the phase in which this handler runs.
?
还一种配置情况是?:
?
module.xml e中仅仅配置一些东西如下:
<module name="logging"? style="margin: 0cm 0cm 0pt;">?但是这样比较麻烦是在axis2.xml u中的配置相对复杂些,如下
?
<phaseOrder type="InFlow">
<phase name="logging">
???????????? <handler name="InInterfaceLOGHandler" style="margin: 0cm 0cm 0pt;"></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 并运行服务进行测试