本文为原创,如需转载,请注明作者和出处,谢谢!
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
{
String getGreeting(String name)
{
return " 您好 + name;
}
}
下面我们来编写一个记录请求和响应SOAP消息的Axis2模块。当客户端调用WebService方法时,该Axis2模块会将请求和响应SOAP消息输出到Tomcat控制台上。
第1步:编写LoggingModule类
LoggingModule类实现了Module接口,代码如下:
import org.apache.axis2.AxisFault;
org.apache.axis2.context.ConfigurationContext;
org.apache.axis2.description.AxisDescription;
org.apache.axis2.description.AxisModule;
org.apache.axis2.modules.Module;
org.apache.neethi.Assertion;
org.apache.neethi.Policy;
LoggingModule implements Module
{
// initialize the module
void init(ConfigurationContext configContext, AxisModule module)
throws AxisFault
{
System.out.println( init );
}
engageNotify(AxisDescription axisDescription) AxisFault
{
}
shutdown the module shutdown(ConfigurationContext configurationContext)
shutdown String[] getPolicyNamespaces()
{
null ;
}
applyPolicy(Policy policy, AxisDescription axisDescription)
boolean canSupportAssertion(Assertion assertion)
{
true ;
}
}
在本例中LoggingModule类并没实现实际的功能,但该类必须存在。当Tomcat启动时会装载该Axis2模块,同时会调用LoggingModule类的init方法,并在Tomcat控制台中输出“init”。
第2步:编写LogHandler类
LogHandler类实现了Handler接口,代码如下:
org.apache.axis2.engine.Handler;
org.apache.axis2.handlers.AbstractHandler;
org.apache.commons.logging.Log;
org.apache.commons.logging.LogFactory;
LogHandler extends AbstractHandler Handler
{
private static final Log log = LogFactory.getLog(LogHandler. );
String name;
String getName()
{
name;
}
InvocationResponse invoke(MessageContext msgContext)
AxisFault
{
向Tomcat控制台输出请求和响应SOAP消息 log.info(msgContext.getEnvelope().toString());
InvocationResponse.CONTINUE;
}
revoke(MessageContext msgContext)
{
log.info(msgContext.getEnvelope().toString());
}
setName(String name)
{
this .name name;
}
}
LogHandler类的核心方法是invoke,当使用该Axis2模块的WebService的方法被调用时,LogHandler类的invoke方法被调用。
第3步:编写module.xml文件
在meta-inf目录中建立一个module.xml文件,内容如下:
InFlow
handler ="InFlowLogHandler" ="module.LogHandler"
order phase ="loggingPhase" /> </ handler OutFlow ="OutFlowLogHandler"
OutFaultFlow ="FaultOutFlowLogHandler" InFaultFlow ="FaultInFlowLogHandler" module
第4步:在axis2.xml文件中配置Axis2模块
打开axis2.xml文件,分别在如下四个<phaSEOrder>元素中加入<phase name="loggingPhase"/>:
< phase ="soapmonitorPhase" phaSEOrder ="OutFlow"
="Security" ="InFaultFlow" ="OutFaultFlow"
第5步:在services.xml文件中引用logging模块
使用logging模块
<!-- 引用logging模块 --> ref ="logging" parameter ="ServiceClass"
service.MyService
parameter messageReceivers messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-out"
class ="org.apache.axis2.rpc.receivers.RPcmessageReceiver" 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消息。
MessageBox.Show(my.getGreeting( 中国 ));
MessageBox.Show( 完成调用 );
在执行上面的代码后,在
Tomcat
控制台中输出的信息如下 图所示。版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。