webservice系列3---chain

来源:互联网 发布:mac怎么注销用户 编辑:程序博客网 时间:2024/06/14 17:11

本节摘要:本节主要介绍webservice的高级特性chain的开发和配置

1.引言

      之前在上webservice系列2---javabean&handler中讲了handler的使用,当有多个handler的时候,难道我们要一个一个的在wsdd文件中配置,然后一个一个的引入到需要的webservice中码?of course ,no。Apache组织已经替我们考虑到了这种需求,chain就是专门用来处理这种情况的。chain,英文解释是链的意思,在我们这里,就是把多个handler串起来组成一个链子。

 

2.项目环境

system:win7

myeclipse:6.5

tomcat:5.0

JDK:开发环境1.5,编译环境1.4

project

此工程在上一节的基础之上增加了一个GroupServiceHandler2类和GroupServiceChain类;

GroupServiceHandler2.java复制了GroupServiceHandler1.java;

GroupServiceChain.java是一个chain处理类;

 

3.chain介绍

    Handler Chain是实现一连串多个Handler的链,Chain必须继承SimpleChain类,然后在构造方法中使用:this.addHandler(handler对象);将Handler加入到Chain中。

那么,根据以上的描述,开发一个chain的流程如下:

(1)新建一个类,然后继承SimpleChain类;

(2)在wsdd配置文件中加入chain处理类,通过<chain></chain>标签加入;

(3)在需要的webservice服务中引入chain,通过<requestFlow></requestFlow>标签引入;

 

4.项目代码

声明:下面中写的同上,指的是和上一节博客webservice系列2---javabean&handler中的一模一样;

新建工程,导入jar包,jar包同上;

配置文件

web.xml---web.xml同上;

server-config.wsdd

server-config.wsdd
  1 <?xml version="1.0" encoding="UTF-8"?>  2 <deployment xmlns="http://xml.apache.org/axis/wsdd/"  3     xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">  4     <globalConfiguration>  5         <parameter name="sendMultiRefs" value="true" />  6         <parameter name="disablePrettyXML" value="true" />  7         <parameter name="adminPassword" value="admin" />  8         <parameter name="attachments.Directory"  9             value="D:\tomcat5\webapps\WebService\WEB-INF\attachments" /> 10         <parameter name="dotNetSoapEncFix" value="true" /> 11         <parameter name="enableNamespacePrefixOptimization" 12             value="false" /> 13         <parameter name="sendXMLDeclaration" value="true" /> 14         <parameter name="sendXsiTypes" value="true" /> 15         <parameter name="attachments.implementation" 16             value="org.apache.axis.attachments.AttachmentsImpl" /> 17         <requestFlow> 18             <handler type="java:org.apache.axis.handlers.JWSHandler"> 19                 <parameter name="scope" value="session" /> 20             </handler> 21             <handler type="java:org.apache.axis.handlers.JWSHandler"> 22                 <parameter name="scope" value="request" /> 23                 <parameter name="extension" value=".jwr" /> 24             </handler> 25         </requestFlow> 26     </globalConfiguration> 27     <handler name="LocalResponder" 28         type="java:org.apache.axis.transport.local.LocalResponder" /> 29     <handler name="URLMapper" 30         type="java:org.apache.axis.handlers.http.URLMapper" /> 31     <handler name="Authenticate" 32         type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" /> 33     <service name="AdminService" provider="java:MSG"> 34         <parameter name="allowedMethods" value="AdminService" /> 35         <parameter name="enableRemoteAdmin" value="false" /> 36         <parameter name="className" value="org.apache.axis.utils.Admin" /> 37         <namespace>http://xml.apache.org/axis/wsdd/</namespace> 38     </service> 39     <service name="Version" provider="java:RPC"> 40         <parameter name="allowedMethods" value="getVersion" /> 41         <parameter name="className" value="org.apache.axis.Version" /> 42     </service> 43  44     <transport name="http"> 45         <requestFlow> 46             <handler type="URLMapper" /> 47             <handler 48                 type="java:org.apache.axis.handlers.http.HTTPAuthHandler" /> 49         </requestFlow> 50         <parameter name="qs:list" 51             value="org.apache.axis.transport.http.QSListHandler" /> 52         <parameter name="qs:wsdl" 53             value="org.apache.axis.transport.http.QSWSDLHandler" /> 54         <parameter name="qs.list" 55             value="org.apache.axis.transport.http.QSListHandler" /> 56         <parameter name="qs.method" 57             value="org.apache.axis.transport.http.QSMethodHandler" /> 58         <parameter name="qs:method" 59             value="org.apache.axis.transport.http.QSMethodHandler" /> 60         <parameter name="qs.wsdl" 61             value="org.apache.axis.transport.http.QSWSDLHandler" /> 62     </transport> 63     <transport name="local"> 64         <responseFlow> 65             <handler type="LocalResponder" /> 66         </responseFlow> 67     </transport> 68  69     <!-- 配置一个chain,用来统计webservice的访问次数 --> 70     <chain name="GroupServiceChain1"> 71         <handler type="java:com.server.chain.GroupServiceChain"/> 72     </chain> 73  74     <!-- 配置自己的服务 --> 75     <service name="GroupService" provider="java:RPC"> 76        <!-- 暴露部分方法 77        <parameter name="allowedMethods" value="方法1,方法2" /> 78         --> 79         <parameter name="allowedMethods" value="*" /> 80         <parameter name="className" 81             value="com.server.impl.IGroupInfoServiceImpl" /> 82         <!-- 配置javabean  --> 83         <beanMapping 84             languageSpecificType="java:com.server.bean.GroupInfo" 85             qname="ns:GroupInfo" xmlns:ns="urn:BeanService"> 86         </beanMapping> 87  88         <beanMapping 89             languageSpecificType="java:com.server.bean.UserInfo" 90             qname="ns:UserInfo" xmlns:ns="urn:BeanService"> 91         </beanMapping> 92  93  94         <!-- 引入一个用来统计webservice的访问次数的chain --> 95         <requestFlow> 96            <chain type="GroupServiceChain1"/> 97         </requestFlow> 98  99     </service>100 </deployment>

 

