RMI

RMI
(Remote Method Invocation,远程方法调用)
支持存储于不同地址空间的程序级对象之间彼此进行通信,实现远程对象之间的无缝远程调用。其威力就体现在它强大的开发分布式网络应用的能力上。

一个正常工作的RMI系统由下面几个部分组成:

·远程服务的接口定义

·远程服务接口的具体实现

·桩(Stub)和框架(Skeleton)文件

·一个运行远程服务的服务器

·一个RMI命名服务,它允许客户端去发现这个远程服务

·类文件的提供者(一个HTTP或者FTP服务器)

·一个需要这个远程服务的客户端程序 

RMI原理图

 

JRMP:Java远程消息交换协议JRMP(Java Remote Messaging Protocol),JRMP是专为Java对象制定的。
IIOP:Internet Inter-ORB Protocol(互联网内部对象请求代理协议),它是一个用于CORBA 2.0及兼容平台上的协议。用来在CORBA对象请求代理之间交流的协议。Java中使得程序可以和其他语言的CORBA实现互操作性的协议。

方法调用从客户对象经占位程序(Stub)、远程引用层(Remote Reference Layer)和传输层(Transport Layer)向下,传递给主机,然后再次经传 输层,向上穿过远程调用层和骨干网(Skeleton),到达服务器对象。 占位程序扮演着远程服务器对象的代理的角色,使该对象可被客户激活。 远程引用层处理语义、管理单一或多重对象的通信,决定调用是应发往一个服务器还是多个。传输层管理实际的连接,并且追踪可以接受方法调用的远程对象。服务器端的骨干网完成对服务器对象实际的方法调用,并获取返回值。返回值向下经远程引用层、服务器端的传输层传递回客户端,再向上经传输层和远程调用层返回。最后,占位程序获得返回值。 

API包

java.rmi.Naming

java.rmi.activation

java.rmi.dgc

java.rmi.registry

java.rmi.server

示例程序

远程服务的接口定义 

package com.joyen.learning.javase.rmi;

import java.rmi.Remote;

public interface Calculator extends Remote {

    public long add(long a, long b) throws java.rmi.RemoteException;
    public long sub(long a, long b) throws java.rmi.RemoteException;
    public long mul(long a, long b) throws java.rmi.RemoteException;
    public long div(long a, long b) throws java.rmi.RemoteException;
}

远程服务接口的具体实现 

 1 package com.joyen.learning.javase.rmi;
 2 
 3 import java.rmi.RemoteException;
 4 import java.rmi.server.UnicastRemoteObject;
 5 
 6 public class CalculatorImpl extends UnicastRemoteObject implements Calculator {
 7 
 8 
 9     protected CalculatorImpl() throws RemoteException {
10         super();
11     }
12 
13     @Override
14     public long add(long a, long b) throws RemoteException {
15         return a + b;
16     }
17 
18     @Override
19     public long sub(long a, long b) throws RemoteException {
20         return a - b;
21     }
22 
23     @Override
24     public long mul(long a, long b) throws RemoteException {
25         return a * b;
26     }
27 
28     @Override
29     public long div(long a, long b) throws RemoteException {
30         return a / b;
31     }
32 
33 }

一个RMI命名服务,它允许客户端去发现这个远程服务 

 1 package com.joyen.learning.javase.rmi;
 2 
 3 import java.rmi.Naming;
 4 import java.rmi.registry.LocateRegistry;
 5 
 6 public class CalculatorServer {
 7 
 8     public CalculatorServer() {
 9         try {
10             Calculator c = new CalculatorImpl();
11             LocateRegistry.createRegistry(1099); 
12             Naming.bind("rmi://localhost:1099/CalculatorService", c);
13             System.out.println("INFO:远程Calculator对象绑定成功!"); 
14         } catch (Exception e) {
15             System.out.println("Trouble: " + e);
16         }
17     }
18 
19     public static void main(String args[]) {
20         new CalculatorServer();
21     }
22 }

一个需要这个远程服务的客户端程序 

 1 package com.joyen.learning.javase.rmi;
 2 
 3 import java.net.MalformedURLException;
 4 import java.rmi.Naming;
 5 import java.rmi.NotBoundException;
 6 import java.rmi.RemoteException;
 7 
 8 public class CalculatorClient {
 9 
10     public static void main(String[] args) {
11         try {
12             Calculator c = (Calculator) Naming
13                     .lookup("rmi://localhost:1099/CalculatorService");
14             System.out.println(c.sub(4, 3));
15             System.out.println(c.add(4, 5));
16             System.out.println(c.mul(3, 6));
17             System.out.println(c.div(9, 3));
18         } catch (MalformedURLException murle) {
19             System.out.println();
20             System.out.println("MalformedURLException");
21             System.out.println(murle);
22         } catch (RemoteException re) {
23             System.out.println();
24             System.out.println("RemoteException");
25             System.out.println(re);
26         } catch (NotBoundException nbe) {
27             System.out.println();
28             System.out.println("NotBoundException");
29             System.out.println(nbe);
30         } catch (java.lang.ArithmeticException ae) {
31             System.out.println();
32             System.out.println("java.lang.ArithmeticException");
33             System.out.println(ae);
34         }
35     }
36 }

RMI.pptx

参考:

http://www.blogjava.net/zhenyu33154/articles/320245.html

http://gongjiayun.iteye.com/blog/906159

http://blog.csdn.net/wangxingbao4227/article/details/6842951

原文地址:https://www.cnblogs.com/mingluosunshan/p/5391706.html