RMI 远程方法调用

首先RMI远程调用技术适用与cs结构,即服务端与客户端结构


具体流程如下:服务端-代理-    代理--客户端

即是代理与代理之间的互动,客户段的代理我们称之为存根。

1,服务端注册对象。

2,客户端通过调用对象获取存根

3,客户端通过存根(代理)远程访问服务端提供的方法。


RMI主要用于双方为java应用之间的访问。而webservice适用于跨语言跨系统系统之间的访问。


package com.huawei.rmi;


import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;
//服务段定义的远程方法接口
public interface Warehouse extends Remote {
	double getPrice(String description) throws RemoteException;
}
package com.huawei.rmi;


import java.rmi.RemoteException;
import java.util.Enumeration;


import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingException;


public class WarehouseClient {
	public static void main(String[] args) throws NamingException, RemoteException {
		Context namingContext = new InitialContext();//构建一个上下文,用来访问注册表
		System.out.println("RMI registry bindings");
		//NameClassPair 是一个助手类,它包含绑定对象的名字和该对象所属类的名字
		Enumeration<NameClassPair> e = namingContext.list("rmi://localhost/");
		//打印绑定对象的信息
		while(e.hasMoreElements())
			System.out.println(e.nextElement().getName());
		//客户端获取存根
		String url = "rmi://localhost/central_warehouse";
		
		Warehouse centralWarehouse = (Warehouse) namingContext.lookup(url);
		String descr = "xiongf";
		double price = centralWarehouse.getPrice(descr);
		System.out.println(price);
	}
}

package com.huawei.rmi;


import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;
//实现类
class WarehouseImpl extends UnicastRemoteObject implements Warehouse{
	private Map<String,Double> prices;
	public WarehouseImpl() throws RemoteException {
		prices = new HashMap();
		prices.put("Blackwell", 25.325);
		prices.put("xiongf", 423.4);
	}


	@Override
	public double getPrice(String description) throws RemoteException {
		// TODO Auto-generated method stub\
		Double price = prices.get(description);
		
		return price==null?0:price;
	}
	
}
package com.huawei.rmi;


import java.rmi.RemoteException;


import javax.naming.Context;
import javax.naming.NamingException;
//服务端注册远程对象
import javax.naming.InitialContext;
/**
 * 直接构造并注册了一个WarehouseImpl对象
 * @author Administrator
 *
 */
public class WarehouseServer {
	public static void main(String[] args) throws RemoteException, NamingException {
		System.out.println("Constructing server implementation...");
		WarehouseImpl centralWarehouse = new WarehouseImpl();
		System.out.println("Binding server implementation to registry");
		Context namingContext = new InitialContext();
		namingContext.bind("rmi:central_warehouse",centralWarehouse);//将name与obj对象绑定(unbind)解除绑定
		System.out.println("Waiting for invocations form  clients");
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值