Reference:
Large file transfer with Java-rmi
Simple methods and examples of RMI remote method invocation
RMI is a remote method invocation protocol similar to RPC, which is simpler than RPC type WebService and can also be accessed across processes
To create a model or JavaBean, note that the class must implement serialization Serializable
public class Person implements Serializable {private int id; private String name; private int age;public void setId (int id) {this.id = ID;} public int getId () {return ID;} public void SetName (String name) {this.name = name;} Public String GetName () {return name;} public void Setage (int.) {this.age = age;} public int getage () {return age;}}
Calling interface
This is the interface that is called by the remote object and must inherit remote class public interface Personservice extends remote {public list<person> GetList () throws Rem Oteexception;}
Implement interfaces, note that you must inherit UnicastRemoteObject
public class Personserviceimpl extends UnicastRemoteObject implements Personservice {public Personserviceimpl () throws remoteexception {super ();//TODO auto-generated constructor stub} @Overridepublic list<person> GetList () throws remoteexception {//TODO auto-generated method StubSystem.out.println ("Get person start!"); List<person> personlist=new linkedlist<person> (); Person Person1=new person ();p erson1.setage (+);p erson1.setid (0);p erson1.setname ("Leslie");p Ersonlist.add (Person1 ); Person Person2=new person ();p erson2.setage (+);p Erson2.setid (1);p erson2.setname ("Rose");p Ersonlist.add (Person2); return personlist;}}
Server Side
public class Rmiserver{public static void Main (string[] args) {try {personservice personservice=new Personserviceim PL ();//Registered Communication Port Locateregistry.createregistry (6600);//Registered Communication path Naming.rebind ("Rmi://127.0.0.1:6600/personservice", Personservice); System.out.println ("Service start!");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Client
public class Rmiclient {public static void main (string[] args) {try{//Call remote object, note RMI path and interface must be consistent with server configuration Personserv Ice personservice= (Personservice) naming.lookup ("Rmi://127.0.0.1:6600/personservice"); List<person> personlist=personservice.getlist (); for (person person:personlist) {System.out.println ("ID:" +person.getid () + "Age:" +person.getage () + "Name:" +person.get Name ()); }}catch (Exception ex) {ex.printstacktrace (); } }}
Java RMI Remote Method invocation