?????? Axis2 是下一代的 Apache Axis ,它的體系結(jié)構(gòu)和原來(lái)的 Axis1.x 有所不同。它體現(xiàn)出更靈活、有效和可配置性。盡管它的體系結(jié)構(gòu)是新的,但在 Axis1.x 版本下有些良好的概念還是保存下來(lái)了,如: handler 在 axis2 中依然保留。
1、
Axis2
所體現(xiàn)的特點(diǎn)有:
Speed :速度快,使用自己的對(duì)象模型和 StAX 來(lái)解析 achieve 。
Low memory foot print :
AXIOM :使用自己的輕量級(jí)對(duì)象模型 AXIOM ,用于消息處理, axiom 是具有可擴(kuò)展性、高性能和開發(fā)便利的優(yōu)點(diǎn)。
Hot Deployment :可進(jìn)行熱部署,在部署新的服務(wù)時(shí)不需要重新啟動(dòng)服務(wù)。
Asynchronous Web Services : 支持異步服務(wù)。
MEP Support : 支持消息交換模式( Message Exchange Patterns : MEPs ) ,MEP 是內(nèi)嵌式的,支持 WSDL2.0 中的基本的 MEPs 。
Flexibility :具有靈活性,使得開發(fā)者自由向引擎中插入擴(kuò)展功能。
Stability :穩(wěn)定性。定義一系列的公布的接口,這些接口相對(duì)于其余的 Axis 改變得相對(duì)較慢。
Component-Oriented Deployment : 面向組件的部署。可以輕松定義可復(fù)用的網(wǎng)絡(luò)或 handler 來(lái)實(shí)現(xiàn)常規(guī)應(yīng)用程序的處理或?qū)⑵浞峙浣o其伙伴。
Transport Framework :
WSDL 支持: 支 WSDL 1.1 和 WSDL 2.0 版本 。
Add-ons :
Composition and Extensibility: 模塊支持組合和擴(kuò)展功能。
Axis2
的
samples
:
binary
發(fā)布中的
samples/userguide/src
目錄下
2、
Web Services Using Axis2
?????? 使用 axis2 寫 web services 有兩種方法
ü ???????? 使用 Axis2 的基本接口( Primary API )來(lái)實(shí)現(xiàn)業(yè)務(wù)邏輯
ü ???????? 從 WSDL 文件生成 Skeleton ,再實(shí)現(xiàn)業(yè)務(wù)邏輯
1) ????? 使用 Axis2 的基本接口( Primary API )來(lái)實(shí)現(xiàn)業(yè)務(wù)邏輯
????????????? 主要的操作是:
????????????? A :寫實(shí)現(xiàn)類
????????????? B :寫一個(gè) services.xml 來(lái)解釋 web service
??????? C :為 web 服務(wù)創(chuàng)建一個(gè) aar 文檔
??????? D :將文檔部署
?????? 步驟一:編寫類文件,描述 web service 的業(yè)務(wù)邏輯
public class MyService{
??? public void ping(OMElement element){
???? ......
??? }
??? public OMElement echo(OMElement element){
???? ......
??? }
}
// 這個(gè)類中給了兩個(gè)方法,一個(gè)是只有輸入,另一個(gè)是有輸入和輸出,輸入輸出類型都是 OMElement |
?????? 步驟二:為 web service 編寫配置文件 services.xml
?????? ?????? 一個(gè) services.xml 文件中可以寫多個(gè) web 服務(wù)的配置信息,可以對(duì)一組相關(guān)的服務(wù) 進(jìn)行配置管理,使用 <ServiceGroup> 元素包含多個(gè) <service> 元素
<service >
??? <description>
??????? This is a sample Web Service with two operations, echo and ping.
</description>
<!- -
描述服務(wù)的類名
-- >
<parameter name="ServiceClass" locked="false">userguide.example1.MyService</parameter>
<!—
分別描述服務(wù)中的操作,每個(gè)操作都有相應(yīng)的
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> |
?????? 步驟三:創(chuàng)建 archive 文件
?????? Archive 文件的目錄結(jié)構(gòu)如下圖所示,在當(dāng)前目錄下,創(chuàng)建一個(gè) META-INF 目錄, services.xml 文件就存放于此目錄,當(dāng)前目錄下存放的是源文件,可以先創(chuàng)建一個(gè) jar 文件,再將其更改擴(kuò)展名。
?????? 可以將命令行切換到 example1 目錄下,鍵入命令: jar cvf MyService.jar .
之后將 jar 更為 aar
?????? 步驟四:部署文件
????????????? 將 aar 文件復(fù)制到 axis2 的 WEB-INF 目錄下的 service 目錄下,或者使用它提供的 ?? upload service 接口直接部署。
?????? REST: 第二代 Web services 使用的體系結(jié)構(gòu),而不是使用 SOAP 。 REST 代表: Representational State Transfer 。表示每一個(gè) URL 都表示一個(gè)對(duì)象資源。可以使用 HTTP Get 獲得對(duì)象的內(nèi)容,或者是刪除、或?qū)?duì)象進(jìn)行更改。
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 ,再實(shí)現(xiàn)業(yè)務(wù)邏輯
??????
?????? 步驟一:根據(jù) WSDL 文件生成 skeleton
????????????? 使用 axis2 提供的 WSDL2Java 工具生成 skeleton ,工具使用的參數(shù)說(shuō)明如下
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 目錄下。
?????? 步驟二:實(shí)現(xiàn)商業(yè)邏輯
?????? ? 在 src 目錄下的目錄中找到 Axis2SampleDocLitTypeSkeleton.java 文件,它就是 web service 的 skeleton ,對(duì)其中的方法進(jìn)行編輯來(lái)實(shí)現(xiàn)商業(yè)邏輯。
?????? 步驟三: services.xml
?????? 生成 skeleton 時(shí),服務(wù)的配置文件 services.xml 文件是自動(dòng)生成的,存放于 resources 目錄下。
?????? 步驟四:打包( packaging )
?????? 將類文件打包形成 aar 文件,然后將其部署到 axis2 下的 services 目錄下。使用 WSDL2Java 工具生成 skeleton 時(shí)同時(shí)產(chǎn)生的數(shù)據(jù)綁定類在 schemaorg_apache_xmlbeans 目錄下,也需要將其復(fù)制到類路徑下,將 skeleton 和支持的文件進(jìn)行編譯。將需要的目錄文件以下面的結(jié)構(gòu)存放,編譯之后打包生成 aar 文件。
即將 services.xml 文件放于 META-INF 目錄下,將也將 schemaorg_apache-xmlbeans 目錄和 skeleton 所在的源文件目錄放于一個(gè)目錄下。同時(shí)將 wsdl 文件也放于 META-INF 目錄下。
對(duì) perf 的操作如下:
在 src 目錄下, javac –classpath .;%axis2classpath% org\apache\axis2\userguide\*.java
編譯成功之后,將 schemaorg_apache_xmlbeans 目錄拷貝到 src 目錄下,建立一個(gè) META-INF 目錄,將 resources 目錄下的 services.xml 文件和 *.wsdl 文件復(fù)制到 META-INF 目錄下
在 src 目錄下,運(yùn)行 jar 命令 jar -cfv perf.jar META-INF org schemaorg_apache_xmlbeans
將 perf.jar 改名為 aar 部署到 tomcat 容器中。
或者是在編輯完 skeleton 后,運(yùn)行生成的 build.xml ,即 ant 命令,則 ant 會(huì)對(duì)程序進(jìn)行編譯,成功之后,把生成的 build 目錄下的 classes 下的 org 目錄和 META-INF 還有 resources
目錄下的 schemaorg_apache_xmlbeans 目錄添加到一個(gè) jar 文件即可,更改 jar 擴(kuò)展名為 aar 擴(kuò)展名。
3、
Web Service
客戶端使用Axis2調(diào)用服務(wù)
?????? 客戶端調(diào)用使用可以使用阻塞和非阻塞 API 。
?????? 阻塞模式( Blocking API ):一旦服務(wù)被調(diào)用,客戶端處于阻塞狀態(tài),它不能執(zhí)行其他的操作,除非收到服務(wù)端發(fā)送的響應(yīng)信息或是出錯(cuò)信息。(同步模式調(diào)用)
?????? 非阻塞模式( Non-Blocking API ):這是一種基于回調(diào)或者是輪詢的 API 。當(dāng)服務(wù)調(diào)用以后,客戶端立刻能獲得控制權(quán)進(jìn)行其他操作,服務(wù)端的返回消息使用提供的 callback 對(duì)象得到。這樣可以使得客戶端應(yīng)用程序可以同時(shí)調(diào)用多個(gè) web services 而不用阻塞已經(jīng)調(diào)用的操作。(異步模式調(diào)用)它們都是在 API 層實(shí)現(xiàn)。
?????? 根據(jù) API 層的異步調(diào)用和傳輸層的異步調(diào)用 ,可以得到四種不同模式來(lái)調(diào)用 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 |
客戶端調(diào)用 web 服務(wù)也有兩種方法:
ü ???????? 編寫 axis2 的主要的 API
ü ???????? 使用產(chǎn)生的 Stub 編寫
1) ????? 使用 Axis2 的原始的 API 編寫客戶端來(lái)調(diào)用 Web Service
?????? 測(cè)試的例子在 Eclipse 集成開發(fā)環(huán)境下使用 ,例子見 axis2 的 samples 或是 F:\proTest\axis2\ClientInvoke 下的實(shí)例。
2) ????? 使用 axis2 的代碼生成綁定數(shù)據(jù)來(lái)編寫 service 。
?????? 使用 WSDL2Java 命令來(lái)生成 stub ,對(duì)于 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
文件,編譯時(shí)可以直接使用
WSDL2Java
生成的
build.xml
文件使用
ant
命令進(jìn)行編譯。
4、 模塊(Module)
?????? 為一個(gè)給定服務(wù)自定義模塊步驟有:
(1) ?????? 創(chuàng)建模塊的實(shí)現(xiàn)
(2) ?????? 創(chuàng)建 handler
(3) ?????? 創(chuàng)建 module.xml 文件
(4) ?????? 如果需要自定義 phases ,則要修改 axis2.xml
(5) ?????? 修改 services.xml 文件使之在部署時(shí)結(jié)合 engage 模塊
(6) ?????? 在一個(gè) *.mar 文件中打包
(7) ?????? 將模塊在 axis2 中部署
例子:在 MyService 服務(wù)中添加一個(gè) logging 模塊
?????? Logging 模塊的功能是它包含一個(gè) handler ,此 handler 能夠記錄被傳遞的消息。在 axis2 中,使用 .mar 文件來(lái)部署模塊。模塊的結(jié)構(gòu)如下圖所示:
?????? 步驟一 : LoggingModule 類
?????? LoggingModule 是 axis2 模塊的實(shí)現(xiàn)類, axis2 的模塊應(yīng)該實(shí)現(xiàn) org.apache.axis2.modules.Module 接口的方法 :
?????? // 實(shí)始化模塊操作
?????? Public void init(ConfigurationContext configContext,AxisModule module) throws AxisFault;
?????? // 模塊結(jié)束的處理
?????? Public void shutdown(AxisConfiguration axisSystem) throws AxisFault;
?????? Public void engageNotify(AxisDescription axisDescription) throws AxisFault;
?????? 此處的 loggingModule 中這三個(gè)方法的實(shí)現(xiàn)為空。
?????? 步驟二 : LogHandler
?????? Axis2 中的 module 可以包含有一個(gè)或更多個(gè) handler , handler 可以在不同階段( phase )處理各種各樣的 SOAP 頭。要編寫一個(gè) handler ,需要實(shí)現(xiàn) org.apache.axis2.engin.Handler 類。便利其見, org.apache.axis2.handlers.AbstractHandler 類提供 Handler 接口的抽象實(shí)現(xiàn)。在 hander 中,方法 invoke 和 revoke 表示如下:
Public void invoke(MessageContext ctx)// 將控制權(quán)交給 handler 時(shí)由 axis2 的引擎調(diào)用 invoke
Public void revoke(MessageContext ctx)// 當(dāng) hander 被撤消時(shí)由 axis2 引擎來(lái)調(diào)用 revoke 方法
位置: F:\protest\axis2\myservicemodule\loggingmodule
?????? 步驟三 : module.xml
?????? Module.xml 文件包含了對(duì)特殊模塊的部署配置。它包含的信息詳細(xì)如:模塊的實(shí)現(xiàn)類(如 LoggingModule 類可以在不同的階段運(yùn)行), module.xml 文件配置信息中主要的元素有表現(xiàn)了不同階段的說(shuō)明:
?Inflow :代表了 handler chain 在一個(gè)消息到來(lái)時(shí)運(yùn)行
?Outflow :代表了 handler chain 在消息離開時(shí)運(yùn)行
?Outfaultflow :代表 handler chain 將在出現(xiàn)故障并且故障離開時(shí)運(yùn)行
?Infaultflow :代表 handler chain 將在出現(xiàn)故障并且故障進(jìn)入時(shí)運(yùn)行。
?????? 步驟四 :修改 axis2.xml 文件
?????? 因?yàn)椤?/span> loggingPhase ”是由模塊的編寫者自己定義的,它不是 axis2 預(yù)先定義的,所以需要針其介紹到 axis2.xml 文件中,這樣 axis2 的引擎可以知道在哪兒以不同的“ flows (如 InFlow,OutFlow )”安放 handler 。更改后的 axis2.xml 文件見 axis2 user’s guide 的第四部分。
Axis2.xml 文件中定義的是幾個(gè) phases 。
?????? 步驟五 :修改 services.xml 文件
?????? 在 services.xml 文件中添加 module 元素,使用 ref 來(lái)指向使用的模塊,如:
?????? <module ref=”logging”/>
?????? 步驟六 :打包 packaging
?????? 在部署模塊之前,需要?jiǎng)?chuàng)建一個(gè) *.mar 文件 , 對(duì)于 loggingmodule 創(chuàng)建的模塊文件為 logging.mar ,使用 jar 生成 logging.jar 文件,再將擴(kuò)展名更改。
?????? 先編譯系統(tǒng): F:\proTest\axis2\MyServiceModule>javac -classpath .;%axis2classpath% userguide\loggingmodule\*.java
?????? 步驟七:部署模塊
?????? 將 mar 文件放入 axis2 的 WEB-INF 的 modules 目錄下。
?????? 對(duì)于日志文件的修改需要,在 tomcat 下,更改 log4j.properties 文件,屬性文件的位置在 webapps\axis2\WEB-INF\classes 目錄下,將 log4j.rootCategory=ERROR,LOGFILE 改為 log4j.rootCategory=INFO,ERROR,LOGFILE
5、
其他例子
?????? 現(xiàn)實(shí)例子說(shuō)明使用 axis2 的功能 。
?????? Google spell checker sample
?????? Google search sample
?????? Amazon queuing sample
Google spell checker sample : 提供的服務(wù)是拼寫檢查,使用了 blocking 和 non-blocking 模式來(lái)調(diào)用服務(wù)。 \samples\googleSpellcheck
Google Search Sample: 包含一個(gè)檢查程序,它使用了在 SOAP API 之上的檢查。客戶端使用 non-blocking 模式。 Samples\googleSearch
Amazon Queuing Service :說(shuō)明如何使用 Amazon 查詢服務(wù)。它有兩個(gè)用戶接口,一個(gè)是用于入隊(duì)( enqueue ),另一個(gè)是出隊(duì)( dequeue )。例子位置 samples\amazonQS