RMI The concept of

RMI(Remote Method Invocation) Remote method call is a kind of communication mechanism between computers by using remote object to call each other . Use this mechanism , Objects on one computer can call objects on another computer to get remote data .RMI yes Enterprise JavaBeans The pillar of , It's about building a distributed system Java A convenient way for applications . in the past ,TCP/IP Socket communication is the main means of remote communication , But this development method does not use the object-oriented method to achieve development , When developing such a communication mechanism, programmers often feel bored , Regarding this RPC(Remote Procedure Call) emerge as the times require , It makes it easier for programmers to call remote programs , But in the face of complex communications ,RPC Still not very good support , and RPC Failed to achieve object-oriented call development mode . in the light of RPC Service legacy ,RMI In front of the world , It's designed as an object-oriented way of communication , Allows programmers to communicate using remote objects , And support multi-threaded Services , This is a revolution in telematics , A new milestone for telecommunication .

Java RMI A method of is called remotely (Remote Method Invocation). It's a mechanism , Be able to make in some Java The object on the virtual machine calls another Java Methods on objects in virtual machine . Any object that can be called with this method must implement the remote interface .

RMI Development steps

  1. First, create a remote interface and declare a remote method , Note that this is the interface for communication between the two sides , Need to inherit Remote
  2. Develop a class to implement remote interface and remote method , It's worth noting that implementing classes requires inheritance UnicastRemoteObject
  3. adopt javac Command compile file , adopt java -server Command registration service , Start the remote object
  4. Finally, the client looks up the remote object , And call the remote method

Example source code :

First of all, build a Model layer , Note that this object needs to be remotely transmitted , So you have to inherit Serializable

package com.blankjor.rmi;
import java.io.Serializable;
/**
* @desc Transport persistence layer calls data objects , Must be realized Serializable Interface
* @author Blankjor
* @date 2017 year 5 month 30 Japan Afternoon 3:28:45
*/
public class EntityData implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String tel; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getTel() {
return tel;
} public void setTel(String tel) {
this.tel = tel;
} }

Create a remote interface MyRemote, Note that the remote interface must inherit Remote

package com.blankjor.rmi;
import java.rmi.Remote;
import java.rmi.RemoteException; /**
* @desc Remote interface , Realization Remote
* @author Blankjor
* @date 2017 year 5 month 30 Japan Afternoon 2:49:05
*/
public interface MyRemote extends Remote {
EntityData sayHello() throws RemoteException; }

establish MyRemoteImpl Implement remote interface MyRemote, Note that this is the remote object implementation class , Need to inherit UnicastRemoteObject,UnicastRemoteObject The required remote interface is realized

package com.blankjor.rmi;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject; /**
* @desc Interface implementation , In order to accomplish some remote functions , With the help of UnicastRemoteObject, automatically
* @author Blankjor
* @date 2017 year 5 month 30 Japan Afternoon 2:50:39
*/
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote { /**
* Must be realized , In order to throw out RemoteException
*
* @throws RemoteException
*/
protected MyRemoteImpl() throws RemoteException {
} @Override
public EntityData sayHello() throws RemoteException {
EntityData ed = new EntityData();
ed.setId(1);
ed.setName("lyc");
ed.setTel("110");
return ed;
} }

Set up a remote server , And register after opening

