Axis可以通過Module模塊進(jìn)行擴(kuò)展,用戶可以編寫定制自己的Module模塊。編寫一個(gè)Module的模塊至少需要實(shí)現(xiàn)兩個(gè)接口,分別是Handler和Module接口。開發(fā)axis2的Module模塊需要如下步驟:
1、 實(shí)現(xiàn)Module接口的實(shí)現(xiàn)類,這個(gè)類要完成基本的初始化、銷毀等操作
2、 實(shí)現(xiàn)Handler接口的實(shí)現(xiàn)類,這個(gè)類主要是完成業(yè)務(wù)處理
3、 在META-INF目錄下,創(chuàng)建module.xml配置文件
4、 在axis2.xml中增加配置module的模塊
5、 在services.xml中增加module的模塊配置
6、 最后發(fā)表axis2的module模塊,需要用jar命令將工程打包成mar,然后將mar文件發(fā)布到[tomcat_home]/webapps/axis2/WEB-INF/modules目錄下;
首先編寫一個(gè)簡(jiǎn)單的WebService,代碼如下:
代碼
package
com.hoo.module;
import
java.util.Random;
/** * <b>function:</b>編寫一個(gè)簡(jiǎn)單的WebService服務(wù)器端代碼 * @author hoojo * @createDate 2011-3-15 上午06:19:15 * @file SimpleWebService.java * @package com.hoo.module * @project Axis2WebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */
public
class
SimpleWebService {
public
int
randInt() {
return
new
Random().nextInt(100);
}
}
編寫Module接口的實(shí)現(xiàn)類,代碼如下:
代碼
package
com.hoo.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;
/** * <b>function:</b>axis2模塊,實(shí)現(xiàn)Module接口實(shí)現(xiàn)類 * @author hoojo * @createDate 2011-3-15 上午03:22:24 * @file CustomModule.java * @package com.hoo.module * @project Axis2WebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */
public
class
CustomModule
implements
Module {
public
void
applyPolicy(Policy policy, AxisDescription axisDescription)
throws
AxisFault {
System.out.println("
###############applyPolicy##############
");
}
public
boolean
canSupportAssertion(Assertion assertion) {
System.out.println("
##############canSupportAssertion##############
");
return
true
;
}
public
void
engageNotify(AxisDescription axisDescription)
throws
AxisFault {
System.out.println("
##############engageNotify#############
");
}
public
void
init(ConfigurationContext ctx, AxisModule module)
throws
AxisFault {
System.out.println("
##############init##############
");
}
public
void
shutdown(ConfigurationContext ctx)
throws
AxisFault {
System.out.println("
##############shutdown#############
");
}
}
編寫實(shí)現(xiàn)Handler接口的實(shí)現(xiàn)類,代碼如下:
代碼
package
com.hoo.module;
import
org.apache.axis2.AxisFault;
import
org.apache.axis2.context.MessageContext;
import
org.apache.axis2.description.HandlerDescription;
import
org.apache.axis2.description.Parameter;
import
org.apache.axis2.engine.Handler;
/** * <b>function:</b>axis2模塊Handler接口實(shí)現(xiàn) * @author hoojo * @createDate 2011-3-15 上午03:37:43 * @file CustomHandler.java * @package com.hoo.module * @project Axis2WebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */
public
class
CustomHandler
implements
Handler {
private
String name;
public
void
setName(String name) {
this
.name = name;
}
public
String getName() {
return
this
.name;
}
public
void
cleanup() {
System.out.println("
###########cleanup###########
");
}
public
void
flowComplete(MessageContext ctx) {
System.out.println("
###########flowComplete###########
");
System.out.println(ctx.getEnvelope().toString());
}
public
HandlerDescription getHandlerDesc() {
System.out.println("
###########getHandlerDesc###########
");
return
null
;
}
public
Parameter getParameter(String name) {
System.out.println("
###########getParameter###########
");
return
null
;
}
public
void
init(HandlerDescription handlerDescription) {
System.out.println("
###########init###########
");
}
public
InvocationResponse invoke(MessageContext ctx)
throws
AxisFault {
System.out.println(ctx.getEnvelope().toString());
return
InvocationResponse.CONTINUE;
}
}
編寫module.xml文件
代碼
<
module
name
=
"customModule"
class
=
"com.hoo.module.CustomModule"
>
<
InFlow
>
<
handler
name
=
"InFlowLogHandler"
class
=
"com.hoo.module.CustomHandler"
>
<
order
phase
=
"customPhase"
/>
</
handler
>
</
InFlow
>
<
OutFlow
>
<
handler
name
=
"OutFlowLogHandler"
class
=
"com.hoo.module.CustomHandler"
>
<
order
phase
=
"customPhase"
/>
</
handler
>
</
OutFlow
>
<
InFaultFlow
>
<
handler
name
=
"FaultInFlowLogHandler"
class
=
"com.hoo.module.CustomHandler"
>
<
order
phase
=
"customPhase"
/>
</
handler
>
</
InFaultFlow
>
<
OutFaultFlow
>
<
handler
name
=
"FaultOutFlowLogHandler"
class
=
"com.hoo.module.CustomHandler"
>
<
order
phase
=
"customPhase"
/>
</
handler
>
</
OutFaultFlow
>
</
module
>
編寫services.xml文件
代碼
<
service
name
=
"CustomModuleService"
>
<
description
>
使用CustomModule SimpleWebService模塊
</
description
>
<!-- 引用customModule模塊 -->
<
module
ref
=
"customModule"
/>
<
parameter
name
=
"ServiceClass"
>
com.hoo.module.SimpleWebService
</
parameter
>
<
messageReceivers
>
<
messageReceiver
mep
=
"http://www.w3.org/2004/08/wsdl/in-out"
class
=
"org.apache.axis2.rpc.receivers.RPCMessageReceiver"
/>
<
messageReceiver
mep
=
"http://www.w3.org/2004/08/wsdl/in-only"
class
=
"org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"
/>
</
messageReceivers
>
</
service
>
在[tomcat_home]\webapps\axis2\WEB-INF\conf中axis2.xml文件中加入內(nèi)容,在所有的<phaseOrder>標(biāo)簽中加入 <phase name="customPhase"/>
打包發(fā)布module,在c盤建立CustomModuleService,然后將CustomModule.class和CustomHandler.class以及類路徑目錄復(fù)制到該目錄。然后將module.xml文件放到META-INF(沒有新建)目錄。
運(yùn)行jar命令:jar cvf custom-module.mar .
將生成的custom-module.mar文件粘貼到[tomcat_home] \webapps\axis2\WEB-INF\modules目錄中
發(fā)布WebService,建立目錄simpleWebService,將SimpleWebService.xml和類路徑復(fù)制到該目錄下,將services.xml復(fù)制到META-INF目錄。
運(yùn)行jar命令:jar cvf simple-service.aar .
將生成的simple-service.aar文件復(fù)制到[tomcat_home] \webapps\axis2\WEB-INF\services目錄下
然后重啟tomcat服務(wù)。
客戶端訪問WebService代碼
代碼
package
com.hoo.service;
import
javax.xml.namespace.QName;
import
org.apache.axis2.AxisFault;
import
org.apache.axis2.addressing.EndpointReference;
import
org.apache.axis2.client.Options;
import
org.apache.axis2.rpc.client.RPCServiceClient;
/** * <b>function:</b>訪問SimpleWebService * @author hoojo * @createDate 2011-3-15 上午07:26:08 * @file SimpleWebServiceClient.java * @package com.hoo.service * @project Axis2WebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */
public
class
SimpleWebServiceClient {
public
static
void
main(String[] args)
throws
AxisFault {
String target = "
http://localhost:8080/axis2/services/CustomModuleService
";
RPCServiceClient client =
new
RPCServiceClient();
Options options = client.getOptions();
options.setManageSession(
true
);
EndpointReference epr =
new
EndpointReference(target);
options.setTo(epr);
QName qname =
new
QName("
http://module.hoo.com
", "
randInt
");
Object[] result = client.invokeBlocking(qname,
new
Object[] {
null
},
new
Class[] {
int
.
class
});
System.out.println(result[0]);
}
}
九、 編寫Module模塊