Java RMI overview

The advantage of RMI is that you don't have to write network or I / O code yourself.

A client program invoking a remote method (where the real server is located) is the same as a normal method call to an object running on the client's own local JVM.

RMI calls the customer helper a stub, and the service helper a skeleton

Five steps:

1. Make remote interface

The remote interface defines methods that can be called remotely by the client. The client will use it as the class type of the service. Both stub and the actual service implement this interface.

Remote is a "token" interface, so remote does not have a method.

All declared methods throw RemoteException. The client will call the methods on the stub that implements the remote interface, and the underlying stub uses network and I / O, so all kinds of bad things may happen...

Determine whether the variable and return value belong to primitive type or serializable type. Because it is transmitted in the network, it needs to be serialized.

2. Implementation of remote production

A class that does actual work.

First, implement the myremote interface.

In order to become a remote service object, some "remote" functions are required. The simplest way is to extend Java rmi. server. Unicastremoteobject, let the superclass help you do these things.

Since the constructor of a superclass throws an exception, the class must also have a constructor.

3. Stub and skeleton generated by RMIC

Auxiliary classes for customers and services do not need to be created by yourself.

I use the project created by eclipse to execute RMIC package name under the bin directory of the project directory file name

Note that the file name is not appended class

Then you will find that a myremoteimpl will be generated under XXX \ bin \ package \_ Stub. Class file.

As the warning says, there is no skeleton generation, that is, there is no name myremoteimpl_ Skel. Class file.

4. Start RMI register (rmiregistry)

Under the bin directory of the directory where the JDK is installed, found rmiregistry Exe, but... I don't know how to use... Embarrassing... I kept reporting errors, and then gave up starting rmiregistry on the command line

Directly in the code through LocateRegistry createRegistry(12312); Create the rmirgistry service and specify the port number.

The remote object is generated first, and then through naming Rebind() binds the remote object to rmiresistry

5. Start remote service

Client through naming Lookup (the name used when registering) gets the service.

RMI remote call steps:

Steps 2-6 are completely transparent to the customer object

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
分享
二维码
< <上一篇
下一篇>>