Axis可以通过Module模块进行扩展,用户可以编写定制自己的Module模块。编写一个Module的模块至少需要实现两个接口,分别是Handler和Module接口。开发axis2的Module模块需要如下步骤:
1、 实现Module接口的实现类,这个类要完成基本的初始化、销毁等操作
2、 实现Handler接口的实现类,这个类主要是完成业务处理
3、 在META-INF目录下,创建module.xml配置文件
4、 在axis2.xml中增加配置module的模块
5、 在services.xml中增加module的模块配置
6、 最后发表axis2的module模块,需要用jar命令将工程打包成mar,然后将mar文件发布到[tomcat_home]/webapps/axis2/WEB-INF/modules目录下;
首先编写一个简单的WebService,代码如下:
代码
package
com.hoo.module;
import
java.util.Random;
/** * <b>function:</b>编写一个简单的WebService服务器端代码 * @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接口的实现类,代码如下:
代码
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模块,实现Module接口实现类 * @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#############
");
}
}
编写实现Handler接口的实现类,代码如下:
代码
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接口实现 * @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文件中加入内容,在所有的<phaseOrder>标签中加入 <phase name="customPhase"/>
打包发布module,在c盘建立CustomModuleService,然后将CustomModule.class和CustomHandler.class以及类路径目录复制到该目录。然后将module.xml文件放到META-INF(没有新建)目录。
运行jar命令:jar cvf custom-module.mar .
将生成的custom-module.mar文件粘贴到[tomcat_home] \webapps\axis2\WEB-INF\modules目录中
发布WebService,建立目录simpleWebService,将SimpleWebService.xml和类路径复制到该目录下,将services.xml复制到META-INF目录。
运行jar命令:jar cvf simple-service.aar .
将生成的simple-service.aar文件复制到[tomcat_home] \webapps\axis2\WEB-INF\services目录下
然后重启tomcat服务。
客户端访问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]);
}
}
更多文章、技术交流、商务合作、联系博主
微信扫码或搜索:z360901061
微信扫一扫加我为好友
QQ号联系: 360901061
您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】元

