posts - 495,comments - 227,trackbacks - 0

          學(xué)習(xí)java tutorial中的rmi章節(jié),讀書筆記


          JAVA-RMI 所采用的機(jī)制:

          1. 某Object需要remote call ,則它所有的方法都放在一個(gè)新聲明的接口中,該接口必須extends          Remote接口

             Compute接口中存放所有需要進(jìn)行遠(yuǎn)程調(diào)用的方法,讓實(shí)現(xiàn)該接口的類來實(shí)現(xiàn)這些被遠(yuǎn)程調(diào)用的方法

          package compute;

          import java.rmi.Remote;
          import java.rmi.RemoteException;

          public interface Compute extends Remote {
              Object executeTask(Task t) throws RemoteException;
          }


          2.采用遠(yuǎn)程調(diào)用的方法同樣有一些參數(shù)和返回值,對于這些參數(shù)和返回值,在不同JAVA虛擬機(jī)間傳輸,RM  I采取的是值傳遞,所以所有的參數(shù)和返回值必須extends Serializable

          package compute;

          import java.io.Serializable;

          public interface Task extends Serializable {
              Object execute();
          }


          /*******************************************************************************************
          上面為第一部分,在這當(dāng)中,我們設(shè)計(jì)了一個(gè)接口,其中包含需要遠(yuǎn)程調(diào)用的方法,然后讓該接口的實(shí)現(xiàn)者(即遠(yuǎn)程對象的owner)來實(shí)現(xiàn)這些方法
          *******************************************************************************************/


          3. 現(xiàn)在我們?yōu)榱说玫竭h(yuǎn)程對象,就來實(shí)現(xiàn)一下上面定義的接口:
             main函數(shù)中,新建一個(gè)遠(yuǎn)程對象engine,并將它與名字name邦定:Naming.rebind(name, engine);

          注:The superclass UnicastRemoteObject supplies implementations for a number of     java.lang.Object methods (equals, hashCode, toString) so that they are defined     appropriately for remote objects. UnicastRemoteObjectalso includes constructors and     static methods used to export a remote object, that is, make the remote object available     to receive incoming calls from clients.

          package engine;

          import java.rmi.*;
          import java.rmi.server.*;
          import compute.*;

          public class ComputeEngine extends UnicastRemoteObject
                                     implements Compute
          {
              public ComputeEngine() throws RemoteException {
                  super();
              }

              public Object executeTask(Task t) {
                  return t.execute();
              }

              public static void main(String[] args) {
                  if (System.getSecurityManager() == null) {
                      System.setSecurityManager(new RMISecurityManager());
                  }
                  String name = "http://host/Compute";
                  try {
                      Compute engine = new ComputeEngine();
                      Naming.rebind(name, engine);
                      System.out.println("ComputeEngine bound");
                  } catch (Exception e) {
                      System.err.println("ComputeEngine exception: " +
                    e.getMessage());
                      e.printStackTrace();
                  }
              }
          }

          /*******************************************************************************************
          上面為第二部分,在這當(dāng)中,我們得到了一個(gè)實(shí)現(xiàn)了Compute接口的遠(yuǎn)程對象engine,注意:engine中只有那些Compute接口中的方法能夠被遠(yuǎn)程調(diào)用,其他方法只能本地調(diào)用。現(xiàn)在,將遠(yuǎn)程對象engine邦定到RMI Registry 。 client可以從registry那里得到engine的引用,實(shí)際上是一個(gè)stub,相當(dāng)于一個(gè)engine的代理,遠(yuǎn)程方法被client調(diào)用以后,可以改變engine的狀態(tài)
          *******************************************************************************************/
            
          4. 下面我們實(shí)現(xiàn)那些遠(yuǎn)程方法調(diào)用時(shí)所要用到的參數(shù):Task

          package client;

          import compute.*;
          import java.math.*;

          public class Pi implements Task {

              /** constants used in pi computation */
              private static final BigDecimal ZERO =
                  BigDecimal.valueOf(0);
              private static final BigDecimal  ONE =
                  BigDecimal.valueOf(1);
              private static final BigDecimal FOUR =
                  BigDecimal.valueOf(4);

              /** rounding mode to use during pi computation */
              private static final int roundingMode =
                  BigDecimal.ROUND_HALF_EVEN;

              /** digits of precision after the decimal point */
              private int digits;
             
              /**
               * Construct a task to calculate pi to the specified
               * precision.
               */
              public Pi(int digits) {
                  this.digits = digits;
              }

              /**
               * Calculate pi.
               */
              public Object execute() {
                  return computePi(digits);
              }

              /**
               * Compute the value of pi to the specified number of
               * digits after the decimal point.  The value is
               * computed using Machin's formula:
               *
               *          pi/4 = 4*arctan(1/5) - arctan(1/239)
               *
               * and a power series expansion of arctan(x) to
               * sufficient precision.
               */
              public static BigDecimal computePi(int digits) {
                  int scale = digits + 5;
                  BigDecimal arctan1_5 = arctan(5, scale);
                  BigDecimal arctan1_239 = arctan(239, scale);
                  BigDecimal pi = arctan1_5.multiply(FOUR).subtract(
                                            arctan1_239).multiply(FOUR);
                  return pi.setScale(digits,
                                     BigDecimal.ROUND_HALF_UP);
              }
              /**
               * Compute the value, in radians, of the arctangent of
               * the inverse of the supplied integer to the speficied
               * number of digits after the decimal point.  The value
               * is computed using the power series expansion for the
               * arc tangent:
               *
               * arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 +
               *     (x^9)/9 ...
               */  
              public static BigDecimal arctan(int inverseX,
                                            int scale)
              {
                  BigDecimal result, numer, term;
                  BigDecimal invX = BigDecimal.valueOf(inverseX);
                  BigDecimal invX2 =
                      BigDecimal.valueOf(inverseX * inverseX);

                  numer = ONE.divide(invX, scale, roundingMode);

                  result = numer;
                  int i = 1;
                  do {
                      numer =
                          numer.divide(invX2, scale, roundingMode);
                      int denom = 2 * i + 1;
                      term =
                          numer.divide(BigDecimal.valueOf(denom),
                                       scale, roundingMode);
                      if ((i % 2) != 0) {
                          result = result.subtract(term);
                      } else {
                          result = result.add(term);
                      }
                      i++;
                  } while (term.compareTo(ZERO) != 0);
                  return result;
              }
          }

          5. 最后我們實(shí)現(xiàn)client對象
             遠(yuǎn)程對象comp是通過RMI的注冊表來查找到的

          package client;

          import java.rmi.*;
          import java.math.*;
          import compute.*;

          public class ComputePi {
              public static void main(String args[]) {
                  if (System.getSecurityManager() == null) {
                      System.setSecurityManager(new RMISecurityManager());
                  }
                  try {
                      String name = "http://" + args[0] + "/Compute";
                      Compute comp = (Compute) Naming.lookup(name);
                      Pi task = new Pi(Integer.parseInt(args[1]));
                      BigDecimal pi = (BigDecimal) (comp.executeTask(task));
                      System.out.println(pi);
                  } catch (Exception e) {
                      System.err.println("ComputePi exception: " +
                                         e.getMessage());
                      e.printStackTrace();
                  }
              }
          }

          posted on 2008-04-03 13:25 SIMONE 閱讀(1003) 評論(1)  編輯  收藏 所屬分類: rmi

          FeedBack:
          # re: JAVA-RMI學(xué)習(xí)心得
          2008-05-13 11:21 | 狀元郎
          ICE是個(gè)很有潛力的中間件架構(gòu)。語言中立。性能很好。
          據(jù)說是用來替代CORBA和RMI的。  回復(fù)  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 贵溪市| 揭东县| 武胜县| 岱山县| 余干县| 贵南县| 乡宁县| 浪卡子县| 辉县市| 色达县| 安康市| 宁德市| 儋州市| 普安县| 会东县| 盈江县| 永昌县| 清新县| 灵山县| 中方县| 仁怀市| 岑巩县| 中牟县| 神池县| 定州市| 凌海市| 通渭县| 深圳市| 高平市| 修文县| 鄂托克前旗| 阳信县| 弥渡县| 霍林郭勒市| 长顺县| 民和| 铁岭县| 红河县| 广西| 耒阳市| 乐至县|