码迷,mamicode.com
首页 > 其他好文 > 详细

RMI

时间:2016-04-14 17:31:46      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

·远程服务的接口定义

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

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

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

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

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

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

 

 技术分享

 

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 }

参考:

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

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

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

RMI

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!