Java rmi remote method call simple example

Source: Internet
Author: User

RMI Concept

Remote Method Invocation (RMI) Remote Method call is a communication mechanism in which computers use Remote object calls to communicate with each other. Using this mechanism, objects on a computer can call objects on another computer to obtain remote data. RMI is the pillar of Enterprise JavaBeans and a convenient way to build distributed Java applications. In the past, TCP/IP socket communication was the main means of remote communication, but this development method did not use the object-oriented method for development, when developing such a communication mechanism, programmers often feel bored. RPC (Remote Procedure Call) emerged, making it easier for programmers to Call Remote programs, however, in the face of complex information messaging, RPC is still not well supported, and RPC is not the development mode of object-oriented calling. RMI is designed as an Object-Oriented communication method that allows programmers to use remote objects for communication and supports multi-threaded services, this is a revolution in remote communication and a new milestone for remote communication.

RMI development steps

  1. Create a Remote interface and declare a Remote method. Note that this is an interface for communication between the two parties.
  2. Develop a class to implement remote interfaces and remote methods. It is worth noting that the implementation class must inherit UnicastRemoteObject
  3. Use the javac command to compile the file, use the java-server command to register the service, and start the remote object
  4. Finally, the client finds the remote object and calls the remote method.

Simple instance

First, create a Model layer for the service. Note that because the object needs to be remotely transmitted, it must inherit the Serializable

PackageRmi. model;

ImportJava. io. Serializable;

//Note that the object must inherit the Serializable
Public ClassPersonEntityImplementsSerializable {
Private IntId;
PrivateString name;
Private IntAge;

Public VoidSetId (IntId ){
This. Id=Id;
}

Public IntGetId (){
ReturnId;
}

Public VoidSetName (String name ){
This. Name=Name;
}

PublicString getName (){
ReturnName;
}

Public VoidSetAge (IntAge ){
This. Age=Age;
}

Public IntGetAge (){
ReturnAge;
}
}

Create the Remote interface PersonService. Note that the Remote interface must inherit the Remote

PackageRmi. service;

ImportJava. rmi. Remote;
ImportJava. rmi. RemoteException;
ImportJava. util. List;
ImportRmi. model.*;

//This is an interface called by a Remote object and must inherit the Remote class.
Public InterfacePersonServiceExtendsRemote {
PublicList<PersonEntity>GetList ()ThrowsRemoteException;
}

Create a PersonServiceImpl to implement a remote interface. Note that this is a remote object implementation class and must inherit UnicastRemoteObject

PackageRmi. serviceImpl;

ImportJava. rmi. RemoteException;
ImportJava. rmi. server. UnicastRemoteObject;
ImportJava. util. Collections list;
ImportJava. util. List;

ImportRmi. model. PersonEntity;
ImportRmi. service.*;

//This is the implementation class of the remote object and must inherit the UnicastRemoteObject
Public ClassPersonServiceImplExtendsUnicastRemoteObjectImplements PersonService {

PublicPersonServiceImpl ()ThrowsRemoteException {

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.