0% found this document useful (0 votes)
106 views8 pages

Unit8 RMI and COBRA

This document provides an introduction to RMI (Remote Method Invocation) in Java. RMI allows Java objects to communicate and invoke methods on objects located in a different JVM or machine. It enables building distributed Java applications where parts of the application run on different machines. RMI provides a secure and reliable way for distributed objects to communicate but can be slow for high-performance systems. RMI uses stubs on the client-side that act as a representative for the remote object. The skeleton on the server-side receives remote method invocations and dispatches them to the actual remote object. Writing RMI applications involves defining a remote interface, implementing it, creating server/client programs, starting the RMI registry, and running the

Uploaded by

Prabesh Karki
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)
106 views8 pages

Unit8 RMI and COBRA

This document provides an introduction to RMI (Remote Method Invocation) in Java. RMI allows Java objects to communicate and invoke methods on objects located in a different JVM or machine. It enables building distributed Java applications where parts of the application run on different machines. RMI provides a secure and reliable way for distributed objects to communicate but can be slow for high-performance systems. RMI uses stubs on the client-side that act as a representative for the remote object. The skeleton on the server-side receives remote method invocations and dispatches them to the actual remote object. Writing RMI applications involves defining a remote interface, implementing it, creating server/client programs, starting the RMI registry, and running the

Uploaded by

Prabesh Karki
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/ 8

Introduction to RMI (Remote Method Invocation)

RMI (Remote Method Invocation) is a Java API that provides a mechanism to create
distributed application in Java. RMI allows Java programs (of a single java application)
running on different machines to talk to each other and work together as if they were
all running on the same machine. This is useful for building distributed applications
where different parts of the application run on different machines and need to
communicate with each other.
RMI allows objects to communicate and interact with each other in a distributed
environment, such as a network. RMI enables Java objects to invoke methods on
objects located on another machine.
RMI provides a secure and reliable way to communicate between distributed objects
but however, RMI can be slow and may not be suitable for high-performance
distributed systems.

RMI Stub and Skeleton


In Java, a "stub" and a "skeleton" are related to RMI technology, which allows Java
objects to communicate across network.

Stub
Stub in RMI (Remote Method Invocation) is a client-side object that acts as a
representative for a remote object located on a different machine (server). A "stub" is
a local representative of a remote object that resides on a different Java Virtual
Machine (JVM) of client and can be used to invoke methods on the remote object.
When a client program wants to access a remote object, it obtains a stub for that
object from a registry or naming service. The stub is responsible for making the remote
method call and returning the result back to the client program.
When a client program wants to use/access a remote object, it first obtains a reference
to the remote object using a naming service. The reference returned is a stub object
that acts as a representative for that remote object. The client then uses this stub
object to invoke methods on the remote object as like if it was a local object. The stub
object intercepts or takes over the method calls made by the client to the remote
object and forwards them over the network to the server where the actual object
resides. The results are then sent back to the client through the stub object.
The stub object hides the details of network communication between client and server
machine from the client, making it easy for the client to interact with the remote object
without worrying about the underlying network protocols.

Skeleton
A "skeleton" is a server-side object that receives remote method invocations from the
client and dispatches them to the actual remote object lying on server that the client
wants to access. The skeleton object is responsible for receiving the method call,
unmarshalling the parameters, invoking the method on the remote object, marshalling
the result, and sending it back to the client.
It does the tasks like:
- Receiving the remote method call from client and reading the parameters and
unmarshalling them.
- Invoking the remote method on actual remote object.
- Marshalling and sending back the result to the client.
RMI Architecture

Writing RMI application and running it


To run a Java RMI (Remote Method Invocation) application, you need to follow these
steps:

1. Define the remote interface: Define a Java interface that extends the Remote
class and declares the methods that will be remotely accessible.
2. Implement the remote interface: Implement the remote interface in a Java class
that provides the actual implementation of the methods declared in the remote
interface.

3. Create a server program: Write a Java program that creates an instance of the
remote object and registers it with the RMI registry.

4. Start the RMI registry: Start the RMI registry on the server machine using the
"rmiregistry" command.

5. Create a client program: Write a Java program that obtains a reference to the
remote object from the RMI registry and invokes its methods.

6. Compile the programs: Compile the server and client programs using the "javac"
command.

7. Start the server program: Start the server program on the server machine using
the "java" command.

8. Run the client program: Run the client program on the client machine using the
"java" command.

9. Check the output: Check the output of the client program to verify that the
remote methods were invoked successfully.

It is important to note that when running a Java RMI application, the server and client
programs should be executed on different machines. This is because RMI is designed to
allow objects to be accessed remotely over a network.
Example:
Here's a step-by-step guide to creating an RMI (Remote Method Invocation) application
in Java that calculates the factorial of a number sent by a client:

1. Create two packages: one for the client and one for the server. For this example,
we'll call them "client" and "server".

2. In the "server" package, create an interface called "FactorialInterface" that will


define the remote method to calculate the factorial. Here's the code:

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface FactorialInterface extends Remote {


public int factorial(int n) throws RemoteException;
}
This interface extends the "Remote" interface, which is a marker interface that
indicates that any implementation of this interface can be accessed remotely.
3. In the same package, create a class called "FactorialServer" that will implement
the "FactorialInterface". Here's the code:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class FactorialServer extends UnicastRemoteObject implements


FactorialInterface {
public FactorialServer() throws RemoteException {
super();
}

public int factorial(int n) throws RemoteException {


int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
This class extends the "UnicastRemoteObject" class, which provides the
functionality to export the object and make it remotely accessible. The "factorial"
method simply calculates the factorial of the given number and returns it.

4. In the "client" package, create a class called "FactorialClient" that will create a
connection to the server and call the remote "factorial" method. Here's the code:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class FactorialClient {


public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost");
FactorialInterface factorial = (FactorialInterface)
registry.lookup("Factorial");
int result = factorial.factorial(5);
System.out.println("Factorial of 5 is: " + result);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
This class uses the "LocateRegistry.getRegistry" method to get a reference to
the registry on the server. Then, it uses the "lookup" method to get a reference
to the "Factorial" object (which is an instance of the "FactorialServer" class).
Finally, it calls the "factorial" method on this object and prints the result.

5. Compile both the server and client classes and run them.
Old questions
(only one 5 marks questions asked in majority of question set. So, one 5 marks
question coming is chance).
i. What is RMI? Differentiate it with CORBA? (5)[2071][2072][2075]
ii. Write short note on: a. RMI architecture (2.5)[2072] [2073]
iii. What is CORBA? How is it different from RMI? (2+3) [2074]
iv. What are different layers of RMI architecture? Explain. (5)[2076]
v. Describe the process to run the RMI application. [5] [2075]
vi. What is the significance of stub and skeleton In RMI? Create a RMI application
such that a client sends an Integer number to the server and the server return
the factorial value of that integer. Give a clear specification for every step. (10)
[2077]
vii. Explain RMI architecture layers in detail. Write a Java programs using RMI to
find product of two numbers. (4+6) [2078]
viii. Why CORBA is important? Compare CORBA with RMI. (3+2) [2078]
ix. How CORBA differs from RMI? Discuss the concepts of IDL briefly. (2 + 3)
[model question]

BSC Csit Model Question Paper Advanced Java


Programming Model Questions BSC CSIT
Group ‘A’
Attempt any TWO Questions. (2 × 10 = 20)
1. What are the uses of final modifier? Explain each use of the modifier with
suitable example. (1 + 9)
2. Write a java program to create login form with user id, password, ok button,
and cancel button. Handle key events such that pressing ‘l’ performs login and
pressing ‘c’ clears text boxes and puts focus on user id text box. Assume user
table having fields Uid and Password in the database named account. (10)
3. Discuss various scopes of JSP objects briefly. Create a HTML file with principal,
time and rate. Then create a JSP file that reads values from the HTML form,
calculates simple interest and displays it. (4 + 6)
Group ‘B’
Attempt any EIGHT Questions. (8 × 5 = 40)

4. Write a java program that writes objects of Employee class in the file named
emp.doc. Create Employee class as of your interest. (5)
5. What are layout managers? Explain Gridbag layout with suitable example. (1 +
4)
6. What is the use of action command in event handling? Explain with example. (1
+ 4)
7. What causes SQL exception? How it can be handled? Explain with example. (1
+ 4)
8. Write a java program using TCP such that client sends number to server and
displays its factorial. The server computes factorial of the number received
from client. (5)
9. How JavaFx differs from Swing? Explain steps of creating GUI using javaFx. (2 +
3)
10.What are different ways of writing servlet programs? Write a sample Servlet
program using any one way. (1 + 4)
11.How CORBA differs from RMI? Discuss the concepts of IDL briefly. (2 + 3)
12.When thread synchronization is necessary? Explain with suitable example. (1 +
4)

You might also like