标题: 使用 rmic 构建 java rmi 例子 [打印本页] 作者: dzbjet 时间: 2008-05-10 15:45 标题: 使用 rmic 构建 java rmi 例子 由于学习Java才一周多,因此构建rmic的例子,虽然对照书本写例子,但是由于一些路径的问题,始终不能通过,网上搜索的也是只言片语,因此写一篇文章,总结如下:
整个工程的目的:
客户端调用服务器端的Fib对象的getFib(BigInteger n),计算Fibonacci数列的值。
工程 test_it 目录结构:
e:\codes\java_w\
\test_it
src
test
Fib.java // extends Remote 的接口
FibImp.java // 实现 Fib 接口的文件
FibonacciServer.java // Server 服务程序,用于处理rmi调用
testClient
FibClient.java // rmi的客户端,调用 remote object 的getFib方法,计算Fibonacci数列。
bin
test
Fib.class
FibImp.class
FibonacciServer.class
testClient
FibClient.class
首先,1. create remote interface by extends java.rmi.remote interface
Fib.java:
package test;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.math.BigInteger;
public interface Fib extends Remote {
public BigInteger getFib(int n) throws RemoteException;
public BigInteger getFib(BigInteger n) throws RemoteException;
}
2. define a class that implements this remote interface
FibImp.java
package test;
import java.math.BigInteger;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class FibImp implements Fib {
public FibImp() throws RemoteException {
UnicastRemoteObject.exportObject(this);
}
@Override
public BigInteger getFib(int n) throws RemoteException {
return this.getFib(new BigInteger(Long.toString(n)));
}
@Override
public BigInteger getFib(BigInteger n) throws RemoteException {
System.out.println("Calculating the " + n + "th Fibonacci number");
BigInteger zero = new BigInteger("0");
BigInteger one = new BigInteger("1");