服务端代码

(1)javabean

GroupInfo.java和UserInfo.java代码同上;

(2)服务类和接口

IGroupInfoService.java和IGroupInfoServiceImpl.java文件的代码同上;

(3)handler处理类

GroupServiceHandler.java代码同上;

GroupServiceHandler2.java---也是统计webservice访问次数的,直接把GroupServiceHandler拷贝过来稍作修改;

GroupServiceHandler2.java
 1 package com.server.wsddhandler; 2  3 import org.apache.axis.AxisFault; 4 import org.apache.axis.MessageContext; 5 import org.apache.axis.handlers.BasicHandler; 6  7 /* 8  * 采用handler统计webservice的访问次数 9  */10 public class GroupServiceHandler2 extends BasicHandler {11 12     private static final long serialVersionUID = 2L;13 14     private static long COUNT = 0L;15 16     private int requestCount = 0;17 18     public void invoke(MessageContext arg0) throws AxisFault {19         requestCount++;20         COUNT++;21         //拿到配置文件中配置的属性名称status对应的属性值22         String status = (String) this.getOption("status");23         System.out.println("This is an other handler. GroupServiceHandler2's status is:" + status24                 + ",COUNT=" + COUNT + ",HandlerRequestCount=" + requestCount);25 26     }27 28 };

(4)chain处理类

GroupServiceChain.java

GroupServiceChain.java
 1 package com.server.chain; 2  3 import org.apache.axis.SimpleChain; 4  5 import com.server.wsddhandler.GroupServiceHandler; 6 import com.server.wsddhandler.GroupServiceHandler2; 7  8 public class GroupServiceChain extends SimpleChain { 9 10     private static final long serialVersionUID = 3L;11 12     public GroupServiceChain(){13         GroupServiceHandler handler1=new GroupServiceHandler();14         GroupServiceHandler2 handler2=new GroupServiceHandler2();15         16         this.addHandler(handler1);17         this.addHandler(handler2);18     }19 20 }

 

客户端代码

WebServiceClientUtil.java的代码同上,只是需要修改一下wsdl地址;

原有的以下两行改成:

String wsdl_url = "http://localhost:8080/WebService03/services/GroupService?wsdl";
String qname_url = "http://localhost:8080/WebService03/services/GroupService";

 

5.运行结果

发布项目,启动tomcat服务器;

(1)在浏览器输入wsdl地址

http://localhost:8080/WebService03/services/GroupService?wsdl

(2)采用webservice客户端调用,查看返回结果

客户端后台日志:

服务端后台日志:

0 0
原创粉丝点击