免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1912 | 回复: 0
打印 上一主题 下一主题

rmic 例子 + 疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-10 22:30 |只看该作者 |倒序浏览
由于学习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");
      
        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 服务器端完成对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机制,因此就不需要skeleton文件了。
a) 进入test_it\bin目录
b) rmic -classpath . test.FibImp

第三步:
启动rmiregister,任何目录都可以。
start rmiregister

第四步:
启动server:
a) 进入 test_it\bin\
b) java -Djava.rmi.server.codebase=file:///e:\codes\java_w\test_it\bin\ -classpath . test.FibonacciServer


第五步:启动client:
a) 进入 test_it\bin\testClient
b) java -classpath .. testClient.FibClient

另外,有个疑问,如果第四步中b),不指定 codebase属性,就不行,老是ClassNotFound异常(FibImp_Stub),不知道为什么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP