一个超级简单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(); } } }
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(); } } }
ISayHello
public interface ISayHello extends Remote{ public String sayHello(String name)throws RemoteException; }
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; } }