Simple use of RMI

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[simple use of RMI]

Hello, I'm Zhu Mingxing, a student of the 8th session of Shanghai Branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the task 8 of the Academy's official website, the knowledge point in deep thinking - the simple use of RMI

(1) Background:

The previous demo method calls are all made on the same Java virtual machine. The caller and the callee are on the same heap. Sometimes, some parts of the program have to run on the server, and the client executes on different computers. How to call methods of objects on different machines?

Use RMI (remote method invocation)

JDK provides a perfect, simple and easy-to-use remote call framework, which requires both client and server to be Java programs.

(2) Knowledge analysis:

Design of remote method call

Tasks of auxiliary facilities

Auxiliary facilities are actually objects that perform communication. The client calls the methods of auxiliary facilities as if the client were the server. The client auxiliary facility is not a real remote service. Although the auxiliary facility behaves like it (because it provides the same methods as the service declaration methods), it does not have any method logic required by the client. The auxiliary facility connects to the server, passes the call information to the server, and waits for the server's response.

The auxiliary facilities of the server parse the information sent by the client, and then call the real service.

After the server auxiliary facility obtains the return value, it is packaged and sent back to the auxiliary facility of the client. The client's auxiliary facilities unlock the objects that the information is passed to the client.

Procedure calling method

(3) Frequently asked questions:

What is the difference between a generic call and an RMI call?

(4) Solution:

For the client, although RMI method calls seem to be local, it is risky for the client auxiliary facilities to make calls through the network and throw exceptions. At the beginning of the client's local call, the agent will turn it into remote. The intermediate information is sent from the Java virtual machine to the Java virtual machine. It depends on the protocol adopted by the auxiliary facilities.

We only need the Java to Java part, so just use a simple RMI

(5) Coding practice:

MyRemote. java

/**

*Create remote interface

*The remote interface declares remote methods that can be accessed by the client

*1. The remote interface inherits Java rmi. Remote

*2. Declare that all methods will throw RemoteException

*/

public interface MyRemote extends Remote {

public String sayHello() throws remoteexception;

}

MyRemoteImpl. java

/**

*Remote service class

*1. Implement remote interface

*2. Inherit unicastremoteobject

*In order to become a remote service object, your object must have remote related functions. The simplest way is to inherit unicastremoteobject and let this parent class handle it.

*3. Write a parameterless constructor for RemoteException

*/

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{

@Override

public String sayHello() throws remoteexception {

return "Hello World!";

}

//The constructor also throws an exception

public MyRemoteImpl() throws remoteexception{}

}

MyRemoteServer. java

**

*Bind (string name, object obj): register an object and bind the object to the service name. If the service name is bound to another object, a namealreadyboundexception exception is thrown.

*Rebind (string name, object obj): register the object and bind the object to the service name. If the service name has been bound to other objects, the exception will not be thrown, but the new object will be bound to the service name.

*

*/

public class MyRemoteServer {

public static void main(String[] args) {

try {

//An instance of the remote object registry on the local host, default port 1099

Registry registry = LocateRegistry. createRegistry(1099);

//Create a remote object

MyRemote service = new MyRemoteImpl();

//Register the remote object on the RMI registration server and name it helloservice

registry. rebind("HelloService",service);

} catch (AccessException e) {

e.printStackTrace();

} catch (remoteexception e) {

e.printStackTrace();

}

System. out. Println ("service program start!");

}

}

Start the server console printing:

**

*Lookup (string name): finds an object and returns an object with the same name as the specified one.

*/

public class MyRemoteClient {

public static void main(String[] args) {

new MyRemoteClient(). go();

}

public void go(){

try {

MyRemote service = (MyRemote) Naming. lookup(" rmi://127.0.0.1:1099/HelloService ");

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>