科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道我的RMI实践

我的RMI实践

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

  //: c15:rmi:PerfectTime

作者:中国IT实验室 来源:中国IT实验室 2007年9月9日

关键字: Rmi/Corba/Jini 编程 java

  • 评论
  • 分享微博
  • 分享邮件

  //: c15:rmi:PerfectTime.java
  // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
  // www.BruceEckel.com. See copyright notice in CopyRight.txt.
  // The implementation of
  // the PerfectTime remote object.
  import java.rmi.*;
  import java.rmi.server.*;
  import java.rmi.registry.*;
  import java.net.*;
  
  public class PerfectTime
  extends UnicastRemoteObject
  implements PerfectTimeI {
  // Implementation of the interface:
  public long getPerfectTime()
  throws RemoteException {
  return System.currentTimeMillis();
  }
  // Must implement constructor
  // to throw RemoteException:
  public PerfectTime() throws RemoteException {
  // super(); // Called automatically
  }
  // Registration for RMI serving. Throw
  // exceptions out to the console.
  public static void main(String[] args) throws Exception {
  PerfectTime pt = new PerfectTime();
  Naming.rebind("/PerfectTime", pt);
  System.out.println("Ready to do time");
  }
  } ///:~
  //: c15:rmi:PerfectTimeI.java
  // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
  // www.BruceEckel.com. See copyright notice in CopyRight.txt.
  // The PerfectTime remote interface.
  import java.rmi.*;
  
  interface PerfectTimeI extends Remote {
  long getPerfectTime() throws RemoteException;
  } ///:~
  //: c15:rmi:DisplayPerfectTime.java
  // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
  // www.BruceEckel.com. See copyright notice in CopyRight.txt.
  // Uses remote object PerfectTime.
  import java.rmi.*;
  import java.rmi.registry.*;
  
  public class DisplayPerfectTime {
  public static void main(String[] args)
  throws Exception {
  System.setSecurityManager(
  new RMISecurityManager());
  PerfectTimeI t = (PerfectTimeI)Naming.lookup("/PerfectTime");
  for(int i = 0; i < 10; i++)
  System.out.println("Perfect time = " +
  t.getPerfectTime());
  }
  } ///:~
  grant {
  // Allow everything for now
  permission java.security.AllPermission;
  };
  
  存入于F:\rmi下
  操作步骤
  F:\rmi>java PerfectTime
  Ready to do time
  F:\rmi>javac *.java
  F:\rmi>rmic PerfectTime
  F:\rmi>start rmiregistry
  F:\rmi>java PerfectTime
  Ready to do time
  另开一DOS窗口
  F:\rmi>java -Djava.security.policy=registerit.policy DisplayPerfectTime
  Perfect time = 1011585443953
  Perfect time = 1011585443968
  Perfect time = 1011585444000
  Perfect time = 1011585444000
  Perfect time = 1011585444015
  Perfect time = 1011585444031
  Perfect time = 1011585444031
  Perfect time = 1011585444046
  Perfect time = 1011585444062
  Perfect time = 1011585444062
  注意:如果在rmic PerfectTime时报找不到PerfectTime类,请在环境变量中的ClassPath中加入:F:\rmi

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章