- 論壇徽章:
- 0
|
由于學(xué)習(xí)Java才一周多,因此構(gòu)建rmic的例子,雖然對照書本寫例子,但是由于一些路徑的問題,始終不能通過,網(wǎng)上搜索的也是只言片語,因此寫一篇文章,總結(jié)如下:
整個(gè)工程的目的:
客戶端調(diào)用服務(wù)器端的Fib對象的getFib(BigInteger n),計(jì)算Fibonacci數(shù)列的值。
工程 test_it 目錄結(jié)構(gòu):
e:\codes\java_w\
\test_it
src
test
Fib.java // extends Remote 的接口
FibImp.java // 實(shí)現(xiàn) Fib 接口的文件
FibonacciServer.java // Server 服務(wù)程序,用于處理rmi調(diào)用
testClient
FibClient.java // rmi的客戶端,調(diào)用 remote object 的getFib方法,計(jì)算Fibonacci數(shù)列。
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");
if( n.equals(zero) ) return zero;
if( n.equals(one) ) return one;
BigInteger i = one;
BigInteger a = zero;
BigInteger b = one;
while (i.compareTo(n) == -1) {
BigInteger temp = b;
b = b.add(a);
a = temp;
i = i.add(one);
}
return b;
}
}
|
3. FibonacciServer.java 服務(wù)器端完成對Fib對象的注冊。
package test;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class FibonacciServer {
/**
* @param args
*/
public static void main(String[] args) {
try {
FibImp f = new FibImp();
// 注冊到 registry 中
Naming.rebind("fib", f);
System.out.println("fib server ready");
} catch (RemoteException re) {
System.out.println("Exception in FibonacciImpl.main: " + re);
} catch (MalformedURLException e) {
System.out.println("MalformedURLException " + e);
}
}
}
|
4. FibClient.java
package testClient;
import test.Fib;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class FibClient {
/**
* @param args
*/
public static void main(String[] args) {
String url = "rmi://void-zb/fib";
try {
Fib calc = (Fib) Naming.lookup(url);
BigInteger f = calc.getFib(10);
System.out.println(f);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
}
}
|
第二步:
使用rmic編譯stub文件,在jdk1.5以后,利用java的reflect機(jī)制,因此就不需要skeleton文件了。
a) 進(jìn)入test_it\bin目錄
b) rmic -classpath . test.FibImp
第三步:
啟動rmiregister,任何目錄都可以。
start rmiregister
第四步:
啟動server:
a) 進(jìn)入 test_it\bin\
b) java -Djava.rmi.server.codebase=file:///e:\codes\java_w\test_it\bin\ -classpath . test.FibonacciServer
第五步:啟動client:
a) 進(jìn)入 test_it\bin\testClient
b) java -classpath .. testClient.FibClient
另外,有個(gè)疑問,如果第四步中b),不指定 codebase屬性,就不行,老是ClassNotFound異常(FibImp_Stub),不知道為什么? |
|