Java Remote Method Invocation (RMI)

Download Report

Transcript Java Remote Method Invocation (RMI)

Java Remote Method
Invocation
(RMI)
Kupás Levente
Introduction
Distributed computing has become very popular
in recent years for a few reasons.
Java
supports distributed computing in a variety of ways;
one of the simplest is known as Remote Method
Invocation (RMI).
RMI provides a way for Java programs to interact
with classes and objects working on different virtual
machines on other networked computers. From the
perspective of the client, objects on the server may
be referenced as if they were local.
Interfaces: The Heart of RMI
Specifically, in RMI, the definition of a remote
service is coded using a Java interface.
The Java interface does not contain executable code.
RMI supports two classes that implement the
same interface.
1. The first class is the implementation of the
behavior, and it runs on the server.
2. The second class acts as a proxy for the remote
service and it runs on the client.
This is shown in the following diagram:
The diagram
A client program makes method calls on the proxy
object, RMI sends the request to the remote JVM,
and forwards it to the implementation. Any return
values provided by the implementation are sent
back to the proxy and then to the client's program.
RMI Architecture Layers
Java RMI is comprised of three layers that
support the interface.
Layers
• The first layer is the Stub/Skeleton Layer. This layer
is responsible for managing the remote object
interface between the client and server.
• The second layer is the Remote Reference Layer
(RRL). This layer is responsible for managing the
"liveliness" of the remote objects. It also manages
the communication between the client/server and
virtual machine s, for remote objects.
• The third layer is the transport layer. This is the
actual network/communication layer that is used to
send the information between the client and server
over the wire. It is currently TCP/IP based.
TCP/IP
For any successfully communication the mutual language
condition is necessary.
The TCP is a higher-level protocol that manages state
and error correction automatically.
• TCP/IP provides a persistent, stream-based
connection between two machines based on an IP
address and port number at each end.
• The IP is 32 bites number. This number is individual
for any computer, but can be more IP number in same
computer (depending for network card).
• Usually a DNS name is used instead of an IP address.
In the current release of RMI, TCP/IP connections are
used as the foundation for all machine-to-machine
connections.
Remote Classes and Interfaces
A Remote class has two parts:
• The interface
• The class itself.
The Remote interface must have the following
properties:
1. The remote interface must be public.
2. The remote interface must extend the interface
java.rmi.Remote.
3. Each method in the remote interface must declare
java.rmi.RemoteException in its throws clause in addition to
any application-specific exceptions.
4. A remote object passed as an argument or return value
must be declared as the remote interface, not the
implementation class.
Remote Classes and Interfaces
The Remote class itself has the following
properties:
1. It must implement a Remote interface.
2. It should extend the
java.rmi.server.UnicastRemoteObject class. Objects of
such a class exist in the address space of the
server and can be invoked remotely..
3. It can have methods that are not in its Remote
interface. These can only be invoked locally.
Remote Classes and Interfaces
• The Server requires the definition of both the
Remote class and the Remote interface, but the
Client only uses the Remote interface.
If a remote object is being used remotely, its
type must be declared to be the type of the
Remote interface, not the type of the Remote
class.
Using RMI
In the next, we will build a simple RMI system in a step-by-step
fashion.
1.
2.
3.
4.
5.
6.
Assuming that the RMI system is already designed, we
take the following steps to build a system:
Write and compile Java code for interfaces
Write and compile Java code for implementation classes
Generate Stub and Skeleton class files from the
implementation classes
Write Java code for a remote service host program
Develop Java code for RMI client program
Install and run RMI system
Running the RMI System
• We need to start three consoles, one for the
server, one for the client, and one for the
RMIRegistry.
• Start with the Registry.
>rmiregistry
The rmiregistry command creates and starts a
remote object registry on the specified port
(default=1099) on the current host.
If all goes well, the registry will start running
and we can switch to the next console.
Running the RMI System
• In the second console start the server hosting the
AddServer, and enter the following:
>java AddServer
It will start, load the implementation into memory
and wait for a client connection.
• In the last console, start the client program.
>java AddClient
• That's it; we have created a working RMI system.
Even though we ran the three consoles on the
same computer, RMI uses your network stack and
TCP/IP to communicate between the three
separate JVMs. This is a full-fledged RMI system