[go: up one dir, main page]

0% found this document useful (0 votes)
11 views10 pages

UNIT2JV

Remote Method Invocation (RMI) is a Java API that enables method invocation on objects across different Java Virtual Machines, facilitating the development of distributed applications. Key features include object-oriented access, location transparency, automatic marshalling, and support for multiple protocols. The architecture consists of components like the RMI Registry, Server, and Client, allowing seamless remote communication and method invocation while managing network complexities.

Uploaded by

Rajnish Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

UNIT2JV

Remote Method Invocation (RMI) is a Java API that enables method invocation on objects across different Java Virtual Machines, facilitating the development of distributed applications. Key features include object-oriented access, location transparency, automatic marshalling, and support for multiple protocols. The architecture consists of components like the RMI Registry, Server, and Client, allowing seamless remote communication and method invocation while managing network complexities.

Uploaded by

Rajnish Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Remote Method Invocation (RMI)

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.

Key Features of RMI

 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.

 Automatic Marshalling: RMI handles the serialization and deserialization of objects


automatically, allowing complex data types to be passed between JVMs.

 Support for Multiple Protocols: RMI can work over various transport protocols, including
TCP.

Use of RMI in Distributed Computing

RMI is widely used in distributed computing for various applications, such as:

 Client-Server Applications: RMI allows clients to access services provided by a server


remotely.

 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.

Roles of the RMI Components

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 Client looks up a remote object in the RMI Registry.

 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.

Step 1: Define the Remote Interface

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;

4// Remote interface

5public interface Hello extends Remote {

6 String sayHello() throws RemoteException;


7}

Step 2: Implement the Remote Interface

Create a class that implements the remote interface.

HelloImpl.java

java

RunCopy code

1import java.rmi.server.UnicastRemoteObject;

2import java.rmi.RemoteException;

4// Implementation of the remote interface

5public class HelloImpl extends UnicastRemoteObject implements Hello {

6 protected HelloImpl() throws RemoteException {

7 super();

8 }

10 @Override

11 public String sayHello() throws RemoteException {

12 return "Hello, World!";

13 }

14}

Step 3: Create the RMI Server

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;

4public class Server {

5 public static void main(String[] args) {

6 try {
7 // Create an instance of the remote object

8 HelloImpl hello = new HelloImpl();

10 // Create the RMI registry on port 1099

11 Registry registry = LocateRegistry.createRegistry(1099);

12

13 // Bind the remote object to the registry

14 registry.rebind("Hello", hello);

15

16 System.out.println("Server is ready.");

17 } catch (Exception e) {

18 System.err.println("Server exception: " + e.toString());

19 e.printStackTrace();

20 }

21 }

22}

Step 4: Create the RMI Client

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;

4public class Client {

5 public static void main(String[] args) {

6 try {

7 // Get the registry

8 Registry registry = LocateRegistry.getRegistry("localhost", 1099);

10 // Look up the remote object


11 Hello stub = (Hello) registry.lookup("Hello");

12

13 // Call the remote method

14 String response = stub.sayHello();

15 System.out.println("Response from server: " + response);

16 } catch (Exception e) {

17 System.err.println("Client exception: " + e.toString());

18 e.printStackTrace();

19 }

20 }

21}

Step 5: Compile the Java Files

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

1javac Hello.java HelloImpl.java Server.java Client.java

Step 6: Start the RMI Registry

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.

Step 7: Run the RMI Server

In a new terminal window, run the server:

bash

RunCopy code

1java Server

You should see the output:

RunCopy code
1Server is ready.

Step 8: Run the RMI Client

In another terminal window, run the client:

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

Step 1: Define the Remote Interface

Create a remote interface that declares the method with parameters.

Calculator.java

java

RunCopy code

1import java.rmi.Remote;

2import java.rmi.RemoteException;

4// Remote interface

5public interface Calculator extends Remote {

6 int add(int a, int b) throws RemoteException;

7 String greet(String name) throws RemoteException;

8}

Step 2: Implement the Remote Interface

Create a class that implements the remote interface.

CalculatorImpl.java

java

RunCopy code

1import java.rmi.server.UnicastRemoteObject;
2import java.rmi.RemoteException;

4// Implementation of the remote interface

5public class CalculatorImpl extends UnicastRemoteObject implements Calculator {

6 protected CalculatorImpl() throws RemoteException {

7 super();

8 }

10 @Override

11 public int add(int a, int b) throws RemoteException {

12 return a + b;

13 }

14

15 @Override

16 public String greet(String name) throws RemoteException {

17 return "Hello, " + name + "!";

18 }

19}

Step 3: Create the RMI Server

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;

4public class CalculatorServer {

5 public static void main(String[] args) {

6 try {

7 // Create an instance of the remote object

8 CalculatorImpl calculator = new CalculatorImpl();


9

10 // Create the RMI registry on port 1099

11 Registry registry = LocateRegistry.createRegistry(1099);

12

13 // Bind the remote object to the registry

14 registry.rebind("Calculator", calculator);

15

16 System.out.println("Calculator Server is ready.");

17 } catch (Exception e) {

18 System.err.println("Server exception: " + e.toString());

19 e.printStackTrace();

20 }

21 }

22}

Step 4: Create the RMI Client

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;

4public class CalculatorClient {

5 public static void main(String[] args) {

6 try {

7 // Get the registry

8 Registry registry = LocateRegistry.getRegistry("localhost", 1099);

10 // Look up the remote object

11 Calculator stub = (Calculator) registry.lookup("Calculator");

12
13 // Call the remote methods

14 int sum = stub.add(5, 10);

15 String greeting = stub.greet("Alice");

16

17 System.out.println("Sum: " + sum);

18 System.out.println("Greeting: " + greeting);

19 } catch (Exception e) {

20 System.err.println("Client exception: " + e.toString());

21 e.printStackTrace();

22 }

23 }

24}

Summary of Parameter Passing

 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.

How to Use rmiregistry

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.

Key Points for Exception Handling in RMI

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.

You might also like