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
service;
public
class
MyService
{
public
String getGreeting(String name)
{
return
"
您好
"
+
name;
}
}
下面咱們來編寫一個記錄請求和響應SOAP
消息的Axis2
模塊。當客戶端調用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
{
//
initialize the module
public
void
init(ConfigurationContext configContext, AxisModule module)
throws
AxisFault
{
System.out.println(
"
init
"
);
}
public
void
engageNotify(AxisDescription axisDescription)
throws
AxisFault
{
}
//
shutdown the module
public
void
shutdown(ConfigurationContext configurationContext)
throws
AxisFault
{
System.out.println(
"
shutdown
"
);
}
public
String[] getPolicyNamespaces()
{
return
null
;
}
public
void
applyPolicy(Policy policy, AxisDescription axisDescription)
throws
AxisFault
{
}
public
boolean
canSupportAssertion(Assertion assertion)
{
return
true
;
}
}
在本例中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;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
public
class
LogHandler
extends
AbstractHandler
implements
Handler
{
private
static
final
Log log
=
LogFactory.getLog(LogHandler.
class
);
private
String name;
public
String getName()
{
return
name;
}
public
InvocationResponse invoke(MessageContext msgContext)
throws
AxisFault
{
//
向Tomcat控制檯輸出請求和響應SOAP消息
log.info(msgContext.getEnvelope().toString());
return
InvocationResponse.CONTINUE;
}
public
void
revoke(MessageContext msgContext)
{
log.info(msgContext.getEnvelope().toString());
}
public
void
setName(String name)
{
this
.name
=
name;
}
}
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
文件,分別在以下四個<phaseOrder>
元素中加入
<phase name="loggingPhase"/>
:
<
phaseOrder
type
="InFlow"
>
![](http://static.javashuo.com/static/loading.gif)
<
phase
name
="soapmonitorPhase"
/>
<
phase
name
="loggingPhase"
/>
</
phaseOrder
>
<
phaseOrder
type
="OutFlow"
>
![](http://static.javashuo.com/static/loading.gif)
<
phase
name
="Security"
/>
<
phase
name
="loggingPhase"
/>
</
phaseOrder
>
<
phaseOrder
type
="InFaultFlow"
>
![](http://static.javashuo.com/static/loading.gif)
<
phase
name
="soapmonitorPhase"
/>
<
phase
name
="loggingPhase"
/>
</
phaseOrder
>
<
phaseOrder
type
="OutFaultFlow"
>
![](http://static.javashuo.com/static/loading.gif)
<
phase
name
="Security"
/>
<
phase
name
="loggingPhase"
/>
</
phaseOrder
>
第5步:在services.xml文件中引用logging模塊
services.xml
文件的內容以下:
<
service
name
="myService"
>
<
description
>
使用logging模塊
</
description
>
<!--
引用logging模塊
-->
<
module
ref
="logging"
/>
<
parameter
name
="ServiceClass"
>
service.MyService
</
parameter
>
<
messageReceivers
>
<
messageReceiver
mep
="http://www.w3.org/2004/08/wsdl/in-out"
class
="org.apache.axis2.rpc.receivers.RPCMessageReceiver"
/>
</
messageReceivers
>
</
service
>
第6步:發佈logging模塊
到如今爲止,咱們應用能夠創建兩個發行包:logging.mar
和service.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
模塊,該文件的內容以下:
addressing-1.4.1.mar
soapmonitor-1.4.1.mar
ping-1.4.1.mar
mex-1.4.1.mar
axis2-scripting-1.4.1.mar
logging.mar
若是modules
目錄中不包含modules.list
文件,則Axis2
會裝載modules
文件中的全部Axis2
模塊。
如今啓動Tomcat
,使用以下的C#
代碼調用MyService
的getGreeting
方法則會在Tomcat
控制檯中輸出相應的請求和響應SOAP
消息。
//
async是引用MyService的服務名
async.myService my
=
new
WSC.asyn.myService();
MessageBox.Show(my.getGreeting(
"
中國
"
));
MessageBox.Show(
"
完成調用
"
);