axis2开发webservice之编写Axis2模块(Module)
?axis2中的模塊化開發(fā)。能夠讓開發(fā)者自由的加入自己所需的模塊。提高開發(fā)效率,減少開發(fā)的難度。
Axis2能夠通過模塊(Module)進(jìn)行擴(kuò)展。
Axis2模塊至少須要有兩個(gè)類,這兩個(gè)類分別實(shí)現(xiàn)了Module和Handler接口。開發(fā)和使用一個(gè)Axis2模塊的過程例如以下:
1. 編寫實(shí)現(xiàn)Module接口的類。
Axis2模塊在進(jìn)行初始化、銷毀等動(dòng)作時(shí)會(huì)調(diào)用該類中對應(yīng)的方法)。
2. 編寫實(shí)現(xiàn)Handler接口的類。該類是Axis2模塊的業(yè)務(wù)處理類。
3. 編寫module.xml文件。該文件放在META-INF文件夾中。用于配置Axis2模塊。
4. 在axis2.xml文件里配置Axis2模塊。
5. 在services.xml文件里配置Axis2模塊。每個(gè)Axis2模塊都須要使用<module>元素引用才干使用。
6. 公布Axis2模塊。須要使用jar命令將Axis2模塊壓縮成.mar包(文件擴(kuò)展名必須是.mar),然后將.mar文件放在<Tomcat安裝文件夾>\webapps\axis2\WEB-INF\modules文件夾中。
先來編寫一個(gè)WebService類,代碼例如以下:package ws;public class TestWs {public String showName(String name) {return name; }public String getName() {return "axis2 webservice";} }以下我們來編寫一個(gè)記錄請求和響應(yīng)SOAP消息的Axis2模塊。當(dāng)client調(diào)用WebService方法時(shí),該Axis2模塊會(huì)將請求和響應(yīng)SOAP消息輸出到Tomcat控制臺(tái)上。
第1步:編寫LoggingModule類
????LoggingModule類實(shí)現(xiàn)了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類并沒實(shí)現(xiàn)實(shí)際的功能。但該類必須存在。
當(dāng)Tomcat啟動(dòng)時(shí)會(huì)裝載該Axis2模塊。同一時(shí)候會(huì)調(diào)用LoggingModule類的init方法。并在Tomcat控制臺(tái)中輸出“init”。
第2步:編寫LogHandler類
????LogHandler類實(shí)現(xiàn)了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,當(dāng)使用該Axis2模塊的WebService的方法被調(diào)用時(shí)。LogHandler類的invoke方法被調(diào)用。
第3步:編寫module.xml文件????
????在META-INF文件夾中建立一個(gè)module.xml文件,內(nèi)容例如以下:
<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)文件。分別在例如以下四個(gè)<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文件的內(nèi)容例如以下:
<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模塊
????到如今為止,我們應(yīng)用能夠建立兩個(gè)發(fā)行包:logging.mar和service.aar。跟我們之前公布的.aar包的過程是一樣的。當(dāng)中logging.mar文件是Axis2模塊的發(fā)行包。該包的文件夾結(jié)構(gòu)例如以下:
logging.mar
????module\LoggingModule.class
????module\LogHandler.class
????META-INF\module.xml
????service.aar文件是本例編寫的WebService發(fā)行包。該包的文件夾結(jié)構(gòu)例如以下:
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文件夾中。要注意的是,假設(shè)modules文件夾中包括了modules.list文件,Axis2會(huì)僅僅裝載在該文件里引用的Axis2模塊。因此,必須在該文件里引用logging模塊,該文件的內(nèi)容例如以下:
假設(shè)modules文件夾中不包括modules.list文件,則Axis2會(huì)裝載modules文件里的全部Axis2模塊。
????如今啟動(dòng)Tomcat,結(jié)果例如以下
能夠看到,logging已經(jīng)初始化了。
接下來就是用wsdl2java方法生成client代碼,再去調(diào)用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,今天任務(wù)完畢的不錯(cuò),加油!"); <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>} }
執(zhí)行結(jié)果
可是始終沒有在控制臺(tái)輸出對應(yīng)的請求和響應(yīng)SOAP消息。沒辦法,僅僅好再細(xì)致檢查了一遍,沒發(fā)現(xiàn)錯(cuò)誤,還以為是tomcat須要重新啟動(dòng)一下才干夠呢。結(jié)果在stop時(shí),發(fā)現(xiàn)tomcat輸出了soap信息,例如以下
完整的結(jié)果例如以下
?
轉(zhuǎn)載于:https://www.cnblogs.com/gavanwanggw/p/6816155.html
總結(jié)
以上是生活随笔為你收集整理的axis2开发webservice之编写Axis2模块(Module)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android--------Popup
- 下一篇: 【干货】分享总结:MySQL数据一致性