Java RMI

Java RMI 指的是远程方法调用 (Remote Method Invocation)。它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法。可以用此方法调用的任何对象必须实现该远程接口。
下面通过一个简单的例子来说明RMI的原理和应用。
[color=violet]/**
*
* 定义一个远程接口,必须继承Remote接口,其中需要远程调用的方法必须抛出RemoteException异常
*/
public interface PowerService extends Remote {

/**
*
* @throws java.rmi.RemoteException
*/
public String StrongPower() throws RemoteException;
}[/color]

[color=violet]/**
*
* 远程的接口的实现
*/
public class PowerServiceImpl extends UnicastRemoteObject implements PowerService {
/**
* 因为UnicastRemoteObject的构造方法抛出了RemoteException异常,因此这里默认的构造方法必须写,必须声明抛出RemoteException异常
*
* @throws RemoteException
*/
public PowerImpl() throws RemoteException {
}

/**
* @throws java.rmi.RemoteException
*/
public String StrongPower() throws RemoteException {
return "hao ke";
}
}[/color]
下面一步配置文件,配置远程ip、port可根据项目需要选择是否添加,如无需要可在PowerRmiServer中直接定义对象,ip,端口。
****************************************
[color=violet]RmiServiceConfig.properties
#RMI 服务IP与端口
Rmi.Service.Ip=172.15.1.3
Rmi.Service.Port=10201

#服务实现类
Rmi.Service.services=PowerService

PowerService = com.lc.da.PowerServiceImpl[/color]
****************************************
读取配置文件
[color=violet]public class Configuration {
private static Map<String, Object> config = new ConcurrentHashMap<String, Object>();
Properties rmiConfig = new Properties();
try {
rmiConfig.load(new FileInputStream(System.getProperty("user.dir")+"//bin//RmiServiceConfig.properties"));
for (Entry<Object, Object> e : rmiConfig.entrySet()) {
Configuration.addConfig(String.valueOf(e.getKey()), String.valueOf(e.getValue()));
}
} catch (IOException e) {
e.printStackTrace();
}
public static void addConfig(String key, String value) {
config.put(key, value);
}
public static String getString(String key) {
Object value = getConfig(key);

if (value != null && !"".equals(value.toString())) {
return value.toString();
} else {
throw new RuntimeException("config key :["+key+"] is not find.");
}
}
}[/color]
****************************************
[color=violet]/**
*
* 创建RMI注册表,启动RMI服务,并将远程对象注册到RMI注册表中。
*/
public class PowerRmiServer {
private static String ip = Configuration.getString("Rmi.Service.Ip");

private static int port = Integer.parseInt(Configuration.getString("Rmi.Service.Port"));

private static String services = Configuration.getString("Rmi.Service.services");
public static void main(String args[]) {

try {
//本地主机上的远程对象注册表Registry的实例,并指定端口为port,这一步必不可少(Java默认端口是1099),必不可缺的一步,缺少注册表创建,则无法绑定对象到远程注册表上
LocateRegistry.createRegistry(port);
//把远程对象注册到RMI注册服务器上,并命名为service
//绑定的URL标准格式为:rmi://ip:port/services(其中协议名可以省略,下面两种写法都是正确的)
Naming.bind("rmi://ip:port/service",services);
// Naming.bind("//ip:port/service",services);

System.out.println("远程对象services绑定成功!");
} catch (RemoteException e) {
System.out.println("创建远程对象发生异常!");
e.printStackTrace();
} catch (AlreadyBoundException e) {
System.out.println("发生重复绑定对象异常!");
e.printStackTrace();
} catch (MalformedURLException e) {
System.out.println("发生URL畸形异常!");
e.printStackTrace();
}
}
}

客户端调用:
/**
*
* 在客户端调用远程对象上的远程方法,并返回结果。
*/
public class TestClient {
public static void main(String args[]){
try {
//在RMI服务注册表中查找名称为service的对象,并调用其上的方法
PowerService power =(PowerService) Naming.lookup("rmi://172.15.1.3:10201/service");
System.out.println(power.StrongPower());
} catch (NotBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
[/color]
后话:实际应用到项目中,根据业务需求,逻辑业务也许会很复杂,但万变不离其宗,rmi就跟webservice差不多,得先把内部方法发布,提供给客户端调用。这儿的发布,就是创建注册表,把提供调用的对象方法bind到注册表中。
另:由于本人正在学习中,组织语言能力较差,写此文章只是为了方便日后加深印象,除了代码是本人结合自己写的项目之外,注释大多借鉴网上总结。如有侵权,请联系我删除。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值