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目录中。
先来编写一个WebService类,代码如下:
public class MyService
{
public String getGreeting(String name)
{
return " 您好 " + name;
}
}
下面我们来编写一个记录请求和响应 SOAP消息的 Axis2模块。当客户端调用 WebService方法时,该 Axis2模块会将请求和响应 SOAP消息输出到 Tomcat控制台上。
第 1步:编写 LoggingModule类
LoggingModule 类实现了 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接口,代码如下:
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文件,内容如下:
< 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" >
< order phase ="loggingPhase" />
</ handler >
</ InFaultFlow >
</ module >
第 4步:在 axis2.xml文件中配置 Axis2模块
打开 axis2.xml文件,分别在如下四个 <phaseOrder>元素中加入 <phase name="loggingPhase"/> :


< phase name ="soapmonitorPhase" />
< phase name ="loggingPhase" />
</ phaseOrder >
< phaseOrder type ="OutFlow" >


< phase name ="Security" />
< phase name ="loggingPhase" />
</ phaseOrder >
< phaseOrder type ="InFaultFlow" >


< phase name ="soapmonitorPhase" />
< phase name ="loggingPhase" />
</ phaseOrder >
< phaseOrder type ="OutFaultFlow" >


< phase name ="Security" />
< phase name ="loggingPhase" />
</ phaseOrder >
第 5步:在 services.xml文件中引用 logging模块
services.xml文件的内容如下:
< description >
使用logging模块
</ description >
<!-- 引用logging模块 -->
< module ref ="logging" />
< parameter name ="ServiceClass" >
service.MyService
</ parameter >
< messageReceivers >
< messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-out"
class ="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</ messageReceivers >
</ service >
第 6步:发布 logging模块
到现在为止,我们应用可以建立两个发行包: logging.mar和 service.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模块,该文件的内容如下:
addressing-1.4.1.mar
soapmonitor-1.4.1.mar
ping-1.4.1.mar
mex-1.4.1.mar
axis2-scripting-1.4.1.mar
logging.mar
如果 modules目录中不包含 modules.list文件,则 Axis2会装载 modules文件中的所有 Axis2模块。
现在启动 Tomcat,使用如下的 C#代码调用 MyService的 getGreeting方法则会在 Tomcat控制台中输出相应的请求和响应 SOAP消息。
async.myService my = new WSC.asyn.myService();
MessageBox.Show(my.getGreeting( " 中国 " ));
MessageBox.Show( " 完成调用 " );
在执行上面的代码后,在 Tomcat控制台中输出的信息如下 图所示。
本文出处: