Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
103 views

Chapter 5 Remote Procedure Call - and - Remote Method Invocation

The document discusses Remote Procedure Call (RPC) and Remote Method Invocation (RMI) in Java. It describes that RPC and RMI allow invoking functions on remote servers. RMI specifically refers to the Java implementation which allows invoking methods on remote Java objects. The document outlines key concepts of RMI including stubs, skeletons, remote interfaces, and the RMI registry.

Uploaded by

PS 4 MTA
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Chapter 5 Remote Procedure Call - and - Remote Method Invocation

The document discusses Remote Procedure Call (RPC) and Remote Method Invocation (RMI) in Java. It describes that RPC and RMI allow invoking functions on remote servers. RMI specifically refers to the Java implementation which allows invoking methods on remote Java objects. The document outlines key concepts of RMI including stubs, skeletons, remote interfaces, and the RMI registry.

Uploaded by

PS 4 MTA
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

© Copyright 1992-2015 by Pearson Education, Inc.

All Rights
Reserved.
Outline
5.1. Overview of RPC & RMI
5.2. Stub and skeleton
5.3. The RMI Registry
5.4. The Remote Interface
5.5. Implementing RMI

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 RPC (Remote Procedure Call) and RMI (Remote Method
Invocation) are both mechanisms for invoking functions or
methods located on remote computers. Here is an overview of
each:
 RPC:
 RPC is a generic concept that refers to any mechanism for
making a procedure call across a network.
 In RPC, the client sends a request to the server to execute a
procedure or function, and the server returns the result to the
client.
 RPC allows clients to invoke functions on a remote server as if
they were local functions.
 RPC can be used with a variety of programming languages and
protocols, such as XML-RPC, JSON-RPC, gRPC, and CORBA.
3
 RMI:
 RMI is a specific implementation of RPC in the Java programming
language.
 RMI allows Java objects running in one JVM to invoke methods on
Java objects running in another JVM, located on a remote machine.
 RMI uses Java serialization to pass parameters and return values
between the client and server, and it provides automatic garbage
collection and exception handling.
 RMI provides a simple and intuitive way to create distributed
applications in Java.
 Both RPC and RMI provide a way to develop distributed applications
by allowing clients to invoke functions on a remote server. However,
RMI is specifically designed for Java applications, while RPC can be
used with any programming language or protocol.
4
 In the context of Remote Procedure Call (RPC) and (RMI), stub and
skeleton are important components that help in the communication
between the client and the server.
 A stub is a client-side proxy that represents the remote object and
provides a local interface for the client to interact with the remote
object.
 When the client invokes a method on the stub, the stub marshals the
parameters, sends the request to the server, waits for the response,
and unmarshals the result before returning it to the client.
 The stub hides the details of the remote communication and makes
it appear as if the remote object is a local object.
 In RMI, the stub is generated automatically by the RMI compiler
and is responsible for handling the communication with the remote
server.
5
 A skeleton is a server-side proxy that receives requests from the
client, unmarshals the parameters, invokes the remote method,
marshals the result, and sends it back to the client. The skeleton is
responsible for handling the low-level details of the remote
communication and providing a transparent interface to the server
object.
 In RMI, the skeleton is generated automatically by the RMI
runtime system.
 In summary, the stub is a client-side proxy that communicates with
the server, while the skeleton is a server-side proxy that
communicates with the client. Both stub and skeleton are
generated automatically by the RMI compiler/runtime and are
responsible for hiding the low-level details of the remote
communication and providing a transparent interface to the client
and server, respectively. 6
 The RMI Registry is a central component of Remote Method
Invocation (RMI) in Java.
 It is a simple naming service that allows clients to look up remote
objects by their names and obtain references to those objects for
remote method invocation.
 The RMI Registry serves as a central repository for storing references
to remote objects.
 When a server creates a remote object, it registers the object with the
RMI Registry using a name that identifies the object.
 The name can be any string that uniquely identifies the object within
the registry.
 Clients can then look up the remote object in the RMI Registry by its
name, and the registry returns a reference to the object. The client can
then use the reference to invoke methods on the remote object as if it
were a local object. 7
 The RMI Registry can be started as a separate process or can be
