RMI基础

RMI是java最基本的分布式计算模型。这是一种最简单的方法实现分布式计算和远程调用。
数次碰壁以,找到一不错的教程,在此进行简单解读:
原文地址:
[url]http://www.bitscn.com/pdb/java/200605/22375.html[/url]

[color=red]服务的约定接口,需要扩展Remote接口[/color]
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;
}



[color=red]服务器端实现类[/color]

import java.rmi.server.UnicastRemoteObject; 

public class CalculatorImpl extends UnicastRemoteObject implements Calculator
{

// 这个实现必须有一个显式的构造函数,并且要抛出一个RemoteException异常
public CalculatorImpl()
throws java.rmi.RemoteException {
super();
}

public long add(long a, long b)
throws java.rmi.RemoteException {
return a + b;
}

public long sub(long a, long b)
throws java.rmi.RemoteException {
return a - b;
}

public long mul(long a, long b)
throws java.rmi.RemoteException {
return a * b;
}

public long div(long a, long b)
throws java.rmi.RemoteException {
return a / b;
}
}

  



[color=red]程序入口,服务器启动类[/color]
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();
}
}


  [color=red]客户端程序[/color]


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);
}
}
}



OK,现在代码已经完整了。接下来看看具体启动。
1 在该服务器实现类的路径下执行以下命令
>rmic CalculatorImpl

rmic 程序可以将CalculatorImpl生成Calculator_stub.class

2 在程序的目录执行

 >rmiregistry


3 新建一个策略文件policy.txt(文本)

grant {
permission java.security.AllPermission "", "";
};


这是最简单的授权策略,授权所有权限。若果在正式的生产环境,应该按照具体需求具体配置。
4 启动服务端

 > java -Djava.security.policy=policy.txt CalculatorServer


5 启动客户端

> java -Djava.security.policy=policy.txt CalculatorClient


  如果所有的这些都成功运行,你应该看到下面的输出:
  1
  9
  18
  3

如果在执行那两个可执行文件的时候发现找不到程序,请检查好path环境变量配置好没有。
另外
rmiregistry在执行后请勿关闭。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值