编写Module模块

简介: 九、 编写Module模块 Axis可以通过Module模块进行扩展,用户可以编写定制自己的Module模块。编写一个Module的模块至少需要实现两个接口,分别是Handler和Module接口。
九、 编写Module模块

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,代码如下:

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif 代码 package com.hoo.module; import java.util.Random; /** * function:编写一个简单的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接口的实现类,代码如下:

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif 代码 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; /** * function: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接口的实现类,代码如下:

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif 代码 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; /** * function: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文件

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif 代码 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文件

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif 代码 service name="CustomModuleService"> description>使用CustomModule SimpleWebService模块description> 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文件中加入内容,在所有的标签中加入

打包发布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代码

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif 代码 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; /** * function:访问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]); } }

目录
相关文章
|
10月前
|
前端开发 JavaScript
模块打包中CommonJS与ES6 Module的导入与导出问题详解
文章全面解析了CommonJS模块系统的模块定义、导出、导入的操作和注意事项。同时,它也简要地提到了ES6 Module的相关概念,包括命名导出、默认导出、命名导入、默认导入、混合导入和复合写法。
401 0
|
10月前
|
JavaScript 前端开发 编译器
CommonJS与ES6 Module的本质区别
文章主要讨论了CommonJS和ES6 Module两种JavaScript模块系统的核心区别,包括动态与静态解决依赖方式,值拷贝与动态映射,以及如何处理循环依赖的问题。
113 0
|
JavaScript 前端开发 API
【ES6】Module模块详解
【ES6】Module模块详解
172 0
|
JavaScript
es6 Module和commonjs的区别
es6 Module和commonjs的区别
|
Java Android开发
Java工具IDEA创建模块(Module)、如何创建 Module:、如何删除模块
Java工具IDEA创建模块(Module)、如何创建 Module:、如何删除模块
Java工具IDEA创建模块(Module)、如何创建 Module:、如何删除模块
|
Python
Python编程:importlib.import_module动态导入模块
Python编程:importlib.import_module动态导入模块
319 0
|
前端开发 JavaScript Shell
十七、详解 ES6 Modules
对于新人朋友来说,想要自己去搞定一个ES6开发环境并不是一件容易的事情,因为构建工具的学习本身又是一个非常大的方向,我们需要花费不少的时间才能掌握它。 好在慢慢的开始有大神提供了一些非常简单易懂,学习成本非常低的解决方案来帮助大家学习。create-react-app就是这些解决方案中,个人认为最简单易懂的一种方式。
145 0
十七、详解 ES6 Modules
ES6(Module 模块化)
模块化 ES6的模块化的基本规则或特点: 1:每一个模块只加载一次, 每一个JS只执行一次, 如果下次再去加载同目录下同文件,直接从内存中读取。
972 0
|
前端开发 JavaScript API
深入ES6 模块系统
深入ES6 模块系统 本文转载自:众成翻译 译者:neck 链接:http://www.zcfy.cc/article/4436 原文:https://ponyfoo.com/articles/es6-modules-in-depth#the-es6-module-system ES6 模块系统 在ES6之前,我们用自己的方式来在 JavaScript 中实现模块。
919 0