Java RMI 教程:远程方法调用解析

需积分: 0 3 下载量 62 浏览量 更新于2024-07-24 收藏 1.67MB PDF 举报
"Java RMI (Remote Method Invocation) 是一种机制,允许在不同的Java Virtual Machines (JVM) 上调用方法。这些JVM可以位于不同的机器上,也可以在同一台机器上,但被调用的方法在不同的地址空间运行。Java RMI是一种面向对象的远程过程调用机制,它与CORBA、SunRPC和DCERPC有显著区别。 与CORBA的对比: - CORBA是语言和机器独立的,而RMI专为在JVM上运行的Java设计。 - CORBA提供了更多功能,如服务器应用程序启动、管理持久状态和事务支持,而Java RMI则没有对象代理。 与SunRPC和DCERPC的差异: - RMI不是语言和机器独立的。 - RMI支持类中的方法概念以及类内的多态性。 使用RMI运行程序涉及三个实体: 1. 客户端:这是你编写的用于访问远程方法的程序。 2. 服务器:这是你编写的用于实现远程方法的程序。客户端连接到服务器并请求执行一个方法。对于客户端来说的远程方法,在服务器端是本地方法。 3. 对象注册表:这是一个服务,它使得客户端能够找到并连接到远程对象。 在Java RMI中,远程接口定义了客户端可以调用的方法。服务器实现这个接口,并注册到对象注册表中。客户端通过获取注册表中的引用来调用远程方法。RMI处理所有网络通信和对象序列化,使得在不同JVM间的通信变得简单。 为了实现RMI,你需要以下步骤: 1. 定义远程接口:接口中声明了可被远程调用的方法。 2. 实现远程接口:创建一个类实现远程接口,并使用`@Remote`注解标记该接口。 3. 创建远程对象:在服务器端实例化远程接口的实现,并将其注册到RMI注册表。 4. 获取远程对象:客户端通过RMI注册表查找并获取远程对象的引用。 5. 调用远程方法:客户端使用获取的引用调用远程方法。 Java RMI的优势在于其简洁性和与Java语言的紧密集成,使得开发者可以利用Java的全部特性进行分布式编程。然而,由于其不支持跨语言和平台,可能限制了与其他非Java系统的交互。 Java RMI提供了一种高效且易于使用的工具,用于构建分布式Java应用程序,特别是在同一Java生态系统内通信时。了解并熟练掌握RMI,可以帮助开发者实现更强大的系统架构,提高软件的可扩展性和灵活性。"
2009-09-14 上传
web报表工具 请移步:http://download.csdn.net/source/2881508 不多说,除了RMI的学习外,gui对新手入门也是个不错的学习 /* *此类适合在本地注册的RMI服务器,避免了使用了多个DOS的写法,只需要简单的给使用 *本类提供的方法即可。 */ package common.rmiserver; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.Remote; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; //import java.rmi.RMISecurityManager; import java.rmi.RemoteException; import java.net.MalformedURLException; import java.rmi.server.UnicastRemoteObject; import common.zip.ZipClientSocketFactory; import common.zip.ZipServerSocketFactory; public final class RMIServer { private Registry registry = null; // @jve:decl-index=0: private String serverName = "RMIServer"; private String serverPath = "localhost"; private int port = 1099; private Remote serverInterface = null; private Boolean isStart = false; private AllFace dataAllFace = null; private int dataPort = 0;// 数据端口,0表示端口由RMI服务器动态生成. private Remote stub; public RMIServer() { } /* * 使用默认端口,默认服务器名称,服务路径构造函数 use the default port,server name and the url */ public RMIServer(Remote obj) { this.serverInterface = obj; } /* * 使用默认端口,服务路径构造函数 use the default port and server url */ public RMIServer(String servername, Remote obj) { this.serverName = servername; this.serverInterface = obj; } /* * 服务器为默认值的构造函数 use the default url */ public RMIServer(String servername, int port, Remote obj) { this.port = port; this.serverName = servername; this.serverInterface = obj; } /* * 适合1.4范围的版本,当然也适合5.0的版本 */ public Boolean setStart() throws RemoteException, MalformedURLException, NotImplementInterfaceException { if (registry == null) registry = LocateRegistry.createRegistry(port); if (serverInterface == null) { throw new NotImplementInterfaceException( "not found the reote interface method!!"); } Naming.rebind("rmi://"+serverPath + ":" + port + "/" + serverName, serverInterface); isStart = true; return isStart; } /* * jdk5.0以后的写法,在使用之前使用setXxx方法给对象赋值 */ public Boolean start() throws RemoteException, MalformedURLException, NotImplementInterfaceException { if(stub==null) stub = (Remote) UnicastRemoteObject.exportObject(dataAllFace, dataPort);// 0表示端口随机生成. if (registry == null) registry = LocateRegistry.createRegistry(port, new ZipClientSocketFactory(), new ZipServerSocketFactory()); setProperty(serverPath); //绑定IP registry.rebind(serverName, stub); isStart = true; return isStart; } /* * 如果有多个ip,则使用这个方法绑定指定的IP */ public void setProperty(String ip) { System.setProperty("java.rmi.server.hostname", ip); } /* * close the server */ public void close() throws RemoteException, MalformedURLException, NotBoundException { //UnicastRemoteObject.unexportObject(dataAllFace, true); Naming.unbind("rmi://"+serverPath + ":" + port + "/" + serverName); isStart = false; } /* * set server name,if not set the name,the service will use the default name * "RMIServer" */ public void setServerName(String serverName) { this.serverName = serverName; } /* * set the server URL,default localhost "rmi://localhost" */ public void setServerPath(String serverPath) { this.serverPath = serverPath; } /* * set the server port default port is 1099. */ public void setPort(int port) { this.port = port; } /* * set then remote implement method */ public void setServerInterface(Remote serverInterface) { this.serverInterface = serverInterface; } /* * set the server Security */ public void setSecurityManager() { /* if (System.getSecurityManager() == null) { try { // java -Djava.security.policy=policy.txt // common.rmiserver.RMIServer System.setSecurityManager(new RMISecurityManager());// 暂时不要安全设置, } catch (java.rmi.RMISecurityException exc) { throw exc; } }*/ } /* * set the remote method */ public void setDataAllFace(AllFace dataAllFace) { this.dataAllFace = dataAllFace; } /* * set the remote dataport */ public void setDataPort(int dataPort) { this.dataPort = dataPort; } // ----------------------------------------------- /* * return the server URL */ public String getServerPatth() { return serverPath; } /* * return remote method */ public Remote getserverInterface() { return serverInterface; } /* * return the server name */ public String getServerName() { return serverName; } /* * return the server use port */ public int getPort() { return port; } /* * return the server then action state */ public Boolean isStart() { return isStart; } /* * return remote method */ public AllFace getDataAllFace() { return dataAllFace; } /* * return remote dataport */ public int getDataPort() { return dataPort; } }