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
- Create a Remote interface and declare a Remote method. Note that this is an interface for communication between the two parties.
- Develop a class to implement remote interfaces and remote methods. It is worth noting that the implementation class must inherit UnicastRemoteObject
- Use the javac command to compile the file, use the java-server command to register the service, and start the remote object
- 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 {