Remote method invocation

from Wikipedia, the free encyclopedia

Remote Method Invocation ( RMI , German about "Calling remote method"), sometimes referred to as remote method invocation called, is calling a method of a remote Java - object and implements the Java own way of remote procedure call . “Remote” means that the object can be located in another Java Virtual Machine , which in turn can run on a remote computer or on the local computer. Here, the call to the calling object (or the programmer) looks the same as a local call, but have special exceptions ( exceptions ) are intercepted, which may, for example, signal a disconnect.

On the client side, the so-called stub takes care of the network transport. The stub must be available to the client either locally or over the network. Before the release of Java 2 Standard Edition (J2SE) in version 1.5.0, the stub was a class created with the RMI compiler rmic . From version 1.5 it is no longer necessary to call the RMI compiler rmic . The Java Virtual Machine is responsible for creating the stub .

Although remote objects can also be made available by a remote object that is already known in the program, the address of the server and an identifier (an RMI URL) are required for the first connection. For the identifier, a name service on the server returns a reference to the remote object. For this to work, the remote object must have previously registered with the name service in the server under this name. The RMI name service is java.rmi.Namingaddressed using static methods of the class . The name service is implemented as a stand-alone program and is called the RMI Registry .

Communication protocol

RMI also denotes a communication protocol based on TCP / IP and JRMP which is used for remote calls between Java objects, and a Java standard class library with which these calls can be implemented. This class library is part of the J2SE . Alternatively, IIOP can also be used as a communication protocol. Two ports are reserved for RMI . Port 1099 is reserved for the RMI registry, i.e. the name service. Port 1098 is reserved for the activator .


Remote interface
describes the functions that are available on the server and thus defines the behavior of the remote object (without implementing it).
Remote object
represents the remote object and is on the server. It implements the remote interface and the behavior of the remote methods available to the clients. One or more instances of the remote object can be created by the server. A remote object must be derived from UnicastRemoteObject and must have a parameterless constructor, because this only calls the constructor of UnicastRemoteObject and could otherwise throw a RemoteException. Every method must declare a RemoteException, including the parameterless constructor.
Remote Reference
is a reference to remote objects. The clients get the remote reference from the RMI registry.


The 4 steps at RMI
  1. The server registers a remote object with the RMI registry under a unique name.
  2. The client looks up this name in the RMI registry and receives an object reference that must correspond to its remote interface .
  3. The client calls a method from the object reference (that this method exists is guaranteed by the remote interface ). An object of a class X can be passed that is not yet known to the JVM of the server (this is possible if X implements an interface known to the server). In this case, the server JVM loads class X dynamically, for example from the client's web server.
  4. The server JVM executes the method on the remote object , using any dynamically loaded external code (e.g. methods from X), which is generally subject to security restrictions. The return values ​​of this call are sent to the client, or the client receives an error message (e.g. if the connection is aborted).


As a complement of RMI is the so-called activation ( "activation") are available. Similar to the RMI registry, this is also a central point of contact for RMI clients. However, the Activator can also create RMI server objects and start new virtual machines .

See also

Web links