Handle 特性:假如客户端有一个请求,要记录某一个webservice被调用的次数,如果我们要把这个功能在webservice Object 里面去实现
不仅浸入了原来的程序,而且增加程序的不稳定性。
(1) server-config.wsdd文件配置如下:
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" />
<handler name="wsTestHandler" type="java:cn.com.chenlly.ssh.webservice.axis.WSTestServiceHandle">//自定义一个handle
<parameter name="status" value="success"/>
</handler>
<!-- 自定义服务 -->
<service name="ws" provider="java:RPC">
<parameter name="className"
value="cn.com.chenlly.ssh.webservice.axis.WSTestServiceImpl"/>
<parameter name="allowedMethods" value="*" />
<parameter name="scope" value="request"/>
<responseFlow> //如果handler配置在responseFlow,那么handler 会在service执行之后执行
<handler type="wsTestHandler"/>
</responseFlow>
<requestFlow> //如果handler配置在requestFlow,那么handler 会在service执行之前执行
<handler type="wsTestHandler"/>
</requestFlow>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper" />
</requestFlow>
</transport>
</deployment>
(2) WSTestServiceHandle.java类如下:
package cn.com.chenlly.ssh.webservice.axis;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
/**
* @Class WSTestServiceHandle.java
* @Description Handle类可以在每个webservice 方法被调用之前或者之后做一些事情
* @Copyright (c) 2010, 正辰科技有限公司 All rights reserved.
* @Author Chenlly
* @Version 1.0
* @Date Apr 19, 2010 1:55:13 PM
*/
public class WSTestServiceHandle extends BasicHandler {
private static long COUNT = 0L;
private int requestCount = 0;
@Override
public void invoke(MessageContext arg0) throws AxisFault {
requestCount++;
COUNT++;
String status = (String) this.getOption("status");//拿到配置文件中配置的属性值
System.out.println(" status is:"+status+", count ="+COUNT+", requestCount = "+requestCount+"");
}
}
//继承BasicHandler
//MessageContext AXIS 的上下文,存储AXIS和WebService 的基本信息
(3) //执行两次WSTestServiceClient 后台结果如下:
//服务器端:
status is:success, count =1, requestCount = 1
webService init.....
status is:success, count =2, requestCount = 2
status is:success, count =3, requestCount = 3
webService init.....
status is:success, count =4, requestCount = 4