Java RMI Remote Method invocation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.