axis架构的web service开发
通过wsdd发布web service,在服务器端建立服务程序供web service client调用。
client调用server时,server类的生成原理:针对每个client 的 request,server类都会生成相应的server类实例对象。而不是singlton
项目:client和server中通讯String 类型数据。
1)服务器端配置文件 server-config.wsdd 内容如:
<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig"
xmlns:java=" http://xml.apache.org/axis/wsdd/providers/java "
xmlns:handler=" http://xml.apache.org/axis/wsdd/providers/handler "
xmlns=" http://xml.apache.org/axis/wsdd/ ">
<globalConfiguration name="defaultClientConfig">
<requestFlow name="RequestFlow1" type="">
<handler name="Handler1"
type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session" />
</handler>
<handler name="Handler2"
type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request" />
<parameter name="extension" value=".jwr" />
</handler>
</requestFlow>
</globalConfiguration>
<handler name="URLMapper"
type="java:org.apache.axis.handlers.http.URLMapper" />
<handler name="LocalResponder"
type="java:org.apache.axis.transport.local.LocalResponder" />
<handler name="Authenticate"
type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
<transport name="http" type="">
<requestFlow name="RequestFlow1" type="">
<handler name="Handler1" type="URLMapper" />
<handler name="Handler2"
type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
</requestFlow>
</transport>
<transport name="local" type="">
<responseFlow name="ResponseFlow1" type="">
<handler name="Handler1" type="LocalResponder" />
</responseFlow>
</transport>
<service name="AdminService" type="" provider="java:MSG">
<parameter name="allowedMethods" value="AdminService" />
<parameter name="enableRemoteAdmin" value="false" />
<parameter name="className" value="org.apache.axis.utils.Admin" />
<namespace>http://xml.apache.org/axis/wsdd/</namespace>
</service>
<service name="Version" type="" provider="java:RPC">
<parameter name="allowedMethods" value="getVersion" />
<parameter name="className" value="org.apache.axis.Version" />
</service>
<service name="MessageServiceImpl" provider="java:RPC">
<parameter name="className" value="com.unsap.dec.adapter.webservice.service.impl.MessageServiceImpl" />
<parameter name="allowedMethods" value="*" />
</service>
</deployment>
2)服务器端程序示例 如:
public class MessageServiceImpl implements MessageService {
static int i;
public MessageServiceImpl(){
System.out.println("==== SET JMSSERVICEIMPL===="+i);
i++;
}
public static JmsService jmsServiceImpl;
public String receiveMsg(String xmlText) {
try {
System.out.println("==Hello== /n" + xmlText);
jmsServiceImpl.SendMsg(xmlText);
} catch (Exception ex) {
ex.printStackTrace();
}
return "==发送===" + xmlText;
}
public void setJmsServiceImpl(JmsService jmsServiceImpl) {
System.out.println("=========== SET JMSSERVICEIMPL===================");
this.jmsServiceImpl = jmsServiceImpl;
}
}
当client调用类MessageServiceImpl 3次时,打印如下内容:
==== SET JMSSERVICEIMPL====1
==Hello==
someone---------anything
==== SET JMSSERVICEIMPL====2
==Hello==
someone---------anything
3)客户端开发示例 如:
public static void main(String[] args) {
try {
String endpoint = " http://localhost:8086/adapter/services/MessageServiceImpl ";
Service service = new Service();
Call call = null;
// 创建Call实例以便发送RPC请求
call = (Call) service.createCall();
call.setOperationName(new QName(endpoint, "receiveMsg"));
call.setTargetEndpointAddress(new java.net.URL(endpoint));
// invoke负责向Web服务器发送客户指定的RPC请求
String ret = (String) call.invoke(new Object[] { "someone---------anything" });
System.out.println(ret);
} catch (Exception ex) {
ex.printStackTrace();
}
}
在 RPC 中,客户机调用服务器上某个分布式对象的一个方法。在方法调用返回之前,该客户机被阻塞;该客户机在可以执行下一条指令之前,必须等待方法调用结束。