`

java RMI的例子(转)

阅读更多

参考别人的程序后写了个自己的RMI 程序

RMI ,远程方法调用(Remote Method Invocation)是Enterprise
     JavaBeans的支柱,是建立分布式Java应用程序的方便途径。RMI 是非 常容易使用的,但是它非常的强大。
    RMI 的基础是接口,RMI 构 架基于一个重要的原理:定义接口和定义接口的具体实现是分开的。

    java 代码

 
  1. package  org.itrun.remote;  
  2.   
  3. import  java.rmi .Remote;  
  4. import  java.rmi .RemoteException;  
  5.   
  6.   
  7. /**  
  8.  * 远程接口  
  9.  * @author jiangzhen  
  10.  *  
  11.  */   
  12. public   interface  TestInterfactRemote  extends  Remote{  
  13.   
  14.     public  String add(String a,String b)  throws  RemoteException;  
  15.       
  16.     public  String add()  throws  RemoteException;  
  17.       
  18. }  
  19.   
  20.   
  21.   
  22.   
  23.   
  24. package  org.itrun.remote;  
  25.   
  26. import  java.rmi .RemoteException;  
  27. import  java.rmi .server.UnicastRemoteObject;  
  28.   
  29.   
  30. /**  
  31.  * 接口的实现  
  32.  * @author jiangzhen  
  33.  *  
  34.  */   
  35. public   class  TestInterfaceRemoteImpl  extends  UnicastRemoteObject  implements   
  36.         TestInterfactRemote {  
  37.   
  38.       
  39.       
  40.       
  41.     public  TestInterfaceRemoteImpl()  throws  RemoteException {  
  42.         super ();  
  43.     }  
  44.   
  45.     public  String add(String a, String b)  throws  RemoteException {  
  46.         return  a+b;  
  47.     }  
  48.   
  49.     public  String add()  throws  RemoteException {  
  50.         return   "Hello Word" ;  
  51.     }  
  52.   
  53. }  
  54.   
  55.   
  56.   
  57.   
  58.   
  59. package  org.itrun.server;  
  60.   
  61. import  java.rmi .Naming;  
  62.   
  63. import  org.itrun.remote.TestInterfaceRemoteImpl;  
  64. import  org.itrun.remote.TestInterfactRemote;  
  65.   
  66.   
  67. /**  
  68.  * 服务器端  
  69.  * @author jiangzhen  
  70.  *  
  71.  */   
  72. public   class  Server{  
  73.       
  74.       
  75.     public  Server() {  
  76.         try  {  
  77.             TestInterfactRemote testInterfactRemote = new  TestInterfaceRemoteImpl();  
  78.             Naming.rebind("rmi ://10.0.0.123/server" , testInterfactRemote);  
  79.         } catch  (Exception e) {  
  80.             e.printStackTrace();  
  81.         }  
  82.     }  
  83.   
  84.     public   static   void  main(String args[]) {  
  85.         new  Server();  
  86.     }  
  87. }  
  88.   
  89.   
  90.   
  91.   
  92. package  org.itrun.client;  
  93.   
  94. import  java.rmi .Naming;  
  95.   
  96. import  org.itrun.remote.TestInterfactRemote;  
  97.   
  98.   
  99.   
  100. /**  
  101.  * 客户端程序  
  102.  * @author jiangzhen  
  103.  *  
  104.  */   
  105. public   class  Client {  
  106.     public   static   void  main(String args[]) {  
  107.         try  {  
  108.             TestInterfactRemote testInterfactRemote = (TestInterfactRemote) Naming  
  109.                     .lookup("rmi ://10.0.0.123/server" );  
  110.             System.out.println(testInterfactRemote.add("rmi  a " "rmib" ));  
  111.         } catch  (Exception e) {  
  112.             e.printStackTrace();  
  113.         }  
  114.     }  
  115. }  




程序完成后

运行RMI 系统

     现在我们建立了所有运行这个简单RMI 系统所需的文件,现在我们终于可以运 行这个RMI 系统啦!来享受吧。

     我们是在命令控制台下运行这个系统的,你必须开启三个控制台窗口,一个运行服务器,一个运行客户端,还有一个运行RMIRegistry。

    首先运行注册程序RMIRegistry,你必须在包含你刚写的类的那么目录下运行这个注册程序。

    >rmiregistry(linux) start rmiregistry(windows)

   运行程序首先需要修改下RMI 的安全策略策略文件在 JDK 目录下的jre/lib/security/java.policy 的最后面加上下面这句话

grant{
        permission java.security.AllPermission "","";
};
这条命令表示允许任何人作任何事情,

修改完后运行命令 rmiregistry ,然后就不用管他了

然后运行 server 端的代码 运行后也不用管他了
java org.itrun.server. Server

最后运行 client 端代码
java org.itrun.server.Client

运行结果 rmi a rmib

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics