java远程通讯技术及简单实现

在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java底层领域中有很多可实现远程通讯的技术,例如:RMI、MINA、ESB、Burlap、SOAP、EJB和JMS等,在j2ee中,对java底层远程通讯的技术进行了封装,形成了 Hessian 、 HttpInvoker 、 XFire 、Axis 等多种形式的远程调用技术。但对高级程序员而言仍需要掌握Java底层领域中远程通讯的技术,尤其是rmi,xml-rpc,JMS。

1.远程服务基本原理
1)底层协议
  要实现网络机器间的通讯,首先得来看看计算机系统网络通信的基本原理,在底层层面去看,网络通信需要做的就是将流从一台计算机传输到另外一台计算机,基于传输协议和网络IO来实现,其中传输协议比较出名的有http、tcp、udp等等,http、tcp、udp都是在基于Socket概念上为某类应用场景而扩展出的传输协议,网络IO,主要有bio、nio、aio三种方式,所有的分布式应用通讯都基于这个原理而实现,只是为了

应用的易用,各种语言通常都会提供一些更为贴近应用易用的应用层协议。
2)应用级协议
  远程服务通讯,需要达到的目标是在一台计算机发起请求,另外一台机器在接收到请求后进行相应的处理并将结果返回给请求端,这其中又会有诸如onewayrequest、同步请求、异步请求等等请求方式,按照网络通信原理,需要实现这个需要做的就是将请求转换成流,通过传输协议传输至远端,远端计算机在接收到请求的流后进行处理,处理完毕后将结果转化为流,并通过传输协议返回给调用端。
<wbr><wbr>在java领域中知名的远程通信的应用级协议有:RMI、XML-RPC、Binary-RPC、SOAP、JMS<br>   <br>   <br><span style="font-size:16px"><strong>2.RMI</strong></span><br><strong>2.1RMI原理</strong><br><wbr><wbr><wbr> RMI,即JavaRMI(Java Remote MethodInvocation),Java远程方法调用.是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口。它使客户机上运行的程序可以调用远程服务器上的对象。远程方法调用特性使Java编程人员能够在网络环境中分布操作。<br>   RMI是个典型的为java定制的远程通信协议,RMI全部的宗旨就是尽可能简化远程接口对象的使用。<br><wbr><wbr><wbr>RMI的基础是接口,RMI构架基于一个重要的原理:定义接口和定义接口的具体实现是分开的。<br>   来看下基于RMI的一次完整的远程通信过程的原理:<br>   1)客户端发起请求,请求转交至RMI客户端的stub类;<br>   2)stub类将请求的接口、方法、参数等信息进行序列化;<br>   3)基于socket将序列化后的流传输至服务器端;<br>   4)服务器端接收到流后转发至相应的skelton类;<br>   5)skelton类将请求的信息反序列化后调用实际的处理类;<br>   6)处理类处理完毕后将结果返回给skelton类;<br>   7)Skelton类将结果序列化,通过socket将流传送给客户端的stub;<br>   8)stub在接收到流后反序列化,将反序列化后的Java Object返回给调用者。<br><strong>2.2JAVA对RMI的支持</strong><br><wbr><wbr><wbr>java.rmi是JAVA提供 RMI 包。RMI是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java虚拟机中的对象上的方法。可以用此方法调用的任何对象必须实现该远程接口。调用这样一个对象时,其参数为 "marshalled"并将其从本地虚拟机发送到远程虚拟机(该远程虚拟机的参数为"unmarshalled")上。该方法终止时,将编组来自远程机的结果并将结果发送到调用方的虚拟机。如果方法调用导致抛出异常</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

,则该异常将指示给调用方.
<wbr><wbr><wbr> Remote接口用于标识其方法可以从非本地虚拟机上调用的接口。<br><strong>2.3rmi在java中的应用</strong><br><wbr><wbr><wbr>要使用RMI,必须构建四个主要的类:远程对象的本地接口、远程对象实现、RMI客户机和RMI服务器。RMI服务器生成远程对象实现的一个实例,并用一个专有的URL注册。RMI客户机在远程RMI服务器上查找服务对象,并将它转换成本地接口类型,然后像对待一个本地对象一样使用</wbr></wbr></wbr></wbr></wbr></wbr>

