一个超级简单rmi通信的实例

RmiServer

一个超级简单rmi通信的实例
public class RmiServer {
    
    public static void main(String[] args) {
        try {
            ISayHello iSayHello = new SayHelloImpl();
            LocateRegistry.createRegistry(8888);
            Naming.bind("rmi://localhost:8888/xxx", iSayHello);
            System.out.println("server start success!!!");
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
View Code

一个超级简单rmi通信的实例

 

 

 

RmiClient

一个超级简单rmi通信的实例
public class RmiClient {

    public static void main(String[] args) {
        try {
            ISayHello iSayHello = (ISayHello) Naming.lookup("rmi://localhost:8888/xxx");
            System.out.println(iSayHello);
            System.out.println(iSayHello.sayHello("hello world!!!"));
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NotBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
    }
}
View Code

 

一个超级简单rmi通信的实例

 

ISayHello

一个超级简单rmi通信的实例
public interface ISayHello extends Remote{

    public String sayHello(String name)throws RemoteException;
}
View Code

 

SayHelloImpl

一个超级简单rmi通信的实例
public class SayHelloImpl extends UnicastRemoteObject implements ISayHello{

    /**
     * @throws RemoteException
     */
    protected SayHelloImpl() throws RemoteException {
    }

    /* (non-Javadoc)
     * @see com.jyxzz.home.rmi.ISayHello#sayHello(java.lang.String)
     */
    public String sayHello(String name) throws RemoteException {
        
        return "Hello lq -> "+name;
    }

}
View Code

 

上一篇:Spring_搭建过程中遇到的问题


下一篇:java – 远程RMI注册表