Jdk5
New Version
RMI
Programming Guide
PrefaceSome time ago, I needed to provide remote access interfaces for one of my Java programs for other Java programs. Java programs can use many remote access technologies to achieve this requirement. Since my remote client is a Java program, I decided to use remote access technology like RMI. RMI is the fastest Remote Access Technology on the Java platform. The Spring framework provides a good tool for various remote access technologies, including RMI, so that we can easily publish RMI interfaces and access RMI remote objects. However, my Java program does not use the Spring framework. Therefore, I have studied how to use RMI technology in general Java programs. I have searched for some RMI materials. Based on the RMI data, building an RMI server requires a lot of code. This is too outrageous! A small demand actually requires so much energy. Later, I read Javadoc and found that all the online RMI materials are outdated. In JDK 5, the RMI technology has been significantly updated. Now, you can use the RMI class provided by JDK to publish the RMI service quite easily!
Body
RMI IntroductionRMI (Remote Method Invocation) is a technology that remotely calls Java methods. It is the earliest developed Java Remote Access Technology and the basis of remote access technologies such as EJB. In the past, there were six steps to use RMI technology: (1) defining and implementing parameters in remote interfaces (2) defining and implementing remote interfaces (3) Writing server code (4) write the client code (5) to generate stub and skeltion, package stub to the client jar, and package the skeltion to the server jar (6) to start rmiregistry, register the service to rmiregistry and run the code. This requires executing some command line programs, which is very complicated! This is not commensurate with a simple technology like RMI! Fortunately, in JDK 5, RMI was significantly updated. RMI has become a simple and lightweight technology! In JDK 5, dynamic proxy technology is used to dynamically generate stub and skeltion classes, saving a lot of effort. RMI creation and release have become very simple!
Create an RMI server and a Registry
1. registry interface
Java. RMI. Registry
Interface Registry
All super interfaces:Remote
public interface Registry
extends Remote
Registry
It is a remote interface for the simple remote object registry. It stores and obtains remote object references bound to any string name.
bind
,
unbind
And
rebind
The method is used to change the name binding in the registry,
lookup
And
list
Method used to query the current name binding. The registry interface is the registry of the RMI object. The client queries and obtains the RMI object through this registry.
2. locateregistry class
Java. RMI. Registry
Class locateregistry
java.lang.Object
java.rmi.registry.LocateRegistry
public final class LocateRegistry
extends Object
LocateRegistry
Gets a reference to the remote object registry that directs on a specific host (including a local host), or creates a remote object registry that accepts calls to a specific port.
Static Registry |
Createregistry(INT port) Create and export the Registry instance on the local host that accepts the specified port request. |
The createregistry () method of the locateregistry class to create and obtain a reference to the remote RMI object registry. Now, we have successfully created an RMI server and an object registry. Then, by calling the registry interface:
Void |
Rebind(String name, remote OBJ) Replace the name binding specified in this registry with the provided remote reference. |
We can publish the RMI object to the RMI server and store it in the Registry, which can be discovered and accessed by the RMI client. Now, the RMI server is ready!
Write RMI objectsRegistry interface method:
Void |
Rebind(String name, remote OBJ) Replace the name binding specified in this registry with the provided remote reference. |
This indicates that the RMI object must be an object that implements the remote interface. In fact, remote interfaces must be implemented for any remote access object in Java! However, objects that only implement the remote interface cannot be published as RMI objects.
Unicastremoteobject class
Java. RMI. Server
Class unicastremoteobject
java.lang.Object
java.rmi.server.RemoteObject
java.rmi.server.RemoteServer
java.rmi.server.UnicastRemoteObject
All Implemented interfaces:Serializable, remote
Directly known subclass:Activationgroup
public class UnicastRemoteObject
extends RemoteServer
Export the remote object with jrmp and obtain the stub that communicates with the remote object. For the following constructor and static
exportObject
Method. The stub of the remote object being exported is obtained as follows:
- If you use
The unicastremoteobject. exportobject (remote) method is used to export the remote object.rmic
Tools are pre-generated from remote object classes) and stub class instances are constructed as follows.
- The "root class" is determined based on the following conditions: If the remote object class is directly extended
Remote interface, the remote object class is the root class; otherwise, the root class is directly extendedRemote
The exclusive class of the remote object class of the interface.
- The name of the stub class to be loaded is connected with a suffix.
"_Stub"
Determine the binary name of the root class.
- Load the stub class according to the name of the Class Loader using the root class. The stub class must be extended.
Remotestub and must have a public constructor. The constructor has a typeRemoteref parameters.
O Finally, use
Remoteref constructs stub class instances.
The following describes how to use the unicastremoteobject class:
Static remote |
Exportobject(Remote OBJ, int port) Export remote objects using specific ports to receive incoming calls. |
The remote interface class can be generated as an Rmi object. Can be published to the RMI registry!
Generate the stub class of the RMI classThe following describes how to use the unicastremoteobject class:
Static remote |
Exportobject(Remote OBJ, int port) Export remote objects using specific ports to receive incoming calls. |
The remote object returned by this method can be published to the RMI registry. The dynamic proxy can automatically generate stub classes. In this way, as long as the unicastremoteobject. exportobject (Remote OBJ, int port) method is used to create the RMI object on the RMI registry, we do not need to worry about the generation of stub and skeltion classes. Note,
Static remotestub |
Exportobject(Remote OBJ) Use an anonymous port to export remote objects so that you can receive incoming calls. |
The RMI object created in this method. The client cannot automatically generate the stub class! Do not use this method. Please note that,
Static remote |
Exportobject(Remote OBJ, int port) Export remote objects using specific ports to receive incoming calls. |
When using this method, we should use 0 as the value of the second parameter. Otherwise, an error may occur!
RMI Best PracticesAfter reading the above text, do you think it is still a bit complicated? Next, let's look at how to compile the RMI program. You will find that RMI programming is really very simple!
Implement remote interfaceThe RMI release class must implement the remote interface. We can enable the extend remote interface implemented by our RMI class. This is the remote interface. Public Interface Remote {} can be seen, it is actually empty, nothing. It is only a tag, indicating that the remote interface is used to identify the interface that the method can call from a non-local virtual machine. Any remote object must implement this interface directly or indirectly. These methods can be used remotely only when they are specified in remote interfaces (interfaces that extend java. RMI. Remote. This type of tag interface can be used to achieve the same purpose. The annotation was not introduced into Java at the beginning, so there was such an empty interface! In addition, all methods that can be remotely accessed must throw a RemoteException. This is because remote calls may encounter errors such as network interruptions.
Class that can be released as RMIA class that can be released as RMI. It must have a constructor with null parameters, and the constructor must be declared to throw a RemoteException. We recommend that you write unicastremoteobject. exportobject (this, 0) as follows: that is, the constructor constructs an Rmi object that can dynamically generate a stub. Of course, you can not write it like this, but you have to write more code when registering the RMI object! In addition, all parameters of the Remote Call method must implement the serializable interface.
Publish RMI objectsThe following is my code for releasing the RMI object: // This is the port used by the RMI server. I used the configuration file string rmiport = config. getinstance (). get ("rmiport"); // create and start the RMI server on the specified port. Otherwise, enter // start rmiregistry // In the command line to manually start the RMI server registry = locateregistry. createregistry (New INTEGER (rmiport ). intvalue (); // I configured some RMI classes in the configuration file, each of which includes the Registry name and the full name map of the RMI class <string, string> rmis = config. getinstance (). getrmis (); set <map. entry <string, string> entrys = rmis. entryset (); // you can obtain all the data with the stack generator, and store the data in the RMI registry one by one! Iterator it = entrys. iterator (); While (it. hasnext () {map. entry <string, string> TMP = (map. entry <string, string>) it. next (); try {// create an Rmi object. You can dynamically generate the remote stub = (remote) class. forname (TMP. getvalue ()). newinstance (); // release the RMI object to the registry with the specified name. rebind (TMP. getkey (), Stub);} catch (instantiationexception e) {/**/e. printstacktrace ();} catch (illegalaccessexception e) {/**/e. printstacktrace ();} catch (classnotfoundexception e) {/**/e. printstacktrace ();} catch (RemoteException e) {/**/e. printstacktrace ();}}
Client accessing the RMI objectThe client that accesses the RMI object is also very simple. For example, // RMI server IP address string rmihost = config. getinstance (). Get ("rmihost"); // RMI server port string rmiport = config.
Getinstance(). Get ("rmiport"); // obtain the reference registry = locateregistry of the RMI server registry.
Getregistry(Rmihost,
NewINTEGER (rmiport). intvalue (); // find and obtain the instance of the RMI object. Iserverreqterminalconfigservice stub = (iserverreqterminalconfigservice) Registry. Lookup ("serverreqterminalconfigservice"); // call the method of objects on the remote RMI server! Stub. rmiremote (); That's easy!
ConclusionBefore jdk5 was released, using RMI was a very tedious task. Many programmers even regard EJB as an encapsulation of RMI and a simplified version of RMI! Now, RMI has been greatly improved. As the best Remote Access Technology with the best performance on the Java platform, RMI is now the simplest remote access technology, and should be widely used.