亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1962 | 回復(fù): 0
打印 上一主題 下一主題

rmic 例子 + 疑問 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-05-10 22:30 |只看該作者 |倒序?yàn)g覽
由于學(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),不知道為什么?
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP