九、 编写Module模块

33 篇文章 0 订阅
33 篇文章 9 订阅

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]);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值