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

Implementation of RMI in Java

The document outlines the implementation of Remote Method Invocation (RMI) in Java, focusing on enabling method calls between objects on different JVMs through a client-server application. It details the RMI architecture, components, and provides a step-by-step algorithm for creating a remote interface, server, and client, along with source code examples for a Calculator application. The lab demonstrates the effectiveness of RMI in simplifying distributed communication and reinforces the concepts of remote method invocation.

Uploaded by

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

Implementation of RMI in Java

The document outlines the implementation of Remote Method Invocation (RMI) in Java, focusing on enabling method calls between objects on different JVMs through a client-server application. It details the RMI architecture, components, and provides a step-by-step algorithm for creating a remote interface, server, and client, along with source code examples for a Calculator application. The lab demonstrates the effectiveness of RMI in simplifying distributed communication and reinforces the concepts of remote method invocation.

Uploaded by

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

Implementation of RMI in Java

1. Objectives
• To understand and implement Remote Method Invocation (RMI) in Java for enabling
method calls between objects on different JVMs.

• To demonstrate distributed communication by developing a client-server application


using Java RMI.

2. Theory
Introduction
Remote Method Invocation (RMI) is a Java API that enables an object in one Java Virtual
Machine (JVM) to invoke methods on an object running in another JVM. It simplifies
distributed computing by allowing seamless communication between remote objects. At its
core, Java RMI is a system that simplifies the development of distributed Java applications.
Its primary purpose is to enable remote communication between Java programs, abstracting
away the intricate details of network programming.

RMI Architecture
The architecture of the RMI system is layered, providing a clear separation of concerns
and facilitating the complex interactions between distributed objects. The complete RMI
system is structured into four distinct layers, each responsible for a specific aspect of remote
communication:

• Application Layer: This is the topmost layer, encompassing the actual business
logic of both the client and server applications. On the server side, this layer contains
the concrete implementation of the remote objects and their methods. On the client
side, it represents the part of the application that initiates remote method calls and
processes their results.

• Proxy Layer (Stub/Skeleton Layer): This layer acts as the crucial intermedi-
ary between the client and the server. It consists of two primary components: the
Stub, which resides on the client side, and the Skeleton, which resides on the server

1
side. These proxies handle the marshalling and unmarshalling of data and the actual
invocation of remote methods.

• Remote Reference Layer (RRL): Positioned beneath the Proxy Layer, the RRL is
responsible for managing the references to remote objects. It handles the semantics of
remote object invocation, including the serialization (marshalling) of method param-
eters into a byte stream on the client side and the deserialization (unmarshalling) of
this binary data back into objects on the server side. The RRL effectively acts as a
communication link between the client-side and server-side RRLs.

• Transport Layer: This is the lowest layer of the RMI architecture, directly man-
aging the network connection between the client and the server. Its responsibilities
include establishing new connections, managing existing ones, and handling the low-
level transmission of data packets across the network.

Figure 1: RMI Architecture

RMI Components
Beyond the layered architecture, an RMI application fundamentally comprises three key
components that interact to facilitate remote method invocation: the RMI Server, the RMI
Client, and the RMI Registry.

• RMI Server: The RMI server is the component that hosts the remote objects. These
are instances of Java classes whose methods are designed to be invoked from other
JVMs. The server is responsible for creating these remote objects and then making
their references accessible to clients by binding them to the RMI registry.

2
• RMI Client: The RMI client is the program that seeks to utilize the services provided
by remote objects. It initiates communication by requesting a reference to a remote
object from the RMI registry. Once a reference is obtained, the client can then invoke
methods on that remote object, treating it much like a local object.

• RMI Registry: The RMI registry functions as a naming service, serving as a central-
ized directory for remote objects. RMI server programs use this service to register their
remote objects, associating them with unique names. Clients, in turn, use these names
to look up and obtain references to the desired remote objects. The registry typically
operates on a default port, 1099, and must be initiated before the server attempts to
bind any objects.

3. Algorithm
1. Define the Remote Interface
1. Import java.rmi.Remote and java.rmi.RemoteException.

2. Declare a public interface that extends Remote.

3. Define one or more method signatures meant to be called remotely.

4. Ensure each method throws RemoteException.

2. Implement the Remote Interface on the Server Side


1. Create a class that:

• Implements the remote interface.


• Extends UnicastRemoteObject.

2. Implement all remote methods declared in the interface.

3. Define a constructor that throws RemoteException.

4. In the main method of the server:

• Create an instance of the implementation class.


• Create or retrieve the RMI registry using LocateRegistry.createRegistry(port).
• Register the remote object with the registry using rebind(String name, Remote
obj).

3
3. Develop the RMI Client
1. Import java.rmi.registry.LocateRegistry and java.rmi.registry.Registry.

2. In the main method:

• Retrieve the registry using LocateRegistry.getRegistry(String host, int


port).
• Look up the remote object from the registry using lookup(String name).
• Cast the returned object to the remote interface type.
• Invoke the remote methods and process the returned results.

4. Source Code
Calculator.java

1 import java.rmi.Remote;
2 import java.rmi.RemoteException;
3

