您的位置:首页 > 编程语言 > Java开发

java分布式对象RMI应用测试用例

2016-02-20 16:59 531 查看
【0】README

0.1)本文旨在对http://blog.csdn.net/PacosonSWJTU/article/details/50705192  中的代码进行实践(如何部署一个使用RMI框架的程序以进行远程调用);

0.2) for complete source code, please visit  https://github.com/pacosonTang/core-java-volume/tree/master/coreJavaAdvanced/chapter11/rmi_test

0.3)目录结构如下: 



【1】部署前的准备

step1)创建两个目录, 分别存放用于启动server 和 client 的类:

server/
    WarehouseServer.class
    Warehouse.class

    WarehouseImpl.class

client/
    WarehouseClient.class
    Warehouse.class
step2)创建download 目录,用于存放 NanoHTTPD web server 

【1】部署步骤如下

step1)打开一个新的控制台窗口,转到download目录,然后将NanoHTTPD.java(NanoHTTPD
web 服务器)复制到这个目录中。(NanoHTTPD web 服务器的源代码 ,参见, https://github.com/pacosonTang/core-java-volume/tree/master/coreJavaAdvanced/chapter11/rmi_test)
<span style="font-size:14px;">package com.corejava.chapter11.download;

import java.io.IOException;

public class NanoHTTPDMain extends NanoHTTPD
{
public NanoHTTPDMain() throws IOException
{
super(8080);
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
System.out.println("\n nano http web server is running! Point your browers to http://localhost:8080/ \n");
}

public static void main(String[] args)
{
try
{
new NanoHTTPDMain();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
</span>



step2)在server 程序中通过代码注册通讯端口和注册通讯路径

<span style="font-size:14px;">public class WarehouseServer
{
public static void main(String[] args)
{
try
{
WarehouseImpl warehouseService = new WarehouseImpl();
// 注册通讯端口
LocateRegistry.createRegistry(1099);
// 注册通讯路径
Naming.rebind("rmi://localhost:1099/warehouseService", warehouseService);
System.out.println("warehouse service starts");
} catch (Exception e)
{
e.printStackTrace();
}
}
}</span>


<span style="font-size:14px;">// 这个WarehouseImpl类是远程方法调用的目标,因为它继承自UnicastRemoteObject,
// 这个类的构造器使得它的对象可供远程访问;
public class WarehouseImpl extends UnicastRemoteObject implements Warehouse
{
private Map<String, Double> prices;

public WarehouseImpl() throws RemoteException
{
prices = new HashMap<>();
prices.put("A", 24.95);
prices.put("B", 49.95);
}

public double getPrice(String description) throws RemoteException
{
Double price = prices.get(description);
return price == null ? 0 : price;
}
}</span>
<span style="font-size:14px;">// 远程对象的接口必须扩展Remote接口
public interface Warehouse extends Remote
{
double getPrice(String description) throws RemoteException;
}
</span>


step3)现在已经准备好启动服务器了。打开第三个控制台窗口,转到server目录,并执行下面的命令:

java -Djava.rmi.server.codebase=http://localhost:8080/ WarehouseServer  // java.rmi.server.codebase属性指出了服务类文件的URL。服务器程序将这个URL传递给RMI注册表。




step4)最后,打开第四个控制台窗口,转到client目录,运行:




<span style="font-size:14px;">// 客户端如何获得远程仓库对象的存根,并调用远程的getPrice方法。
public class WarehouseClient
{
public static void main(String[] args)
{
try
{
Context namingContext = new InitialContext();
// NameClassPair是一个助手类: 它包含绑定对象的名字和该对象所属类的名字。
Enumeration<NameClassPair> e = namingContext
.list("rmi://localhost:1099/");
while (e.hasMoreElements())
System.out.println(e.nextElement().getName());

// 客户端可以通过下面的方式,来指定服务器和远程对象的名字,以此获得访问远程对象所需的存根:
String url = "rmi://localhost:1099/warehouseService";
Warehouse centralWarehouse = (Warehouse) namingContext.lookup(url);

String descr = "A";
double price = centralWarehouse.getPrice(descr);
System.out.println(descr + ": " + price);
} catch (Exception e)
{
e.printStackTrace();
}
}}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: