axis2開發webservice之編寫Axis2模塊(Module)
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安裝文件夾>\webapps\axis2\WEB-INF\modules文件夾中。
先來編寫一個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
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模塊,該文件的內容例如以下:
假設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>} }
執行結果
可是始終沒有在控制臺輸出對應的請求和響應SOAP消息。沒辦法,僅僅好再細致檢查了一遍,沒發現錯誤,還以為是tomcat須要重新啟動一下才幹夠呢。結果在stop時,發現tomcat輸出了soap信息,例如以下
完整的結果例如以下
axis2開發webservice之編寫Axis2模塊(Module)