UNIT2JV
UNIT2JV
Remote Method Invocation (RMI) is a Java API that allows an object running in one Java Virtual
Machine (JVM) to invoke methods on an object running in another JVM. This capability is essential
for building distributed applications, where components may be located on different machines or
networks.
Object-Oriented: RMI allows you to invoke methods on remote objects as if they were local.
Location Transparency: The client does not need to know the physical location of the remote
object.
Support for Multiple Protocols: RMI can work over various transport protocols, including
TCP.
RMI is widely used in distributed computing for various applications, such as:
Distributed Databases: RMI can be used to access and manipulate data stored in a remote
database.
Remote Services: RMI enables the creation of remote services that can be accessed by
clients over a network.
In RMI architecture, the RMI Registry acts as a name server that allows clients to look up remote
objects by name. The Server creates and registers remote objects with the registry, making them
available for clients. The Client retrieves these objects from the registry and invokes methods on
them, enabling remote communication. ### RMI Architecture
RMI (Remote Method Invocation) architecture consists of several key components that work
together to facilitate remote communication between Java applications. The main components
include the RMI Registry, Server, and Client.
1. RMI Registry
Name Server: The RMI Registry acts as a directory service where remote objects are
registered and can be looked up by clients.
Binding Remote Objects: The server binds remote objects to names in the registry, allowing
clients to retrieve them using these names.
Facilitating Lookup: Clients use the registry to look up remote objects by name, obtaining a
reference to the remote object for method invocation.
2. Server
Remote Object Creation: The server creates instances of remote objects that implement
remote interfaces.
Registration: It registers these remote objects with the RMI Registry, making them accessible
to clients.
Listening for Requests: The server listens for incoming requests from clients and processes
method invocations on the remote objects.
3. Client
Remote Object Lookup: The client queries the RMI Registry to obtain a reference to a
remote object by its name.
Method Invocation: Once the client has a reference to the remote object, it can invoke
methods on it as if it were a local object.
Handling Responses: The client receives responses from the server after method
invocations, allowing it to process results or handle exceptions.
Summary of Interaction
The Server registers the remote object with the RMI Registry.
The Client invokes methods on the remote object, and the Server processes these requests,
returning results to the Client.
This architecture enables seamless communication between distributed Java applications, allowing
them to interact as if they were local, while handling the complexities of network communication
and object serialization.
Create a remote interface that declares the methods that can be invoked remotely.
Hello.java
java
RunCopy code
1import java.rmi.Remote;
2import java.rmi.RemoteException;
HelloImpl.java
java
RunCopy code
1import java.rmi.server.UnicastRemoteObject;
2import java.rmi.RemoteException;
7 super();
8 }
10 @Override
13 }
14}
Create a server that registers the remote object with the RMI registry.
Server.java
java
RunCopy code
1import java.rmi.registry.LocateRegistry;
2import java.rmi.registry.Registry;
6 try {
7 // Create an instance of the remote object
12
14 registry.rebind("Hello", hello);
15
16 System.out.println("Server is ready.");
17 } catch (Exception e) {
19 e.printStackTrace();
20 }
21 }
22}
Create a client that looks up the remote object in the RMI registry and invokes its method.
Client.java
java
RunCopy code
1import java.rmi.registry.LocateRegistry;
2import java.rmi.registry.Registry;
6 try {
12
16 } catch (Exception e) {
18 e.printStackTrace();
19 }
20 }
21}
Open a terminal or command prompt and navigate to the directory where you saved the Java files.
Compile the Java files using the following command:
bash
RunCopy code
Before running the server, you need to start the RMI registry. Open a terminal and run the following
command:
bash
RunCopy code
1rmiregistry
Make sure to run this command in the same directory where your compiled classes are located, or
specify the classpath accordingly.
bash
RunCopy code
1java Server
RunCopy code
1Server is ready.
bash
RunCopy code
1java Client
In RMI, parameters passed to remote methods must be serializable. This includes primitive types,
arrays, and objects that implement the Serializable interface. When a remote method is invoked, the
RMI system marshals the parameters into a format suitable for transmission over the network.
For example, if you have a remote method that takes a String and an int as parameters, you would
define it in your remote interface and implement it in your server class. The client would then call
this method, passing the required parameters. ### Example of Passing Parameters in RMI
Calculator.java
java
RunCopy code
1import java.rmi.Remote;
2import java.rmi.RemoteException;
8}
CalculatorImpl.java
java
RunCopy code
1import java.rmi.server.UnicastRemoteObject;
2import java.rmi.RemoteException;
7 super();
8 }
10 @Override
12 return a + b;
13 }
14
15 @Override
18 }
19}
Create a server that registers the remote object with the RMI registry.
CalculatorServer.java
java
RunCopy code
1import java.rmi.registry.LocateRegistry;
2import java.rmi.registry.Registry;
6 try {
12
14 registry.rebind("Calculator", calculator);
15
17 } catch (Exception e) {
19 e.printStackTrace();
20 }
21 }
22}
Create a client that looks up the remote object in the RMI registry and invokes its methods.
CalculatorClient.java
java
RunCopy code
1import java.rmi.registry.LocateRegistry;
2import java.rmi.registry.Registry;
6 try {
12
13 // Call the remote methods
16
19 } catch (Exception e) {
21 e.printStackTrace();
22 }
23 }
24}
Parameters: The add method takes two integers as parameters, while the greet method
takes a String.
Serialization: Both parameters are serialized when sent over the network, allowing the
server to receive them correctly.
Return Values: The server processes the parameters and returns the results back to the
client, demonstrating the passing of parameters in remote method invocations.
The rmiregistry command is a crucial component of Java's Remote Method Invocation (RMI)
architecture. It acts as a name server that allows remote objects to be registered and looked up by
clients. The RMI registry facilitates the binding of remote objects to names, enabling clients to find
and invoke methods on these objects without needing to know their physical location.
Purpose of rmiregistry
1. Name Registration: The RMI registry allows server applications to register remote objects
with a specific name. This name can then be used by clients to look up the remote object.
2. Object Lookup: Clients can query the RMI registry to obtain a reference to a remote object
by its name. This simplifies the process of accessing remote services.
3. Decoupling: By using a registry, clients and servers are decoupled. Clients do not need to
know the details of the server's implementation or its network address; they only need to
know the name of the remote object.
1. Start the RMI Registry: You can start the RMI registry by running the rmiregistry command
in the terminal. By default, it listens on port 1099.
2. Register Remote Objects: The server application registers its remote objects with the
registry using the rebind method.
3. Lookup Remote Objects: The client application looks up the remote object in the registry
using the lookup method.
Handling exceptions in RMI (Remote Method Invocation) is crucial for ensuring that your distributed
application can gracefully manage errors that may occur during remote method calls. In RMI,
exceptions can arise from various sources, such as network issues, remote object unavailability, or
method invocation errors.
1. RemoteException: This is the superclass for all exceptions that can occur during a remote
method call. Any method in a remote interface must declare that it
throws RemoteException.
2. Specific Exceptions: You can also define and throw specific exceptions in your remote
methods. These exceptions should be serializable.
3. Client-Side Handling: The client should handle exceptions that may occur during the lookup
of remote objects or during method invocation.
4. Server-Side Handling: The server should handle exceptions that may occur during the
processing of remote method calls.