?????? Axis2 是下一代的 Apache Axis ,它的體系結構和原來的 Axis1.x 有所不同。它體現出更靈活、有效和可配置性。盡管它的體系結構是新的,但在 Axis1.x 版本下有些良好的概念還是保存下來了,如: handler 在 axis2 中依然保留。
1、
Axis2
所體現的特點有:
Speed :速度快,使用自己的對象模型和 StAX 來解析 achieve 。
Low memory foot print :
AXIOM :使用自己的輕量級對象模型 AXIOM ,用于消息處理, axiom 是具有可擴展性、高性能和開發便利的優點。
Hot Deployment :可進行熱部署,在部署新的服務時不需要重新啟動服務。
Asynchronous Web Services : 支持異步服務。
MEP Support : 支持消息交換模式( Message Exchange Patterns : MEPs ) ,MEP 是內嵌式的,支持 WSDL2.0 中的基本的 MEPs 。
Flexibility :具有靈活性,使得開發者自由向引擎中插入擴展功能。
Stability :穩定性。定義一系列的公布的接口,這些接口相對于其余的 Axis 改變得相對較慢。
Component-Oriented Deployment : 面向組件的部署。可以輕松定義可復用的網絡或 handler 來實現常規應用程序的處理或將其分配給其伙伴。
Transport Framework :
WSDL 支持: 支 WSDL 1.1 和 WSDL 2.0 版本 。
Add-ons :
Composition and Extensibility: 模塊支持組合和擴展功能。
Axis2
的
samples
:
binary
發布中的
samples/userguide/src
目錄下
2、
Web Services Using Axis2
?????? 使用 axis2 寫 web services 有兩種方法
ü ???????? 使用 Axis2 的基本接口( Primary API )來實現業務邏輯
ü ???????? 從 WSDL 文件生成 Skeleton ,再實現業務邏輯
1) ????? 使用 Axis2 的基本接口( Primary API )來實現業務邏輯
????????????? 主要的操作是:
????????????? A :寫實現類
????????????? B :寫一個 services.xml 來解釋 web service
??????? C :為 web 服務創建一個 aar 文檔
??????? D :將文檔部署
?????? 步驟一:編寫類文件,描述 web service 的業務邏輯
public class MyService{
??? public void ping(OMElement element){
???? ......
??? }
??? public OMElement echo(OMElement element){
???? ......
??? }
}
// 這個類中給了兩個方法,一個是只有輸入,另一個是有輸入和輸出,輸入輸出類型都是 OMElement |
?????? 步驟二:為 web service 編寫配置文件 services.xml
?????? ?????? 一個 services.xml 文件中可以寫多個 web 服務的配置信息,可以對一組相關的服務 進行配置管理,使用 <ServiceGroup> 元素包含多個 <service> 元素
<service >
??? <description>
??????? This is a sample Web Service with two operations, echo and ping.
</description>
<!- -
描述服務的類名
-- >
<parameter name="ServiceClass" locked="false">userguide.example1.MyService</parameter>
<!—
分別描述服務中的操作,每個操作都有相應的
MessageReceiver
,方法若是接收輸入,返回輸出信息使用的是
RawXMLINOutMessageReceiver
類,如要想使用
WS-Addressing
,需要有元素
actionMapping
-->
??? <operation name="echo">
??????? <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
??????? <actionMapping>urn:echo</actionMapping>
??? </operation>
???? <operation name="ping">
??????? <messageReceiver class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/>
??????? <actionMapping>urn:ping</actionMapping>
??? </operation> ?</service> |
?????? 步驟三:創建 archive 文件
?????? Archive 文件的目錄結構如下圖所示,在當前目錄下,創建一個 META-INF 目錄, services.xml 文件就存放于此目錄,當前目錄下存放的是源文件,可以先創建一個 jar 文件,再將其更改擴展名。
?????? 可以將命令行切換到 example1 目錄下,鍵入命令: jar cvf MyService.jar .
之后將 jar 更為 aar
?????? 步驟四:部署文件
????????????? 將 aar 文件復制到 axis2 的 WEB-INF 目錄下的 service 目錄下,或者使用它提供的 ?? upload service 接口直接部署。
?????? REST: 第二代 Web services 使用的體系結構,而不是使用 SOAP 。 REST 代表: Representational State Transfer 。表示每一個 URL 都表示一個對象資源。可以使用 HTTP Get 獲得對象的內容,或者是刪除、或對對象進行更改。
The main advantages of REST web services are:
Lightweight - not a lot of extra xml markup
Human Readable Results
Easy to build - no toolkits required
SOAP also has some advantages:
Easy to consume - sometimes
Rigid - type checking, adheres to a contract
Development tools
2) ????? 從 WSDL 文件生成 Skeleton ,再實現業務邏輯
??????
?????? 步驟一:根據 WSDL 文件生成 skeleton
????????????? 使用 axis2 提供的 WSDL2Java 工具生成 skeleton ,工具使用的參數說明如下
Usage WSDL2Code -uri <Location of WSDL> : WSDL file location
-o <output Location> : output file location
-a : Generate async style code only. Default is off
-s : Generate sync style code only. Default is off. takes precedence over -a
-p <package name> : set custom package name
-l <language> : valid languages are java and csharp. Default is java
-t : Generate TestCase to test the generated code
-ss : Generate server side code (i.e. skeletons). Default is off
-sd : Generate service descriptor (i.e. services.xml). Default is off. Valid with -ss
-d <databinding> : valid databinding(s) are adb, xmlbeans and jaxme. Default is adb
-g Generates all the classes. valid only with the -ss
-pn <port_name> : name of port in the presence of multiple ports
-sn <service_name> : name of service in the presence of multiple services
-u : unpacks the databinding classes -r <repository_path> : path of the repository against which code is generated |
?????? 將例子中給的 Axis2SampleDocLit.wsdl 生成它的 skeleton ,命令行如下
D:\ws_tools\axis2-std-1.0-bin>WSDL2Java -uri samples\wsdl\Axis2SampleDocLit.wsdl
?-ss -sd -d xmlbeans -o samples\tt -p org.apache.axis2.userguide?? 將生成的文件放于 samples 目錄下的 tt 目錄下。
?????? 步驟二:實現商業邏輯
?????? ? 在 src 目錄下的目錄中找到 Axis2SampleDocLitTypeSkeleton.java 文件,它就是 web service 的 skeleton ,對其中的方法進行編輯來實現商業邏輯。
?????? 步驟三: services.xml
?????? 生成 skeleton 時,服務的配置文件 services.xml 文件是自動生成的,存放于 resources 目錄下。
?????? 步驟四:打包( packaging )
?????? 將類文件打包形成 aar 文件,然后將其部署到 axis2 下的 services 目錄下。使用 WSDL2Java 工具生成 skeleton 時同時產生的數據綁定類在 schemaorg_apache_xmlbeans 目錄下,也需要將其復制到類路徑下,將 skeleton 和支持的文件進行編譯。將需要的目錄文件以下面的結構存放,編譯之后打包生成 aar 文件。
即將 services.xml 文件放于 META-INF 目錄下,將也將 schemaorg_apache-xmlbeans 目錄和 skeleton 所在的源文件目錄放于一個目錄下。同時將 wsdl 文件也放于 META-INF 目錄下。
對 perf 的操作如下:
在 src 目錄下, javac –classpath .;%axis2classpath% org\apache\axis2\userguide\*.java
編譯成功之后,將 schemaorg_apache_xmlbeans 目錄拷貝到 src 目錄下,建立一個 META-INF 目錄,將 resources 目錄下的 services.xml 文件和 *.wsdl 文件復制到 META-INF 目錄下
在 src 目錄下,運行 jar 命令 jar -cfv perf.jar META-INF org schemaorg_apache_xmlbeans
將 perf.jar 改名為 aar 部署到 tomcat 容器中。
或者是在編輯完 skeleton 后,運行生成的 build.xml ,即 ant 命令,則 ant 會對程序進行編譯,成功之后,把生成的 build 目錄下的 classes 下的 org 目錄和 META-INF 還有 resources
目錄下的 schemaorg_apache_xmlbeans 目錄添加到一個 jar 文件即可,更改 jar 擴展名為 aar 擴展名。
3、
Web Service
客戶端使用Axis2調用服務
?????? 客戶端調用使用可以使用阻塞和非阻塞 API 。
?????? 阻塞模式( Blocking API ):一旦服務被調用,客戶端處于阻塞狀態,它不能執行其他的操作,除非收到服務端發送的響應信息或是出錯信息。(同步模式調用)
?????? 非阻塞模式( Non-Blocking API ):這是一種基于回調或者是輪詢的 API 。當服務調用以后,客戶端立刻能獲得控制權進行其他操作,服務端的返回消息使用提供的 callback 對象得到。這樣可以使得客戶端應用程序可以同時調用多個 web services 而不用阻塞已經調用的操作。(異步模式調用)它們都是在 API 層實現。
?????? 根據 API 層的異步調用和傳輸層的異步調用 ,可以得到四種不同模式來調用 web services 。
?
API (Blocking/ |
?Dual Trans |
Description
|
Blocking |
No |
Simplest and the familiar invocation pattern |
Non-Blocking |
No |
Using callbacks or polling |
Blocking |
Yes |
This is useful when the service operation is IN-OUT in nature but the transport used is One-Way (e.g. SMTP) |
Non-Blocking |
Yes |
This is can be used to gain the maximum asynchronous behavior. No blocking in the API level and also in the transport level |
客戶端調用 web 服務也有兩種方法:
ü ???????? 編寫 axis2 的主要的 API
ü ???????? 使用產生的 Stub 編寫
1) ????? 使用 Axis2 的原始的 API 編寫客戶端來調用 Web Service
?????? 測試的例子在 Eclipse 集成開發環境下使用 ,例子見 axis2 的 samples 或是 F:\proTest\axis2\ClientInvoke 下的實例。
2) ????? 使用 axis2 的代碼生成綁定數據來編寫 service 。
?????? 使用 WSDL2Java 命令來生成 stub ,對于 Axis2SampleDocLit.wsdl 操作如下:
D:\ws_tools\axis2-std-1.0-bin\samples\wsdl>WSDL2Java -uri ..\..\samples\wsdl\Axi
s2SampleDocLit.wsdl -o ..\..\samples\axisDocLitStub -p org.apache.axis2.userguide
??????
編輯
axisDocLitStub\src\org\apache\axis2\userguide
目錄下的
Axis2SampleDocLitServiceStub.java
文件,編譯時可以直接使用
WSDL2Java
生成的
build.xml
文件使用
ant
命令進行編譯。
4、 模塊(Module)
?????? 為一個給定服務自定義模塊步驟有:
(1) ?????? 創建模塊的實現
(2) ?????? 創建 handler
(3) ?????? 創建 module.xml 文件
(4) ?????? 如果需要自定義 phases ,則要修改 axis2.xml
(5) ?????? 修改 services.xml 文件使之在部署時結合 engage 模塊
(6) ?????? 在一個 *.mar 文件中打包
(7) ?????? 將模塊在 axis2 中部署
例子:在 MyService 服務中添加一個 logging 模塊
?????? Logging 模塊的功能是它包含一個 handler ,此 handler 能夠記錄被傳遞的消息。在 axis2 中,使用 .mar 文件來部署模塊。模塊的結構如下圖所示:
?????? 步驟一 : LoggingModule 類
?????? LoggingModule 是 axis2 模塊的實現類, axis2 的模塊應該實現 org.apache.axis2.modules.Module 接口的方法 :
?????? // 實始化模塊操作
?????? Public void init(ConfigurationContext configContext,AxisModule module) throws AxisFault;
?????? // 模塊結束的處理
?????? Public void shutdown(AxisConfiguration axisSystem) throws AxisFault;
?????? Public void engageNotify(AxisDescription axisDescription) throws AxisFault;
?????? 此處的 loggingModule 中這三個方法的實現為空。
?????? 步驟二 : LogHandler
?????? Axis2 中的 module 可以包含有一個或更多個 handler , handler 可以在不同階段( phase )處理各種各樣的 SOAP 頭。要編寫一個 handler ,需要實現 org.apache.axis2.engin.Handler 類。便利其見, org.apache.axis2.handlers.AbstractHandler 類提供 Handler 接口的抽象實現。在 hander 中,方法 invoke 和 revoke 表示如下:
Public void invoke(MessageContext ctx)// 將控制權交給 handler 時由 axis2 的引擎調用 invoke
Public void revoke(MessageContext ctx)// 當 hander 被撤消時由 axis2 引擎來調用 revoke 方法
位置: F:\protest\axis2\myservicemodule\loggingmodule
?????? 步驟三 : module.xml
?????? Module.xml 文件包含了對特殊模塊的部署配置。它包含的信息詳細如:模塊的實現類(如 LoggingModule 類可以在不同的階段運行), module.xml 文件配置信息中主要的元素有表現了不同階段的說明:
?Inflow :代表了 handler chain 在一個消息到來時運行
?Outflow :代表了 handler chain 在消息離開時運行
?Outfaultflow :代表 handler chain 將在出現故障并且故障離開時運行
?Infaultflow :代表 handler chain 將在出現故障并且故障進入時運行。
?????? 步驟四 :修改 axis2.xml 文件
?????? 因為“ loggingPhase ”是由模塊的編寫者自己定義的,它不是 axis2 預先定義的,所以需要針其介紹到 axis2.xml 文件中,這樣 axis2 的引擎可以知道在哪兒以不同的“ flows (如 InFlow,OutFlow )”安放 handler 。更改后的 axis2.xml 文件見 axis2 user’s guide 的第四部分。
Axis2.xml 文件中定義的是幾個 phases 。
?????? 步驟五 :修改 services.xml 文件
?????? 在 services.xml 文件中添加 module 元素,使用 ref 來指向使用的模塊,如:
?????? <module ref=”logging”/>
?????? 步驟六 :打包 packaging
?????? 在部署模塊之前,需要創建一個 *.mar 文件 , 對于 loggingmodule 創建的模塊文件為 logging.mar ,使用 jar 生成 logging.jar 文件,再將擴展名更改。
?????? 先編譯系統: F:\proTest\axis2\MyServiceModule>javac -classpath .;%axis2classpath% userguide\loggingmodule\*.java
?????? 步驟七:部署模塊
?????? 將 mar 文件放入 axis2 的 WEB-INF 的 modules 目錄下。
?????? 對于日志文件的修改需要,在 tomcat 下,更改 log4j.properties 文件,屬性文件的位置在 webapps\axis2\WEB-INF\classes 目錄下,將 log4j.rootCategory=ERROR,LOGFILE 改為 log4j.rootCategory=INFO,ERROR,LOGFILE
5、
其他例子
?????? 現實例子說明使用 axis2 的功能 。
?????? Google spell checker sample
?????? Google search sample
?????? Amazon queuing sample
Google spell checker sample : 提供的服務是拼寫檢查,使用了 blocking 和 non-blocking 模式來調用服務。 \samples\googleSpellcheck
Google Search Sample: 包含一個檢查程序,它使用了在 SOAP API 之上的檢查。客戶端使用 non-blocking 模式。 Samples\googleSearch
Amazon Queuing Service :說明如何使用 Amazon 查詢服務。它有兩個用戶接口,一個是用于入隊( enqueue ),另一個是出隊( dequeue )。例子位置 samples\amazonQS