package com.blankjor.rmi;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry; /**
* @desc Register the remote service server
* @author Blankjor
* @date 2017 year 5 month 30 Japan Afternoon 2:54:26
*/
public class MyRemoteServer {
public static void main(String[] args) {
try {
System.out.println(" The remote service is starting , Wait for the call ...");
// Instantiate the remote service object
MyRemote remote = new MyRemoteImpl();
// Register binding
LocateRegistry.createRegistry(1234);
Naming.rebind("rmi://localhost:1234/RemoteHello", remote);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Results after operation :

Client access to remote services , call

package com.blankjor.rmi;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException; /**
* @desc The client that invokes the remote service
* @author Blankjor
* @date 2017 year 5 month 30 Japan Afternoon 3:00:02
*/
public class MyRemoteClient {
public static void main(String[] args) {
try {
// Looking for remote services
MyRemote service = (MyRemote) Naming.lookup("rmi://localhost:1234/RemoteHello");
// Call the remote service method
EntityData ed = service.sayHello();
System.out.println("Welcome " + ed.getName());
} catch (MalformedURLException | RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
}

Results after operation :

summary :
  From the above process ,RMI To the server IP Address and port dependency is very close , But at the time of development, I didn't know the future server IP And how about the port , But the client program depends on this IP And port .
This is also RMI One of the limitations of . There are two ways to solve this problem : One is to pass. DNS To solve , The second is to encapsulate IP Exposed to program code .
  RMI The second limitation of is RMI yes Java Remote call of language , The programming language at both ends must be Java Realization , For communication between different languages, consider using Web Service Or the common object request broker system (CORBA) To achieve .

Reference resources :http://www.cnblogs.com/leslies2/archive/2011/05/20/2051844.html

 http://lavasoft.blog.51cto.com/62575/91679/

java rmi More articles on remote method call instances

  1. Java RMI( Remote method call ) Examples and analysis ( turn )

    Purpose : Through this paper , Can deepen right Java RMI The understanding of the , Know how it works , How to use, etc . And to deepen my own understanding , So it's written . deficiencies , I would also like to point out that . Conceptual explanation : RMI(RemoteMethodInvocat ...

  2. Java RMI( Remote method call ) Examples and analysis

    Purpose : Through this paper , Can deepen right Java RMI The understanding of the , Know how it works , How to use, etc . And to deepen my own understanding , So it's written . deficiencies , I would also like to point out that . Conceptual explanation : RMI(RemoteMethodInvocat ...

  3. JAVA RMI Simple examples of remote method calls [ turn ]

    RMI The concept of RMI(Remote Method Invocation) Remote method call is a kind of communication mechanism between computers by using remote object to call each other . Use this mechanism , An object on one computer can call another a ...

  4. JAVA RMI Simple examples of remote method calls ( Reprint )

    source :http://www.cnblogs.com/leslies2/archive/2011/05/20/2051844.html RMI The concept of RMI(Remote Method Invocati ...

  5. Java RMI The simplest example

    IHello.java import java.rmi.Remote; import java.rmi.RemoteException; public interface IHello extends ...

  6. Java Of RMI Remote method call implementation and application

    Studying recently Dubbo,RMI It's a very important underlying mechanism ,RMI(Remote Method Invocation) Remote method call is a kind of communication mechanism between computers by using remote object to call each other . Use this mechanism , A certain ...

  7. Java RMI Introduction and examples as well as Spring Yes RMI Supported practical application examples

    RMI  Related knowledge RMI The full name is Remote Method Invocation- Remote method call ,Java RMI stay JDK1.1 Implemented in , Its power lies in its powerful ability to develop distributed network applications , Is pure Java Net of ...

  8. Java RMI frame _ Remote method call (2016-08-16)

    Concept : Java RMI A method of is called remotely (Remote Method Invocation). It's a mechanism , Be able to make in some Java The object on the virtual machine calls another Java Methods on objects in virtual machine . can ...

  9. ( turn ) Java RMI frame ( Remote method call )

    " Original works , Allowed to reprint , When reprinting, please make sure to indicate the article in the form of hyperlink   Original source  . Author information and this statement . Otherwise, the legal liability will be investigated .http://haolloyin.blog.51cto.com/1177454/33 ...

Random recommendation

  1. trie Tree model

    Can be used to express all 0,1 choice .. Or the expression of a multistage finite character set

  2. stay Win Server 2012 Install in .NET Framework 3.5 The problem of

    stay Windows Server 2012 Installation on SQL Server 2012 when , Tips Enable Windows function NetFx3 An error occurred when , Error code :-2146498298. Please try from Windows ...

  3. About cocos2d-x And cocos2d-html5 Thinking about resource preloading

    Mobile Resource preloading , When you need to load it , Load from local disk to memory , When texture is not needed , It's forced to clean up the texture in memory : cc.TextureCache.getInstance().removeAllTex ...

  4. 2013 ACM Tonghua Invitational Competition A. Tutor

    A. Tutor Description Lilin was a student of Tonghua Normal University. She is studying at University ...

  5. GUID Widespread use

    GUID(Global unique identifier) Globally unique identifier , It is determined by the identification number on the network card ( Each network card has a unique identification number ) as well as CPU The only number generated by the clock 16 The binary value of a byte . GUID ...

  6. Unknown width and height image is horizontally and vertically centered in div

    <BODY> <div class="box"> <span class="car"></span> <i ...

  7. HashiCorp Vault Introduce

    HashiCorp Vault Is an enterprise class private information management tool . Speaking of Vault, I have to mention its Creator HashiCorp company .HashiCorp It's a family that focuses on DevOps Tool chain companies , Its star products include Vag ...

  8. github and gitlab Coexist

    Originally configured in your own computer github Global variable of , Just play with it github And code cloud projects . Now use your own computer to develop company projects , The company project uses gitlab, Results when pulling the code, you also need to manually enter the user name and password , How many more times can this be tolerated , but ...

  9. MT【6】 The limit of the sum of an equal ratio sequence

    review : The sum formula of equal ratio sequence is used to give the sequence , And then use the method of disprovement to prove the uniqueness . This method of disprovement appears when we talk about elementary symmetric polynomials in higher algebra .

  10. ThinkPHP journal ( How to learn a skill well , The advantages and disadvantages of teaching videos and documents )

    ThinkPHP journal ( How to learn a skill well , The advantages and disadvantages of teaching videos and documents ) One . summary One sentence summary : The most common uses of the basics are just described in the instructional video , Those videos that are not commonly used will not introduce , Because the demand is small , So we still need to make a good reference ...