started within the server process itself.
 When the registry is started as a separate process, it listens on a
specific port for incoming requests from clients.
 The client can then connect to the registry using the host name and
port number of the server machine.
 The RMI Registry is a lightweight and simple naming service that
provides an easy way for clients to obtain references to remote
objects in RMI-based distributed applications.
 It plays a key role in enabling RMI clients to interact with remote
objects without needing to know their physical location or network
details.

8
 In Java, the Remote Interface is an interface that enables objects to
be accessed remotely, across different Java Virtual Machines
(JVMs) or even across different machines.
 Remote Interfaces are used in distributed applications where
different parts of the application are running on different machines
and need to communicate with each other.
 Remote Interfaces in Java are implemented using the Java Remote
Method Invocation (RMI) framework, which provides a
mechanism for invoking methods on objects running in different
JVMs.
 To create a Remote Interface in Java, you need to define an
interface that extends the java.rmi.Remote interface and declare
the methods that can be invoked remotely. For example:
9
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MyRemoteInterface extends Remote {


public void doSomething() throws RemoteException;
}
Once you have defined the Remote Interface, you need to
implement it on the server-side and make it available for remote
access.
This is typically done by creating a remote object that
implements the Remote Interface and registering it with the RMI
registry. For example:
10
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class MyRemoteObject extends UnicastRemoteObject


implements MyRemoteInterface {
public MyRemoteObject() throws RemoteException {
super();
}

public void doSomething() throws RemoteException {


// Implementation code here
}
}

11
On the client-side, you can then access the remote object by
looking it up in the RMI registry and invoking its methods as
if it were a local object.
The RMI framework takes care of the details of marshalling
and unmarshalling parameters and return values across the
network. For example:

12
import java.rmi.Naming;
import java.rmi.RemoteException;
public class MyRemoteClient {
public static void main(String[] args) {
try {
MyRemoteInterface remoteObject = (MyRemoteInterface)
Naming.lookup("rmi://localhost/MyRemoteObject");
remoteObject.doSomething();
} catch (Exception e) {
e.printStackTrace();
}
}
}
13
To implement RMI (Remote Method Invocation) in Java, you
need to follow the following steps:
1.Define the Remote Interface: The first step is to define the
remote interface that specifies the methods that can be invoked
remotely. The interface should extend the java.rmi.Remote
interface and each method should declare
java.rmi.RemoteException. For example:

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

public interface MyRemoteInterface extends Remote {


public String sayHello(String name) throws RemoteException;
}
14
2.Implement the Remote Interface: The next step is to implement the
remote interface in a class that provides the actual implementation of
the methods. This class should extend the
java.rmi.server.UnicastRemoteObject class and have a constructor
that declares java.rmi.RemoteException. For example:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class MyRemoteImpl extends UnicastRemoteObject
implements MyRemoteInterface {
public MyRemoteImpl() throws RemoteException {
super();
}
public String sayHello(String name) throws RemoteException {
return "Hello, " + name + "!";
}} 15
Create and Start the RMI Registry: The RMI registry is a simple
naming service that allows clients to look up remote objects by name.
To create and start the RMI registry, you can use the
java.rmi.registry.LocateRegistry class. For example:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class MyRemoteServer {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.createRegistry(1099);
MyRemoteImpl remoteImpl = new MyRemoteImpl();
registry.bind("MyRemote", remoteImpl);
System.out.println("Remote object bound to registry.");
16
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}

17
Create the Client: Finally, you can create the client that accesses the
remote object by looking it up in the RMI registry using the
java.rmi.Naming class. For example:
import java.rmi.Naming;
import java.rmi.RemoteException;
public class MyRemoteClient {
public static void main(String[] args) {
try {
MyRemoteInterface remoteObject = (MyRemoteInterface)
Naming.lookup("rmi://localhost/MyRemote");

18
String result = remoteObject.sayHello("Alice");
System.out.println(result);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}

19
When you run the server and client, the output should be:

Remote object bound to registry.


Hello, Alice!

20

You might also like