我的RMI实践
来源:互联网 发布:linux ibus rime配置 编辑:程序博客网 时间:2024/05/22 05:11
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>注:本文所涉及代码转自thinking in 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 PerfectTimeReady to do timeF:/rmi>javac *.javaF:/rmi>rmic PerfectTimeF:/rmi>start rmiregistryF:/rmi>java PerfectTimeReady to do time另开一DOS窗口F:/rmi>java -Djava.security.policy=registerit.policy DisplayPerfectTimePerfect time = 1011585443953Perfect time = 1011585443968Perfect time = 1011585444000Perfect time = 1011585444000Perfect time = 1011585444015Perfect time = 1011585444031Perfect time = 1011585444031Perfect time = 1011585444046Perfect time = 1011585444062Perfect time = 1011585444062注意:如果在rmic PerfectTime时报找不到PerfectTime类,请在环境变量中的ClassPath中加入:F:/rmi