RMI实例讲解

RMI实例讲解

RMI是Remote Method Invocation的缩写,是实现java分布式应用的一个框架。远程方法调用是一种计算机之间对象互相调用对方函数,启动对方进程的一种机制,使用这种机制,某一台计算机上的对象在调用另外一台计算机上的方法时,使用的程序语法规则和在本地机上对象间的方法调用的语法规则一样。本文将使用一个实例来介绍RMI的具体使用。

RMI(Remote Method Invocation)
RMI是分布式对象软件包,它简化了在多台计算机上的JAVA应用之间的通信。
必须在jdk1.1以上
RMI用到的类
java.rmi.Remote
所有可以被远程调用的对象都必须实现该接口
java.rmi.server.UnicastRemoteObject
所有可以被远程调用的对象都必须扩展该类


他需要四个文件,通过以下步骤,将能完成这个例子。
第一步,需要声明一个extends Remote的接口;
第二步,实现这个接口,并且加上默认构造函数,在函数体内,只要有一句super();即可
第三步,编写一个服务器端的绑定类
第四步,编写一个客户端的应用
(以下几步均需要独立的DOS界面)
第五步,在编译出来的实现接口类的目录下,使用rmic生成stub 和 skeleton
第六步,在编译出来的出来的类的目录下,运行rmiregistry命令(作者估计这个命令是维护naming映射用的)
第七步,运行server类
第八步,运行client类

具体代码如下:
1.接口
/*
 * Created on 2005-8-22
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.blankboard.rmi;

import java.rmi.*;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public interface Calculator extends Remote {

 public long add(long a, long b) throws RemoteException;

 public long sub(long a, long b) throws RemoteException;

 public long mul(long a, long b) throws RemoteException;

 public long div(long a, long b) throws RemoteException;

}

2.接口实现类
/*
 * Created on 2005-8-22
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.blankboard.rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CalculatorImpl extends UnicastRemoteObject implements Calculator {

 public CalculatorImpl() throws java.rmi.RemoteException {
  super();
 }
 
 
 /* (non-Javadoc)
  * @see com.blankboard.rmi.Calculator#add(long, long)
  */
 public long add(long a, long b) throws RemoteException {
  // TODO Auto-generated method stub
  return a+b;
 }

 /* (non-Javadoc)
  * @see com.blankboard.rmi.Calculator#sub(long, long)
  */
 public long sub(long a, long b) throws RemoteException {
  // TODO Auto-generated method stub
  return a-b;
 }

 /* (non-Javadoc)
  * @see com.blankboard.rmi.Calculator#mul(long, long)
  */
 public long mul(long a, long b) throws RemoteException {
  // TODO Auto-generated method stub
  return a*b;
 }

 /* (non-Javadoc)
  * @see com.blankboard.rmi.Calculator#div(long, long)
  */
 public long div(long a, long b) throws RemoteException {
  // TODO Auto-generated method stub
  return a/b;
 }

}


3.服务端程序
/*
 * Created on 2005-8-22
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.blankboard.rmi;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.rmi.Naming;

public class CalculatorServer {
 public CalculatorServer() {
     try {
       Calculator c = new CalculatorImpl();
       Naming.rebind("rmi://localhost:1099/CalculatorService", c);
     } catch (Exception e) {
       System.out.println("Trouble: " + e);
     }
   }
 

   public static void main(String args[]) {
     new CalculatorServer();
   }
}


4.客户端程序
/*
 * Created on 2005-8-22
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.blankboard.rmi;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;

public class CalculatorClient {
 public static void main(String[] args) {
  try {
   Calculator c = (Calculator) Naming
     .lookup("rmi://localhost/CalculatorService");
   System.out.println(c.sub(4, 3));
   System.out.println(c.add(4, 5));
   System.out.println(c.mul(3, 6));
   System.out.println(c.div(9, 3));
   
  } catch (MalformedURLException murle) {
   System.out.println();
   System.out.println("MalformedURLException");
   System.out.println(murle);
  } catch (RemoteException re) {
   System.out.println();
   System.out.println("RemoteException");
   System.out.println(re);
  } catch (NotBoundException nbe) {
   System.out.println();
   System.out.println("NotBoundException");
   System.out.println(nbe);
  } catch (java.lang.ArithmeticException ae) {
   System.out.println();
   System.out.println("java.lang.ArithmeticException");
   System.out.println(ae);
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值