Welcome 微信登录

首页 / 操作系统 / Linux / Java RMI的例子

参考别人的程序后写了个自己的RMI程序RMI,远程方法调用(Remote Method Invocation)是Enterprise
     JavaBeans的支柱,是建立分布式Java应用程序的方便途径。RMI是非常容易使用的,但是它非常的强大。
    RMI的基础是接口,RMI构架基于一个重要的原理:定义接口和定义接口的具体实现是分开的。    java 代码
package org.itrun.remote;  
  
import java.rmi.Remote;  
import java.rmi.RemoteException;  
  
  
/**
* 远程接口
* @author jiangzhen
*
*/  
public interface TestInterfactRemote extends Remote{  
  
    public String add(String a,String b) throws RemoteException;  
      
    public String add() throws RemoteException;  
      
}  
  
  
  
  
  
package org.itrun.remote;  
  
import java.rmi.RemoteException;  
import java.rmi.server.UnicastRemoteObject;  
  
  
/**
* 接口的实现
* @author jiangzhen
*
*/  
public class TestInterfaceRemoteImpl extends UnicastRemoteObject implements  
         TestInterfactRemote {  
  
      
      
      
    public TestInterfaceRemoteImpl() throws RemoteException {  
        super();  
     }  
  
    public String add(String a, String b) throws RemoteException {  
        return a+b;  
     }  
  
    public String add() throws RemoteException {  
        return "Hello Word";  
     }  
  
}  
  
  
  
  
  
package org.itrun.server;  
  
import java.rmi.Naming;  
  
import org.itrun.remote.TestInterfaceRemoteImpl;  
import org.itrun.remote.TestInterfactRemote;  
  
  
/**
* 服务器端
* @author jiangzhen
*
*/  
public class Server{  
      
      
    public Server() {  
        try {  
             TestInterfactRemote testInterfactRemote = new TestInterfaceRemoteImpl();  
             Naming.rebind("rmi://10.0.0.123/server", testInterfactRemote);  
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
     }  
  
    public static void main(String args[]) {  
        new Server();  
     }  
}  
  
  
  
  
package org.itrun.client;  
  
import java.rmi.Naming;  
  
import org.itrun.remote.TestInterfactRemote;