它。
 1)远程对象的本地接口
package test.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloRMI extends Remote{<wbr><wbr><br><wbr><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> publicString helloWorld() throwsRemoteException;<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> publicString sayHelloToSomeBody(String someBodyName) throwsRemoteException;<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><br> }<br> 2)远程对象实现<br> package test.rmi;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

importjava.rmi.RemoteException;<wbr><wbr><br> importjava.rmi.server.UnicastRemoteObject;<wbr><wbr><br><wbr><br><wbr><br> public class HelloRMIImpl extends UnicastRemoteObject implementsHelloRMI {<wbr><wbr><br><wbr><wbr><wbr> privatestatic final long serialVersionUID =-5464145481720553926L;<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> publicHelloRMIImpl() throws RemoteException{<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> publicString helloWorld() throws RemoteException{<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return "Hello World!";<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> publicString sayHelloToSomeBody(String someBodyName) throwsRemoteException {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return "你好," + someBodyName +"!";<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br> }<wbr><br> 3)RMI服务器<br> package test.rmi;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

importjava.net.MalformedURLException;<wbr><wbr><br> importjava.rmi.AlreadyBoundException;<wbr><wbr><br> import java.rmi.Naming;<wbr><wbr><br> importjava.rmi.RemoteException;<wbr><wbr><br> importjava.rmi.registry.LocateRegistry;<wbr><wbr><br><wbr><br><wbr><br> public class HelloRMIServer{<wbr><wbr><br><wbr><wbr><wbr> publicstatic void main(String args[]){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//创建一个远程对象<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>HelloRMI rhello = newHelloRMIImpl();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//本地主机上的远程对象注册表Registry的实例,并指定端口为8888,这一步必不可少(Java默认端口是1099),必不可缺的一</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

步,缺少注册表创建,则无法绑定对象到远程注册表上<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>LocateRegistry.createRegistry(8888);<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//把远程对象注册到RMI注册服务器上,并命名为RHello<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//绑定的URL标准格式为:rmi://host:port/name(其中协议名可以省略,下面两种写法都是正确的)<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Naming.bind("rmi://localhost:8888/RHello",rhello);<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//Naming.bind("//localhost:8888/RHello",rhello);<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("&gt;&gt;&gt;&gt;&gt;INFO:远程IHello对象绑定成功!");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (RemoteException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("创建远程对象发生异常!");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (AlreadyBoundException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("发生重复绑定对象异常!");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (MalformedURLException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("发生URL畸形异常!");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br> }<wbr><br> 4)RMI客户机<br> package test.rmi;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

importjava.net.MalformedURLException;<wbr><wbr><br> import java.rmi.Naming;<wbr><wbr><br> importjava.rmi.NotBoundException;<wbr><wbr><br> importjava.rmi.RemoteException;<wbr><wbr><br><wbr><br><wbr><br> public class HelloRMIClient{<wbr><wbr><br><wbr><wbr><wbr> publicstatic void main(Stringargs[]){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//在RMI服务注册表中查找名称为RHello的对象,并调用其上的方法<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>HelloRMI rhello =(HelloRMI)Naming.lookup("rmi://localhost:8888/RHello");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(rhello.helloWorld());<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(rhello.sayHelloToSomeBody("staratsky"));<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (NotBoundException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (MalformedURLException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (RemoteException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>总结:从上面的过程来看,RMI对服务器的IP地址和端口依赖很紧密,但是在开发的时候不知道将来的服务器IP和端口如何,但是客户端程序</wbr>

依赖这个IP和端口。
<wbr><wbr><wbr>这也是RMI的局限性之一。这个问题有两种解决途径:一是通过DNS来解决,二是通过封装将IP暴露到程序代码之外。<br><wbr><wbr><wbr>RMI的局限性之二是RMI是Java语言的远程调用,两端的程序语言必须是Java实现,对于不同语言间的通讯可以考虑用WebService来实现。</wbr></wbr></wbr></wbr></wbr></wbr>


3.XML-RPC
3.1原理
<wbr><wbr> XML-RPC网站是这样描述的:<br><wbr><wbr> 它是允许运行在不同操作系统、不同环境中的软件进行基于Internet 过程调用的规范和一组实现。这种远程过程调用使用 HTTP 作为传输协议,XML 作为编码格式。XML-RPC的定义尽可能简单,但能够传送、处理和返回复杂的数据结构。<br><wbr><wbr><wbr>XML-RPC消息都是HTTP-POST请求。请求的主要部分的XML。服务器端执行后的返回结果同样也是XML格式。函数调用的参数可以是scalars,numbers, strings, dates等等;也可以是混合型的记录和结构体。<br><wbr><wbr><wbr>所以,要完成XML-RPC,需要完成3部分工作:<br> 1)接口实现<br><wbr>package test.xmlrpc;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

public class HelloHandler implements ServicesHandler {
<wbr><wbr><wbr> publicString execute(String str){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return "Hello," + str + "!";<br><wbr><wbr><wbr> }<br> }<br> 2)远程调用<br> package test.xmlrpc;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Vector;
import java.net.URL;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class TestClient {
<wbr><wbr><wbr><wbr><wbr>public static void main(String [] args) throws Exception {<br><wbr><wbr><wbr><wbr><wbr><wbr>try {</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// config client<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>config.setServerURL(new URL("<a href="http://localhost:8080/yizhu/HelloHandler">http://localhost:8080/yizhu/HelloHandler</a>"));<wbr><wbr><wbr><wbr><wbr>// should be modified according</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

to your configuration of jsp container
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// create a new XmlRpcClient object and bind above config objectwith it<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>XmlRpcClient client = new XmlRpcClient();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>client.setConfig(config);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// create parameter list<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Vector&lt;String&gt; params = newVector&lt;String&gt;();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>params.addElement("MY");<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// execute XML-RPC call<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>String result = (String) client.execute("HelloHandler.execute",params);<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(result);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (MalformedURLException e) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (XmlRpcException e) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (IOException e) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr> }<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

3)建立一个web服务器
package test.xmlrpc;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.server.*;
import org.apache.xmlrpc.webserver.*;

public class XmlRpcServicesServlet extends HttpServlet {
<wbr><br><wbr>private XmlRpcServletServer server;</wbr></wbr>

<wbr><wbr><wbr> publicvoid init(ServletConfig pConfig) throws ServletException {</wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>super.init(pConfig);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// create a new XmlRpcServletServer object<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>server = new XmlRpcServletServer();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// set up handler mapping of XmlRpcServletServer object<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>PropertyHandlerMapping phm = new PropertyHandlerMapping();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>phm.addHandler("HelloHandler",HelloHandler.class);<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>server.setHandlerMapping(phm);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// more config of XmlRpcServletServerobject<wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>XmlRpcServerConfigImpl serverConfig =(XmlRpcServerConfigImpl)server.getConfig();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>serverConfig.setEnabledForExtensions(true);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>serverConfig.setContentLengthOptional<wbr>(false);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (XmlRpcException e) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>log("Failed to create XmlRpcServer: " + e.getMessage(), e);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (Throwable ignore) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>throw new ServletException(e);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><br><wbr><wbr><wbr> public voiddoPost(HttpServletRequest Request, HttpServletResponseResponse)</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>throws IOException, ServletException {</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>server.execute(Request, Response);<br><wbr><wbr><wbr> }<br><wbr><wbr><wbr><br><wbr><wbr><wbr> public voiddoGet(HttpServletRequest Request, HttpServletResponse Response)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr> throwsIOException, ServletException {</wbr></wbr></wbr>

<wbr><wbr><wbr>server.execute(Request, Response);<br><wbr><wbr><wbr> }<br> }<br> 在web.xml中的配置<br> &lt;servlet&gt;<br><wbr><wbr><wbr>&lt;servlet-name&gt;XmlRpcServer&lt;/servlet-name&gt;<br><wbr><wbr><wbr>&lt;servlet-class&gt;test.xmlrpc.XmlRpcServicesServlet&lt;/servlet-class&gt;<br><wbr>&lt;/servlet&gt;<br><wbr>&lt;servlet-mapping&gt;<br><wbr><wbr>&lt;servlet-name&gt;XmlRpcServer&lt;/servlet-name&gt;<br><wbr><wbr>&lt;url-pattern&gt;/HelloHandler&lt;/url-pattern&gt;<br><wbr>&lt;/servlet-mapping&gt;<br><strong>3.2RMI和RPC的区别</strong><br>   XML-RPC也是一种和RMI类似的远程调用的协议,它和RMI的不同之处在于它以标准的xml格式来定义请求的信息(请求的对象、方法、参数等).所以,在RMI和RPC之间最主要的区别在于方法是如何别调用的。<br><wbr>在RMI中,远程接口使每个远程方法都具有方法签名。如果一个方法在服务器上执行,但是没有相匹配的签名被添加到这个远程接口上,那么这个新方法就不能被RMI客户方所调用。在RPC中,当一个请求到达RPC服务器时,这个请求就包含了一个参数集和一个文本值,通常形成“classname.methodname”的形式。这就向RPC服务器表明,被请求的方法在为“classname”的类中,名叫“methodname”。然后RPC服务器就去搜索与之相匹配的类和方法,并把它作为那种方法参数类型的输入。这里的参数类型是与RPC请求中的类型是匹配的。一旦匹配成功,这个方法就被调用了,其结果被编码后返回客户方。<br><strong>3.3xml-rpc的缺点</strong><br> 1)XML-RPC的消息系统过于简单,没有完整意义上的消息模型<br> 2)XML-RPC调用服务的方式要求直接指定对象和方法,称不上完整的面向服务的体系<br> 3)XML-RPC服务器端提供的服务实际上是特定对象的某个方法,限制了服务器端的开发</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

  
4.Binary-RPC
  Binary-RPC看名字就知道和XML-RPC是差不多的了,不同之处仅在于传输的标准格式由XML转为了二进制的格式。
<wbr><wbr><wbr>Hessian是由caucho提供的一个基于binary-RPC实现的远程通讯library。<br> 1)写一个接口:</wbr></wbr></wbr>

package test.hassian;
public interface SayHello{<wbr><wbr><br><wbr><wbr><wbr> publicString sayHello(Stringname);<wbr><wbr><br><wbr><wbr><wbr> publicStudent getStudent();<wbr><wbr><br><wbr><br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

2)编写一个实现:
package test.hassian;
importcom.caucho.hessian.server.HessianServlet;<wbr><wbr><br> public class SayHelloImpl extends HessianServlet implementsSayHello {<wbr><wbr><br><wbr><wbr><wbr><wbr>public String sayHello(String name){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return "hello "+name;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr> publicStudent getStudent(){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Student<wbr> s=newStudent();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>s.setName("staratsky");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>s.setSchool("ustc");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>returns;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><br> }<br> 3)bean类<br> package test.hassian;<br> importjava.io.Serializable;<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

public class Student implements Serializable{

<wbr><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> privatestatic final long serialVersionUID =-9006571629757493042L;<wbr><wbr><br><wbr><wbr><wbr> privateString name;<wbr><wbr><br><wbr><wbr><wbr> privateString school;<wbr><wbr><br><wbr><wbr><wbr> publicString getName() {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return name;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr> public voidsetName(String name) {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>this.name = name;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr> publicString getSchool() {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return school;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr> public voidsetSchool(String school){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>this.school = school;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr> publicString toString() {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return name+"<wbr>"+school;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br> }<wbr><br> 4)远程调用类<br> package test.hassian;<br> importjava.net.MalformedURLException;<wbr><wbr><br> importcom.caucho.hessian.client.HessianProxyFactory;<wbr><wbr><br> public class HessianClientTest{<wbr><wbr><br><wbr><wbr><wbr> publicstatic void main(String[] args){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>String url="<a href="http://127.0.0.1:8080/yizhu/hassiantest">http://127.0.0.1:8080/yizhu/hassiantest</a>";<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>HessianProxyFactory factory=newHessianProxyFactory();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>SayHello say=(SayHello) factory.create(SayHello.class,url);<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(say.sayHello("abc"));<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(say.getStudent());<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (MalformedURLException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr>}<wbr><br> 服务器端的配置<br> 在web.xml中,加入下面两段代码:<br><wbr>&lt;servlet&gt;<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;servlet-name&gt;hassiantest&lt;/servlet-name&gt;<wbr><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java底层领域中有很多可实现远程通讯的技术,例如:RMI、MINA、ESB、Burlap、SOAP、EJB和JMS等,在j2ee中,对java底层远程通讯的技术进行了封装,形成了 Hessian 、 HttpInvoker 、 XFire 、Axis 等多种形式的远程调用技术。但对高级程序员而言仍需要掌握Java底层领域中远程通讯的技术,尤其是rmi,xml-rpc,JMS。

1.远程服务基本原理
1)底层协议
  要实现网络机器间的通讯,首先得来看看计算机系统网络通信的基本原理,在底层层面去看,网络通信需要做的就是将流从一台计算机传输到另外一台计算机,基于传输协议和网络IO来实现,其中传输协议比较出名的有http、tcp、udp等等,http、tcp、udp都是在基于Socket概念上为某类应用场景而扩展出的传输协议,网络IO,主要有bio、nio、aio三种方式,所有的分布式应用通讯都基于这个原理而实现,只是为了

应用的易用,各种语言通常都会提供一些更为贴近应用易用的应用层协议。
2)应用级协议
  远程服务通讯,需要达到的目标是在一台计算机发起请求,另外一台机器在接收到请求后进行相应的处理并将结果返回给请求端,这其中又会有诸如onewayrequest、同步请求、异步请求等等请求方式,按照网络通信原理,需要实现这个需要做的就是将请求转换成流,通过传输协议传输至远端,远端计算机在接收到请求的流后进行处理,处理完毕后将结果转化为流,并通过传输协议返回给调用端。
<wbr><wbr>在java领域中知名的远程通信的应用级协议有:RMI、XML-RPC、Binary-RPC、SOAP、JMS<br>   <br>   <br><span style="font-size:16px"><strong>2.RMI</strong></span><br><strong>2.1RMI原理</strong><br><wbr><wbr><wbr> RMI,即JavaRMI(Java Remote MethodInvocation),Java远程方法调用.是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口。它使客户机上运行的程序可以调用远程服务器上的对象。远程方法调用特性使Java编程人员能够在网络环境中分布操作。<br>   RMI是个典型的为java定制的远程通信协议,RMI全部的宗旨就是尽可能简化远程接口对象的使用。<br><wbr><wbr><wbr>RMI的基础是接口,RMI构架基于一个重要的原理:定义接口和定义接口的具体实现是分开的。<br>   来看下基于RMI的一次完整的远程通信过程的原理:<br>   1)客户端发起请求,请求转交至RMI客户端的stub类;<br>   2)stub类将请求的接口、方法、参数等信息进行序列化;<br>   3)基于socket将序列化后的流传输至服务器端;<br>   4)服务器端接收到流后转发至相应的skelton类;<br>   5)skelton类将请求的信息反序列化后调用实际的处理类;<br>   6)处理类处理完毕后将结果返回给skelton类;<br>   7)Skelton类将结果序列化,通过socket将流传送给客户端的stub;<br>   8)stub在接收到流后反序列化,将反序列化后的Java Object返回给调用者。<br><strong>2.2JAVA对RMI的支持</strong><br><wbr><wbr><wbr>java.rmi是JAVA提供 RMI 包。RMI是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java虚拟机中的对象上的方法。可以用此方法调用的任何对象必须实现该远程接口。调用这样一个对象时,其参数为 "marshalled"并将其从本地虚拟机发送到远程虚拟机(该远程虚拟机的参数为"unmarshalled")上。该方法终止时,将编组来自远程机的结果并将结果发送到调用方的虚拟机。如果方法调用导致抛出异常</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

,则该异常将指示给调用方.
<wbr><wbr><wbr> Remote接口用于标识其方法可以从非本地虚拟机上调用的接口。<br><strong>2.3rmi在java中的应用</strong><br><wbr><wbr><wbr>要使用RMI,必须构建四个主要的类:远程对象的本地接口、远程对象实现、RMI客户机和RMI服务器。RMI服务器生成远程对象实现的一个实例,并用一个专有的URL注册。RMI客户机在远程RMI服务器上查找服务对象,并将它转换成本地接口类型,然后像对待一个本地对象一样使用</wbr></wbr></wbr></wbr></wbr></wbr>

它。
 1)远程对象的本地接口
package test.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloRMI extends Remote{<wbr><wbr><br><wbr><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> publicString helloWorld() throwsRemoteException;<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> publicString sayHelloToSomeBody(String someBodyName) throwsRemoteException;<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><br> }<br> 2)远程对象实现<br> package test.rmi;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

importjava.rmi.RemoteException;<wbr><wbr><br> importjava.rmi.server.UnicastRemoteObject;<wbr><wbr><br><wbr><br><wbr><br> public class HelloRMIImpl extends UnicastRemoteObject implementsHelloRMI {<wbr><wbr><br><wbr><wbr><wbr> privatestatic final long serialVersionUID =-5464145481720553926L;<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> publicHelloRMIImpl() throws RemoteException{<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> publicString helloWorld() throws RemoteException{<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return "Hello World!";<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> publicString sayHelloToSomeBody(String someBodyName) throwsRemoteException {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return "你好," + someBodyName +"!";<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br> }<wbr><br> 3)RMI服务器<br> package test.rmi;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

importjava.net.MalformedURLException;<wbr><wbr><br> importjava.rmi.AlreadyBoundException;<wbr><wbr><br> import java.rmi.Naming;<wbr><wbr><br> importjava.rmi.RemoteException;<wbr><wbr><br> importjava.rmi.registry.LocateRegistry;<wbr><wbr><br><wbr><br><wbr><br> public class HelloRMIServer{<wbr><wbr><br><wbr><wbr><wbr> publicstatic void main(String args[]){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//创建一个远程对象<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>HelloRMI rhello = newHelloRMIImpl();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//本地主机上的远程对象注册表Registry的实例,并指定端口为8888,这一步必不可少(Java默认端口是1099),必不可缺的一</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

步,缺少注册表创建,则无法绑定对象到远程注册表上<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>LocateRegistry.createRegistry(8888);<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//把远程对象注册到RMI注册服务器上,并命名为RHello<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//绑定的URL标准格式为:rmi://host:port/name(其中协议名可以省略,下面两种写法都是正确的)<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Naming.bind("rmi://localhost:8888/RHello",rhello);<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//Naming.bind("//localhost:8888/RHello",rhello);<wbr><wbr><br><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("&gt;&gt;&gt;&gt;&gt;INFO:远程IHello对象绑定成功!");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (RemoteException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("创建远程对象发生异常!");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (AlreadyBoundException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("发生重复绑定对象异常!");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (MalformedURLException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println("发生URL畸形异常!");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br> }<wbr><br> 4)RMI客户机<br> package test.rmi;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

importjava.net.MalformedURLException;<wbr><wbr><br> import java.rmi.Naming;<wbr><wbr><br> importjava.rmi.NotBoundException;<wbr><wbr><br> importjava.rmi.RemoteException;<wbr><wbr><br><wbr><br><wbr><br> public class HelloRMIClient{<wbr><wbr><br><wbr><wbr><wbr> publicstatic void main(Stringargs[]){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//在RMI服务注册表中查找名称为RHello的对象,并调用其上的方法<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>HelloRMI rhello =(HelloRMI)Naming.lookup("rmi://localhost:8888/RHello");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(rhello.helloWorld());<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(rhello.sayHelloToSomeBody("staratsky"));<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (NotBoundException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (MalformedURLException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (RemoteException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>总结:从上面的过程来看,RMI对服务器的IP地址和端口依赖很紧密,但是在开发的时候不知道将来的服务器IP和端口如何,但是客户端程序</wbr>

依赖这个IP和端口。
<wbr><wbr><wbr>这也是RMI的局限性之一。这个问题有两种解决途径:一是通过DNS来解决,二是通过封装将IP暴露到程序代码之外。<br><wbr><wbr><wbr>RMI的局限性之二是RMI是Java语言的远程调用,两端的程序语言必须是Java实现,对于不同语言间的通讯可以考虑用WebService来实现。</wbr></wbr></wbr></wbr></wbr></wbr>


3.XML-RPC
3.1原理
<wbr><wbr> XML-RPC网站是这样描述的:<br><wbr><wbr> 它是允许运行在不同操作系统、不同环境中的软件进行基于Internet 过程调用的规范和一组实现。这种远程过程调用使用 HTTP 作为传输协议,XML 作为编码格式。XML-RPC的定义尽可能简单,但能够传送、处理和返回复杂的数据结构。<br><wbr><wbr><wbr>XML-RPC消息都是HTTP-POST请求。请求的主要部分的XML。服务器端执行后的返回结果同样也是XML格式。函数调用的参数可以是scalars,numbers, strings, dates等等;也可以是混合型的记录和结构体。<br><wbr><wbr><wbr>所以,要完成XML-RPC,需要完成3部分工作:<br> 1)接口实现<br><wbr>package test.xmlrpc;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

public class HelloHandler implements ServicesHandler {
<wbr><wbr><wbr> publicString execute(String str){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return "Hello," + str + "!";<br><wbr><wbr><wbr> }<br> }<br> 2)远程调用<br> package test.xmlrpc;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Vector;
import java.net.URL;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class TestClient {
<wbr><wbr><wbr><wbr><wbr>public static void main(String [] args) throws Exception {<br><wbr><wbr><wbr><wbr><wbr><wbr>try {</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// config client<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>config.setServerURL(new URL("<a href="http://localhost:8080/yizhu/HelloHandler">http://localhost:8080/yizhu/HelloHandler</a>"));<wbr><wbr><wbr><wbr><wbr>// should be modified according</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

to your configuration of jsp container
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// create a new XmlRpcClient object and bind above config objectwith it<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>XmlRpcClient client = new XmlRpcClient();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>client.setConfig(config);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// create parameter list<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Vector&lt;String&gt; params = newVector&lt;String&gt;();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>params.addElement("MY");<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// execute XML-RPC call<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>String result = (String) client.execute("HelloHandler.execute",params);<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(result);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (MalformedURLException e) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (XmlRpcException e) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (IOException e) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr> }<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

3)建立一个web服务器
package test.xmlrpc;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.server.*;
import org.apache.xmlrpc.webserver.*;

public class XmlRpcServicesServlet extends HttpServlet {
<wbr><br><wbr>private XmlRpcServletServer server;</wbr></wbr>

<wbr><wbr><wbr> publicvoid init(ServletConfig pConfig) throws ServletException {</wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>super.init(pConfig);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// create a new XmlRpcServletServer object<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>server = new XmlRpcServletServer();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// set up handler mapping of XmlRpcServletServer object<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>PropertyHandlerMapping phm = new PropertyHandlerMapping();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>phm.addHandler("HelloHandler",HelloHandler.class);<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>server.setHandlerMapping(phm);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// more config of XmlRpcServletServerobject<wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>XmlRpcServerConfigImpl serverConfig =(XmlRpcServerConfigImpl)server.getConfig();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>serverConfig.setEnabledForExtensions(true);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>serverConfig.setContentLengthOptional<wbr>(false);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (XmlRpcException e) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>log("Failed to create XmlRpcServer: " + e.getMessage(), e);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (Throwable ignore) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>throw new ServletException(e);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><br><wbr><wbr><wbr> public voiddoPost(HttpServletRequest Request, HttpServletResponseResponse)</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>throws IOException, ServletException {</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>server.execute(Request, Response);<br><wbr><wbr><wbr> }<br><wbr><wbr><wbr><br><wbr><wbr><wbr> public voiddoGet(HttpServletRequest Request, HttpServletResponse Response)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr> throwsIOException, ServletException {</wbr></wbr></wbr>

<wbr><wbr><wbr>server.execute(Request, Response);<br><wbr><wbr><wbr> }<br> }<br> 在web.xml中的配置<br> &lt;servlet&gt;<br><wbr><wbr><wbr>&lt;servlet-name&gt;XmlRpcServer&lt;/servlet-name&gt;<br><wbr><wbr><wbr>&lt;servlet-class&gt;test.xmlrpc.XmlRpcServicesServlet&lt;/servlet-class&gt;<br><wbr>&lt;/servlet&gt;<br><wbr>&lt;servlet-mapping&gt;<br><wbr><wbr>&lt;servlet-name&gt;XmlRpcServer&lt;/servlet-name&gt;<br><wbr><wbr>&lt;url-pattern&gt;/HelloHandler&lt;/url-pattern&gt;<br><wbr>&lt;/servlet-mapping&gt;<br><strong>3.2RMI和RPC的区别</strong><br>   XML-RPC也是一种和RMI类似的远程调用的协议,它和RMI的不同之处在于它以标准的xml格式来定义请求的信息(请求的对象、方法、参数等).所以,在RMI和RPC之间最主要的区别在于方法是如何别调用的。<br><wbr>在RMI中,远程接口使每个远程方法都具有方法签名。如果一个方法在服务器上执行,但是没有相匹配的签名被添加到这个远程接口上,那么这个新方法就不能被RMI客户方所调用。在RPC中,当一个请求到达RPC服务器时,这个请求就包含了一个参数集和一个文本值,通常形成“classname.methodname”的形式。这就向RPC服务器表明,被请求的方法在为“classname”的类中,名叫“methodname”。然后RPC服务器就去搜索与之相匹配的类和方法,并把它作为那种方法参数类型的输入。这里的参数类型是与RPC请求中的类型是匹配的。一旦匹配成功,这个方法就被调用了,其结果被编码后返回客户方。<br><strong>3.3xml-rpc的缺点</strong><br> 1)XML-RPC的消息系统过于简单,没有完整意义上的消息模型<br> 2)XML-RPC调用服务的方式要求直接指定对象和方法,称不上完整的面向服务的体系<br> 3)XML-RPC服务器端提供的服务实际上是特定对象的某个方法,限制了服务器端的开发</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

  
4.Binary-RPC
  Binary-RPC看名字就知道和XML-RPC是差不多的了,不同之处仅在于传输的标准格式由XML转为了二进制的格式。
<wbr><wbr><wbr>Hessian是由caucho提供的一个基于binary-RPC实现的远程通讯library。<br> 1)写一个接口:</wbr></wbr></wbr>

package test.hassian;
public interface SayHello{<wbr><wbr><br><wbr><wbr><wbr> publicString sayHello(Stringname);<wbr><wbr><br><wbr><wbr><wbr> publicStudent getStudent();<wbr><wbr><br><wbr><br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

2)编写一个实现:
package test.hassian;
importcom.caucho.hessian.server.HessianServlet;<wbr><wbr><br> public class SayHelloImpl extends HessianServlet implementsSayHello {<wbr><wbr><br><wbr><wbr><wbr><wbr>public String sayHello(String name){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return "hello "+name;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr> publicStudent getStudent(){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Student<wbr> s=newStudent();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>s.setName("staratsky");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>s.setSchool("ustc");<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>returns;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><br> }<br> 3)bean类<br> package test.hassian;<br> importjava.io.Serializable;<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

public class Student implements Serializable{

<wbr><wbr><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr> privatestatic final long serialVersionUID =-9006571629757493042L;<wbr><wbr><br><wbr><wbr><wbr> privateString name;<wbr><wbr><br><wbr><wbr><wbr> privateString school;<wbr><wbr><br><wbr><wbr><wbr> publicString getName() {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return name;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr> public voidsetName(String name) {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>this.name = name;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr> publicString getSchool() {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return school;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr> public voidsetSchool(String school){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>this.school = school;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr> publicString toString() {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return name+"<wbr>"+school;<wbr><wbr><br><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br> }<wbr><br> 4)远程调用类<br> package test.hassian;<br> importjava.net.MalformedURLException;<wbr><wbr><br> importcom.caucho.hessian.client.HessianProxyFactory;<wbr><wbr><br> public class HessianClientTest{<wbr><wbr><br><wbr><wbr><wbr> publicstatic void main(String[] args){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>String url="<a href="http://127.0.0.1:8080/yizhu/hassiantest">http://127.0.0.1:8080/yizhu/hassiantest</a>";<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>HessianProxyFactory factory=newHessianProxyFactory();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>try {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>SayHello say=(SayHello) factory.create(SayHello.class,url);<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(say.sayHello("abc"));<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.out.println(say.getStudent());<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>} catch (MalformedURLException e){<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr><wbr><br><wbr><wbr><wbr>}<wbr><br> 服务器端的配置<br> 在web.xml中,加入下面两段代码:<br><wbr>&lt;servlet&gt;<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;servlet-name&gt;hassiantest&lt;/servlet-name&gt;<wbr><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(java)