4 public interface Calculator extends Remote {


5 public double add(double a, double b) throws RemoteException;
6 public double subtract(double a, double b) throws RemoteException;
7 public double multiply(double a, double b) throws RemoteException;
8 public double divide(double a, double b) throws RemoteException;
9 public String getMessage() throws RemoteException;
10 }

CalculatorServer.java

1 import java.rmi.RemoteException;
2 import java.rmi.server.UnicastRemoteObject;
3 import java.rmi.registry.Registry;
4 import java.rmi.registry.LocateRegistry;
5

6 public class CalculatorServer extends


7 UnicastRemoteObject implements Calculator {

4
8 protected CalculatorServer() throws RemoteException {
9 super();
10 }
11

12 @Override
13 public double add(double a, double b) throws RemoteException {
14 System.out.println("Server: Adding " + a + " + " + b);
15 return a + b;
16 }
17

18 @Override
19 public double subtract(double a, double b) throws RemoteException {
20 System.out.println("Server: Subtracting " + a + " - " + b);
21 return a - b;
22 }
23

24 @Override
25 public double multiply(double a, double b) throws RemoteException {
26 System.out.println("Server: Multiplying " + a + " * " + b);
27 return a * b;
28 }
29

30 @Override
31 public double divide(double a, double b) throws RemoteException {
32 System.out.println("Server: Dividing " + a + " / " + b);
33 if (b == 0) {
34 throw new RemoteException("Division by zero is not allowed");
35 }
36 return a / b;
37 }
38

39 @Override
40 public String getMessage() throws RemoteException {
41 return "Hello from Remote Calculator Server!";
42 }

5
43 public static void main(String[] args) {
44 try {
45 // Create and export a remote object
46 CalculatorServer server = new CalculatorServer();
47

48 // Create and start the registry on port 1099


49 Registry registry = LocateRegistry.createRegistry(1099);
50

51 // Bind the remote object to the registry


52 registry.bind("Calculator", server);
53

54 System.out.println("Calculator Server is ready and


55 waiting for client requests...");
56 System.out.println("Server is running on port 1099");
57

58 } catch (Exception e) {
59 System.err.println("Server exception: " + e.toString());
60 e.printStackTrace();
61 }
62 }
63 }

CalculatorClient.java

1 import java.rmi.registry.LocateRegistry;
2 import java.rmi.registry.Registry;
3 import java.util.Scanner;
4

5 public class CalculatorClient {


6

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


8 try {
9 // Get the registry
10 Registry registry = LocateRegistry.getRegistry("localhost", 1099);
11

6
12 // Lookup the remote object
13 Calculator calculator = (Calculator) registry.lookup("Calculator");
14

15 // Test the remote methods


16 System.out.println("=== RMI Calculator Client ===");
17 System.out.println(calculator.getMessage());
18

19 Scanner scanner = new Scanner(System.in);


20

21 while (true) {
22 System.out.println("\nChoose an operation:");
23 System.out.println("1. Addition");
24 System.out.println("2. Subtraction");
25 System.out.println("3. Multiplication");
26 System.out.println("4. Division");
27 System.out.println("5. Exit");
28 System.out.print("Enter your choice: ");
29

30 int choice = scanner.nextInt();


31

32 if (choice == 5) {
33 System.out.println("Goodbye!");
34 break;
35 }
36

37 if (choice >= 1 && choice <= 4) {


38 System.out.print("Enter first number: ");
39 double num1 = scanner.nextDouble();
40 System.out.print("Enter second number: ");
41 double num2 = scanner.nextDouble();
42

43 double result = 0;
44 String operation = "";
45

46 switch (choice) {

7
47 case 1:
48 result = calculator.add(num1, num2);
49 operation = "Addition";
50 break;
51 case 2:
52 result = calculator.subtract(num1, num2);
53 operation = "Subtraction";
54 break;
55 case 3:
56 result = calculator.multiply(num1, num2);
57 operation = "Multiplication";
58 break;
59 case 4:
60 result = calculator.divide(num1, num2);
61 operation = "Division";
62 break;
63 }
64

65 System.out.println(operation + " Result: " + result);


66 } else {
67 System.out.println("Invalid choice! Please try again.");
68 }
69 }
70

71 scanner.close();
72

73 } catch (Exception e) {
74 System.err.println("Client exception: " + e.toString());
75 e.printStackTrace();
76 }
77 }
78 }

8
5. Output

Figure 2: Server Side Output

Figure 3: Client Side Output

9
6. Discussion
In this lab, we successfully implemented a distributed system using Java’s Remote Method
Invocation (RMI) framework. The system was designed with a remote interface, a server
implementing the remote methods, and a client capable of invoking these methods over the
network. The use of a Calculator example provided a clear and practical understanding of
how RMI enables distributed communication between Java Virtual Machines (JVMs). It
allowed us to focus on the RMI architecture—such as the creation of stubs, object binding
with the RMI registry, and method invocation—without the complexity of real-world busi-
ness logic. The exercise illustrated how RMI hides the underlying network communication,
allowing developers to concentrate on interface design and method implementation.

7. Conclusion
In conclusion, this lab reinforced the concept of remote method invocation using Java RMI.
The successful execution of remote methods proved the effectiveness of Java RMI in building
simple distributed applications.

10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy