Unit 3
Unit 3
The distributed applications consist of two parts - a client part and a server part. A typical server
part of such an application defines several distributed objects and allows clients to request them
by obtaining references to them. The client part obtains a reference to one or more objects from
the server part, and calls their methods.
RMI-P4 provides the mechanism by which client and server parts communicate.
Features
Object reference
Interfaces
Actions
Exceptions
The RMI-P4 implements the following functions that an application based on the Distributed
Object Model requires: Locate the distributed object Communicate with the distributed object
Load classes of objects
Object Reference: In a distributed object model, an object reference is a handle or
identifier that clients use to access remote objects. It abstracts the details of object
location and communication, allowing clients to interact with remote objects as if they
were local.
Interfaces: Interfaces define the contract or set of methods that objects must
implement to provide a specific functionality. In distributed systems, interfaces facilitate
communication between clients and remote objects by specifying the methods that
clients can invoke on those objects.
Actions: Actions refer to the operations or methods that can be performed on objects in
a distributed environment. These actions represent the functionality exposed by remote
objects, which clients can invoke through method calls to achieve desired tasks or
behaviors.
Exceptions: Exceptions in distributed systems are used to handle errors and exceptional
conditions that may occur during remote method invocations. They allow for graceful
error handling and recovery, enabling robust and reliable communication between
clients and remote objects.
3)Write a RMI program that demonstrates the invocation of remote object services. For
example when a client sends a message "hello", the server responds with "Hi, You
There". [7M, CO3, Apply]
Here's a simple RMI (Remote Method Invocation) program that demonstrates the invocation of
remote object services. In this example, when a client sends a message "hello" to the server,
the server responds with "Hi, You There".
```java
import java.rmi.Remote;
import java.rmi.RemoteException;
```java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
@Override
public String sayHello(String message) throws RemoteException {
if ("hello".equalsIgnoreCase(message)) {
return "Hi, You There";
} else {
return "Unknown message";
}
}
}
```
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
System.out.println("Server started");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
import java.rmi.Naming;
1. Mark Phase: Nodes traverse their local memory heap, marking reachable objects.
2. Synchronization: Nodes exchange information to ensure a consistent view of live
objects.
3. Reference Counting or Tracing: Nodes determine unreachable objects using
reference counting or tracing algorithms.
4. Collection: Unreachable objects are reclaimed, freeing up memory space.
5. Finalization (Optional): Finalization methods may be invoked on collected objects
before memory reclamation.
The RMI (Remote Method Invocation) is an API that provides a mechanism to create
distributed application in java. The RMI allows an object to invoke methods on an object
running in another JVM.
The RMI provides remote communication between the applications using two objects
stub and skeleton.
RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM. Let's
understand the stub and skeleton objects:
stub
The stub is an object, acts as a gateway for the client side. All the outgoing requests are
routed through it. It resides at the client side and represents the remote object. When
the caller invokes method on the stub object, it does the following tasks:
skeleton
The skeleton is an object, acts as a gateway for the server side object. All the incoming
requests are routed through it. When the skeleton receives the incoming request, it does
the following tasks:
1. It reads the parameter for the remote method
2. It invokes the method on the actual remote object, and
3. It writes and transmits (marshals) the result to the caller.
In the Java 2 SDK, an stub protocol was introduced that eliminates the need for
skeletons.
1. Location Transparency:
● Use a naming service like RMI registry to map logical object names to
network addresses.
2. Access Transparency:
● Define remote interfaces for methods available for remote invocation.
● RMI generates stubs and skeletons automatically, handling network
communication transparently.
3. Failure Transparency:
● RMI handles failures and exceptions automatically, retrying failed method
calls and propagating exceptions between client and server.
4. Consistency:
● RMI maintains consistency by ensuring synchronous invocation
semantics and preserving the order of method calls.
●
7)With case study explain the concept of event and notification.
Publish-subscribe paradigm: publisher sends notifications, i.e. objects representing
events
Subscriber registers interest to receive notifications
The object of interest: where events happen, change of state as a result of its
operations being invoked
Observer objects: decouple an object of interest from its subscribers (not important)
In our scenario, we have sensors scattered around gathering data like temperature and
humidity. Let's use Jini's Distributed Event Specification (DES) to get alerts when certain
conditions are met.
Implementation:
1. Sensor Setup:
● Each sensor registers with the Jini lookup service.
● Sensors announce what data they can provide, like high temperature or
low humidity.
2. Client Subscription:
● Clients interested in specific data types subscribe to the Jini event service.
● They specify what conditions they want, such as temperature above 30°C.
3. Event Handling:
● When a sensor detects an event, like high temperature, it informs the Jini
event service.
● The event service sends this information to all subscribed clients.
Benefits:
1. Easy Setup:
● Sensors and clients can join or leave the network without any hassle.
2. Instant Alerts:
● Clients get real-time notifications when events occur.
3. Flexible Configuration:
● Clients can choose what data they want and under what conditions they
receive alerts.
Conclusion:
Lightweight Remote Procedure Call (RPC) means a simple and efficient way for
simple and fast, avoiding unnecessary complexity and overhead. With lightweight RPC,
programs can call functions on remote servers just like they would call functions locally,
5. Scalability: It's designed to handle many remote calls at once without slowing
1. Callback RPC:
● Enables a peer-to-peer communication model where processes can act as
both clients and servers.
● Allows processes to handle interactive application problems remotely.
● Provides clients with a handle to server services.
● Involves callback functions that make the client process wait for a
response.
● Manages callback deadlocks to prevent system hang-ups.
2. Broadcast RPC:
● Involves broadcasting a client's request message to all servers on the
network.
● Each server processes the request using the appropriate method.
● Allows specifying that the client's request message should be broadcast.
● Helps reduce network load by distributing processing tasks across
multiple servers.
3. Batch-mode RPC:
● Queues and separates RPC requests on the client-side before sending
them as a batch to the server over the network.
● Minimizes overhead by sending multiple requests in one batch.
● Efficient for applications with lower call rates that can benefit from
batching requests.
● Requires a reliable transmission protocol to ensure all requests are
delivered successfully.
Explanation:
1. Objects:
● Objects are instances of classes in OOP. Each object has its own state
(attributes) and behavior (methods).
2. Message Passing:
● Communication between objects occurs through message passing. An
object sends a message to another object to request a specific behavior
or to obtain information.
3. Sender Object:
● The sender object initiates the communication by sending a message to
the receiver object.
4. Receiver Object:
● The receiver object receives the message sent by the sender object and
responds accordingly.
5. Message:
● The message contains information about the requested behavior or the
data to be exchanged between objects.
6. Method Invocation:
● Upon receiving the message, the receiver object invokes the appropriate
method to perform the requested behavior or to process the received data.
7. Response:
● After processing the message, the receiver object may send a response
back to the sender object, depending on the nature of the communication.
14)What do you mean by code migration? To what extent does java RMI reply on code
migration. [7M, CO3, Understand
Code Migration:
Code migration refers to the process of transferring executable code from one
computing environment to another. This can involve moving code between different
machines, platforms, or execution contexts. The goal of code migration is to enable the
execution of code in a remote or distributed environment, allowing applications to
dynamically adapt to changing conditions, optimize resource utilization, or enhance
system performance.
Java Remote Method Invocation (RMI) is a Java technology that allows communication
between Java objects in different Java virtual machines (JVMs), typically across a
network. While Java RMI primarily focuses on remote method invocation, it does not
heavily rely on code migration as a core feature. However, code migration can still play a
role in certain aspects of Java RMI-based applications:
1. Remote Interface:
● Define an interface that extends java.rmi.Remote.
● Declare the methods that the client can invoke remotely.
2. Implement Remote Interface:
● Create a class that implements the remote interface.
● Implement the methods declared in the interface.
3. Create Server Application:
● Create a server application that exports the remote object and binds it to
the RMI registry.
● Start the RMI registry to listen for client requests.
Client Side:
Explanation:
Server Side:
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
// Server class
public class Server {
public static void main(String[] args) {
try {
// Create and export the remote object
RemoteService service = new RemoteServiceImpl();
System.out.println("Server started.");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Client Side:
import java.rmi.Naming;
// Client class
public class Client {
public static void main(String[] args) {
try {
// Look up the remote object from the RMI registry
RemoteService service = (RemoteService)
Naming.lookup("//localhost/RemoteService");
Design Issues:
1. Interface Definition:
● Define the remote interface that extends java.rmi.Remote.
● Declare methods in the interface that can be invoked remotely.
2. Object Serialization:
● Ensure that objects passed between client and server are serializable.
● Use java.io.Serializable interface for classes that need to be serialized.
3. Naming and Registry:
● Use the RMI registry to bind remote objects and make them accessible to
clients.
● Ensure that clients can look up remote objects using a well-known name
or URL.
4. Security:
● Implement security measures to prevent unauthorized access and ensure
data integrity.
● Use authentication, encryption, and access control mechanisms provided
by Java security APIs.
5. Exception Handling:
● Handle RemoteExceptions and other exceptions gracefully to ensure
robust error handling.
● Define custom exception classes to convey meaningful error messages to
clients.
Implementation:
1. Remote Interface:
● Define a remote interface that extends java.rmi.Remote.
● Declare methods that need to be invoked remotely.
2. Remote Object Implementation:
● Create a class that implements the remote interface.
● Extend java.rmi.server.UnicastRemoteObject to make the object remotely
accessible.
3. Server Application:
● Create a server application that instantiates and exports the remote
object.
● Bind the remote object to the RMI registry using java.rmi.registry.Registry.
4. Client Application:
● Create a client application that looks up the remote object from the RMI
registry.
● Cast the remote object to the remote interface and invoke its methods.
5. Exception Handling:
● Handle RemoteExceptions and other exceptions in both server and client
applications.
● Implement error recovery and retry mechanisms if necessary.