axis2中的模块化开发。能够让开发者自由的加入自己所需的模块。提高开发效率,减少开发的难度。
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安装文件夹>webappsaxis2WEB-INFmodules文件夹中。
先来编写一个WebService类,代码例如以下:package ws;
public class TestWs {
public String showName(String name) {
return name;
}
public String getName() {
return "axis2 webservice";
}
}
以下我们来编写一个记录请求和响应SOAP消息的Axis2模块。当client调用WebService方法时,该Axis2模块会将请求和响应SOAP消息输出到Tomcat控制台上。
第1步:编写LoggingModule类
LoggingModule类实现了Module接口。代码例如以下:
package 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 {
public void applyPolicy(Policy arg0, AxisDescription arg1) throws AxisFault {
}
public boolean canSupportAssertion(Assertion arg0) {
return true;
}
public void engageNotify(AxisDescription arg0) throws AxisFault {
}
public void init(ConfigurationContext arg0, AxisModule arg1)
throws AxisFault {
System.out.println("init");
}
public void shutdown(ConfigurationContext arg0) throws AxisFault {
System.out.println("shutdown");
}
}
在本例中LoggingModule类并没实现实际的功能。但该类必须存在。
当Tomcat启动时会装载该Axis2模块。同一时候会调用LoggingModule类的init方法。并在Tomcat控制台中输出“init”。
第2步:编写LogHandler类
LogHandler类实现了Handler接口。代码例如以下:
package module;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;
public class LogHandler extends AbstractHandler implements Handler {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Handler.InvocationResponse invoke(MessageContext arg0)
throws AxisFault {
System.out.println(arg0.getEnvelope().toString());
return Handler.InvocationResponse.CONTINUE;
}
public void revoke(MessageContext msgContext) {
System.out.println(msgContext.getEnvelope().toString());
}
}LogHandler类的核心方法是invoke,当使用该Axis2模块的WebService的方法被调用时。LogHandler类的invoke方法被调用。
第3步:编写module.xml文件
在META-INF文件夹中建立一个module.xml文件,内容例如以下:
<module name="logging" class="module.LoggingModule">
<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(web-inf/conf)文件。分别在例如以下四个<phaseOrder>元素中增加<phase name="loggingPhase"/>:
<phaseOrder type="InFlow">
<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文件的内容例如以下:
<service name="AxisService"> <description>AxisService</description> <parameter name="ServiceClass">ws.TestWs</parameter> <module ref="logging"/> <operation name="showName"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="getName"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </operation> </service>
第6步:公布logging模块
到如今为止,我们应用能够建立两个发行包:logging.mar和service.aar。跟我们之前公布的.aar包的过程是一样的。当中logging.mar文件是Axis2模块的发行包。该包的文件夹结构例如以下:
logging.mar
moduleLoggingModule.class
moduleLogHandler.class
META-INFmodule.xml
service.aar文件是本例编写的WebService发行包。该包的文件夹结构例如以下:
service.aar
serviceMyService.class
META-INFservices.xml
将logging.mar文件放在<Tomcat安装文件夹>webappsaxis2WEB-INFmodules文件夹中,将service.aar文件放在<Tomcat安装文件夹>webappsaxis2WEB-INFservices文件夹中。要注意的是,假设modules文件夹中包括了modules.list文件,Axis2会仅仅装载在该文件里引用的Axis2模块。因此,必须在该文件里引用logging模块,该文件的内容例如以下:
假设modules文件夹中不包括modules.list文件,则Axis2会装载modules文件里的全部Axis2模块。
如今启动Tomcat,结果例如以下
能够看到,logging已经初始化了。
接下来就是用wsdl2java方法生成client代码,再去调用webservice,例如以下
package ws;
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
public class TestClient {
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>AxisServiceStub stub = new AxisServiceStub();
<span style="white-space:pre"> </span>ShowName show = new ShowName();
<span style="white-space:pre"> </span>show.setName("thinkpad,今天任务完毕的不错,加油!");
<span style="white-space:pre"> </span>System.out.println(stub.showName(show).get_return());
<span style="white-space:pre"> </span>} catch (AxisFault e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} catch (RemoteException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
执行结果

完整的结果例如以下
