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

RmiServer

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

 

 

 

RmiClient

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

 

 

ISayHello

public interface ISayHello extends Remote{

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

 

SayHelloImpl

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

 

posted @ 2019-06-30 16:49  家有小壮壮、  阅读(151)  评论(0编辑  收藏  举报