SlideShare a Scribd company logo
RMI
REMOTE METHOD INVOCATION
RMI applications often comprise two separate programs, a server and a client.
A typical server program creates some remote objects, makes references to these objects
accessible, and waits for clients to invoke methods on these objects.
A typical client program obtains a remote reference to one or more remote objects on a server
and then invokes methods on them.
RMI provides the mechanism by which the server and the client communicate and pass
information back and forth.
Such an application is sometimes referred to as a distributed object application.
1. Writing an RMI Server Program.
2. Creating a Client Program
Distributed object applications need to do the following:
Locate remote objects: Applications can use various mechanisms to obtain references to remote
objects.
For example, an application can register its remote objects with RMI's simple naming facility, the
RMI registry.
Alternatively, an application can pass and return remote object references as part of other
remote invocations.
Communicate with remote objects: Details of communication between remote objects are
handled by RMI.
To the programmer, remote communication looks similar to regular Java method invocations.
Load class definitions for objects that are passed around: Because RMI enables objects to be
passed back and forth,it provides mechanisms for loading an object's class definitions as well as
for transmitting an object's data.
The following illustration depicts an RMI distributed application that uses the RMI registry to
obtain a reference to a remote object.
The server calls the registry to associate (or bind) a name with a remote object. The client looks
up the remote object by its name in the server's registry and then invokes a method on it.
Basic java
Advantages of Dynamic Code Loading
One of the central and unique features of RMI is its ability to download the definition of an
object's class if the class is not defined in the receiver's Java virtual machine.
All of the types and behavior of an object, previously available only in a single Java virtual
machine, can be transmitted to another, possibly remote, Java virtual machine.
RMI passes objects by their actual classes, so the behavior of the objects is not changed when
they are sent to another Java virtual machine
This capability enables new types and behaviors to be introduced into a remote Java virtual
machine, thus dynamically extending the behavior of an application. The compute engine
example in this trail uses this capability to introduce new behavior to a distributed program.
Remote Interfaces, Objects, and Methods
Like any other Java application, a distributed application built by using Java RMI is made up of interfaces and
classes. The interfaces declare methods.
The classes implement the methods declared in the interfaces and, perhaps, declare additional methods as well.
In a distributed application, some implementations might reside in some Java virtual machines but not others.
Objects with methods that can be invoked across Java virtual machines are called remote objects.
An object becomes remote by implementing a remote interface, which has the following characteristics:
A remote interface extends the interface java.rmi.Remote. Each method of the interface declares
java.rmi.RemoteException in its throws clause, in addition to any application-specific exceptions.
RMI treats a remote object differently from a non-remote object when the object is passed from
one Java virtual machine to another Java virtual machine.
Rather than making a copy of the implementation object in the receiving Java virtual machine,
RMI passes a remote stub for a remote object.
The stub acts as the local representative, or proxy, for the remote object and basically is, to the
client, the remote reference.
The client invokes a method on the local stub, which is responsible for carrying out the method
invocation on the remote object.
A stub for a remote object implements the same set of remote interfaces that the remote object
implements.
This property enables a stub to be cast to any of the interfaces that the remote object
implements.
However, only those methods defined in a remote interface are available to be called from the
receiving Java virtual machine.
Creating Distributed Applications by Using RMI
Using RMI to develop a distributed application involves these general steps:
1. Designing and implementing the components of your distributed application.
2. Compiling sources.
3. Making classes network accessible.
4. Starting the application.
Designing and Implementing the
Application Components
First, determine your application architecture, including which components are local objects and
which components are remotely accessible.
Defining the remote interfaces: A remote interface specifies the methods that can be invoked
remotely by a client.
Clients program to remote interfaces, not to the implementation classes of those interfaces.
The design of such interfaces includes the determination of the types of objects that will be
used as the parameters and return values for these methods.
Writing an RMI Server
The compute engine server accepts tasks from clients, runs the tasks, and returns any results.
The server code consists of an interface and a class.
The interface defines the methods that can be invoked from the client. Essentially, the interface
defines the client's view of the remote object.
The class provides the implementation.
Designing a Remote Interface
At the core of the compute engine is a protocol that enables tasks to be submitted to the
compute engine, the compute engine to run those tasks, and the results of those tasks to be
returned to the client
This protocol is expressed in the interfaces that are supported by the compute engine.
Each interface contains a single method. The compute engine's remote interface, Compute,
enables tasks to be submitted to the engine.
The client interface, Task, defines how the compute engine executes a submitted task.
The compute Compute interface defines the remotely accessible part, the compute engine itself.
Here is the source code for the Compute interface:
package compute;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Compute extends Remote {
<T> T executeTask(Task<T> t) throws RemoteException;
}
As a member of a remote interface, the executeTask method is a remote method. Therefore, this
method must be defined as being capable of throwing a java.rmi.RemoteException. This
exception is thrown by the RMI system from a remote method invocation to indicate that either
a communication failure or a protocol error has occurred. A RemoteException is a checked
exception, so any code invoking a remote method needs to handle this exception by either
catching it or declaring it in its throws clause.
The second interface needed for the compute engine is the Task interface, which is the type of
the parameter to the executeTask method in the Compute interface. The compute.Task interface
defines the interface between the compute engine and the work that it needs to do, providing
the way to start the work. Here is the source code for the Task interface:
package compute;
public interface Task<T> {
T execute();
}
The Task interface defines a single method, execute, which has no parameters and throws no
exceptions. Because the interface does not extend Remote, the method in this interface doesn't
need to list java.rmi.RemoteException in its throws clause.
The Task interface has a type parameter, T, which represents the result type of the task's
computation. This interface's execute method returns the result of the computation and thus its
return type is T.
The Compute interface's executeTask method, in turn, returns the result of the execution of the
Task instance passed to it. Thus, the executeTask method has its own type parameter, T, that
associates its own return type with the result type of the passed Task instance.
RMI uses the Java object serialization mechanism to transport objects by value between Java
virtual machines. For an object to be considered serializable, its class must implement the
java.io.Serializable marker interface. Therefore, classes that implement the Task interface must
also implement Serializable, as must the classes of objects used for task results.
Different kinds of tasks can be run by a Compute object as long as they are implementations of
the Task type.
The classes that implement this interface can contain any data needed for the computation of
the task and any other methods needed for the computation.
Here is how RMI makes this simple compute engine possible.
Because RMI can assume that the Task objects are written in the Java programming language,
implementations of the Task object that were previously unknown to the compute engine are
downloaded by RMI into the compute engine's
Java virtual machine as needed.
This capability enables clients of the compute engine to define new kinds of tasks to be run on
the server machine without needing the code to be explicitly installed on that machine.
The compute engine, implemented by the ComputeEngine class, implements the Compute
interface, enabling different tasks to be submitted to it by calls to its executeTask method.
These tasks are run using the task's implementation of the execute method and the results, are
returned to the remote client.
Implementing a Remote Interface
In general, a class that implements a remote interface should at least do the following:
1. Declare the remote interfaces being implemented.
2. Define the constructor for each remote object.
3. Provide an implementation for each remote method in the remote interfaces.
An RMI server program needs to create the initial remote objects and export them to the RMI
runtime, which makes them available to receive incoming remote invocations.
This setup procedure can be either encapsulated in a method of the remote object
implementation class itself or included in another class entirely.
The setup procedure should do the following:
1. Create and install a security manager
2. Create and export one or more remote objects
3. Register at least one remote object with the RMI registry (or with another naming service,
such as a service accessible through the Java Naming and Directory Interface) for bootstrapping
purposes
Declaring the Remote Interfaces Being
Implemented
The implementation class for the compute engine is declared as follows:
public class ComputeEngine implements Compute
This declaration states that the class implements the Compute remote interface and therefore
can be used for a remote object.
The ComputeEngine class defines a remote object implementation class that implements a
single remote interface and no other interfaces.
The ComputeEngine class also contains two executable program elements that can only be
invoked locally.
The first of these elements is a constructor for ComputeEngine instances. The second of these
elements is a main method that is used to create a ComputeEngine instance and make it
available to clients.

More Related Content

DOCX
Oracle docs rmi applications
PPTX
Remote Method Invocation
PPT
Remote Method Invocation
PPTX
Remote method invocation
PDF
Module 3 remote method invocation-2
PPT
PPT
PPT
Distributed Programming using RMI
Oracle docs rmi applications
Remote Method Invocation
Remote Method Invocation
Remote method invocation
Module 3 remote method invocation-2
Distributed Programming using RMI

What's hot (20)

PDF
Java RMI Detailed Tutorial
PPT
A Short Java RMI Tutorial
PDF
Remote Method Invocation
PPSX
Java rmi
DOCX
remote method invocation
PPTX
Introduction To Rmi
PPTX
Remote Method Innovation (RMI) In JAVA
PPT
PDF
Introduction to Remote Method Invocation (RMI)
PPTX
Rmi presentation
PDF
Java rmi tutorial
PPTX
PPTX
Remote Method Invocation (Java RMI)
PPS
Java rmi
DOCX
Report on mini project(Student database handling using RMI)
PPT
remote method invocation
PPTX
Java RMI(Remote Method Invocation)
PDF
Rmi ppt-2003
PPTX
Rmi architecture
Java RMI Detailed Tutorial
A Short Java RMI Tutorial
Remote Method Invocation
Java rmi
remote method invocation
Introduction To Rmi
Remote Method Innovation (RMI) In JAVA
Introduction to Remote Method Invocation (RMI)
Rmi presentation
Java rmi tutorial
Remote Method Invocation (Java RMI)
Java rmi
Report on mini project(Student database handling using RMI)
remote method invocation
Java RMI(Remote Method Invocation)
Rmi ppt-2003
Rmi architecture
Ad

Viewers also liked (14)

PPTX
Java RMI
PDF
Java rmi
DOC
Ravi Tuppad
PPTX
PDF
Networking
PDF
Java networking programs - theory
PDF
javarmi
PDF
DOC
Java Servlets & JSP
PDF
Java servlets
PPT
Distributes objects and Rmi
PDF
Remote Method Invocation (RMI)
PPTX
Java RMI Presentation
PPS
Java rmi example program with code
Java RMI
Java rmi
Ravi Tuppad
Networking
Java networking programs - theory
javarmi
Java Servlets & JSP
Java servlets
Distributes objects and Rmi
Remote Method Invocation (RMI)
Java RMI Presentation
Java rmi example program with code
Ad

Similar to Basic java (20)

DOCX
Remote Method Invocation
PPTX
Remote method invocatiom
PDF
RMI (Remote Method Invocation)
PPT
Remote method invocation
PPTX
PPT
Distributed Programming using RMI
PPTX
RMI_0745444444444444444444444111119.pptx
PPSX
Javarmi 130925082348-phpapp01
DOCX
Java interview questions for freshers
PPTX
Java - Remote method invocation
PDF
Chapter 6-Remoting
PPTX
Remote method invocation
PPTX
What is rmi?
PPTX
Java RMI
PDF
Remote Method Invocation in JAVA
PDF
PDF
Roboconf Detailed Presentation
PDF
PPT
MIDELWARE TECH
Remote Method Invocation
Remote method invocatiom
RMI (Remote Method Invocation)
Remote method invocation
Distributed Programming using RMI
RMI_0745444444444444444444444111119.pptx
Javarmi 130925082348-phpapp01
Java interview questions for freshers
Java - Remote method invocation
Chapter 6-Remoting
Remote method invocation
What is rmi?
Java RMI
Remote Method Invocation in JAVA
Roboconf Detailed Presentation
MIDELWARE TECH

More from Raghu nath (20)

PPTX
Mongo db
PDF
Ftp (file transfer protocol)
PDF
MS WORD 2013
PDF
Msword
PDF
Ms word
PDF
Javascript part1
PDF
Regular expressions
PDF
Selection sort
PPTX
Binary search
PPTX
JSON(JavaScript Object Notation)
PDF
Stemming algorithms
PPTX
Step by step guide to install dhcp role
PPTX
Network essentials chapter 4
PPTX
Network essentials chapter 3
PPTX
Network essentials chapter 2
PPTX
Network essentials - chapter 1
PPTX
Python chapter 2
PPTX
python chapter 1
PPTX
Linux Shell Scripting
PPTX
Mongo db
Ftp (file transfer protocol)
MS WORD 2013
Msword
Ms word
Javascript part1
Regular expressions
Selection sort
Binary search
JSON(JavaScript Object Notation)
Stemming algorithms
Step by step guide to install dhcp role
Network essentials chapter 4
Network essentials chapter 3
Network essentials chapter 2
Network essentials - chapter 1
Python chapter 2
python chapter 1
Linux Shell Scripting

Recently uploaded (20)

PPTX
Onica Farming 24rsclub profitable farm business
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PDF
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Landforms and landscapes data surprise preview
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
PDF
English Language Teaching from Post-.pdf
PPTX
Introduction and Scope of Bichemistry.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
ACUTE NASOPHARYNGITIS. pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
High Ground Student Revision Booklet Preview
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
Onica Farming 24rsclub profitable farm business
How to Manage Loyalty Points in Odoo 18 Sales
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
Week 4 Term 3 Study Techniques revisited.pptx
Software Engineering BSC DS UNIT 1 .pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Landforms and landscapes data surprise preview
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
The Final Stretch: How to Release a Game and Not Die in the Process.
NOI Hackathon - Summer Edition - GreenThumber.pptx
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
English Language Teaching from Post-.pdf
Introduction and Scope of Bichemistry.pptx
Renaissance Architecture: A Journey from Faith to Humanism
ACUTE NASOPHARYNGITIS. pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
High Ground Student Revision Booklet Preview
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
UPPER GASTRO INTESTINAL DISORDER.docx

Basic java

  • 2. RMI applications often comprise two separate programs, a server and a client. A typical server program creates some remote objects, makes references to these objects accessible, and waits for clients to invoke methods on these objects. A typical client program obtains a remote reference to one or more remote objects on a server and then invokes methods on them. RMI provides the mechanism by which the server and the client communicate and pass information back and forth. Such an application is sometimes referred to as a distributed object application.
  • 3. 1. Writing an RMI Server Program. 2. Creating a Client Program
  • 4. Distributed object applications need to do the following: Locate remote objects: Applications can use various mechanisms to obtain references to remote objects. For example, an application can register its remote objects with RMI's simple naming facility, the RMI registry. Alternatively, an application can pass and return remote object references as part of other remote invocations. Communicate with remote objects: Details of communication between remote objects are handled by RMI. To the programmer, remote communication looks similar to regular Java method invocations.
  • 5. Load class definitions for objects that are passed around: Because RMI enables objects to be passed back and forth,it provides mechanisms for loading an object's class definitions as well as for transmitting an object's data. The following illustration depicts an RMI distributed application that uses the RMI registry to obtain a reference to a remote object. The server calls the registry to associate (or bind) a name with a remote object. The client looks up the remote object by its name in the server's registry and then invokes a method on it.
  • 7. Advantages of Dynamic Code Loading One of the central and unique features of RMI is its ability to download the definition of an object's class if the class is not defined in the receiver's Java virtual machine. All of the types and behavior of an object, previously available only in a single Java virtual machine, can be transmitted to another, possibly remote, Java virtual machine. RMI passes objects by their actual classes, so the behavior of the objects is not changed when they are sent to another Java virtual machine This capability enables new types and behaviors to be introduced into a remote Java virtual machine, thus dynamically extending the behavior of an application. The compute engine example in this trail uses this capability to introduce new behavior to a distributed program.
  • 8. Remote Interfaces, Objects, and Methods Like any other Java application, a distributed application built by using Java RMI is made up of interfaces and classes. The interfaces declare methods. The classes implement the methods declared in the interfaces and, perhaps, declare additional methods as well. In a distributed application, some implementations might reside in some Java virtual machines but not others. Objects with methods that can be invoked across Java virtual machines are called remote objects. An object becomes remote by implementing a remote interface, which has the following characteristics: A remote interface extends the interface java.rmi.Remote. Each method of the interface declares java.rmi.RemoteException in its throws clause, in addition to any application-specific exceptions.
  • 9. RMI treats a remote object differently from a non-remote object when the object is passed from one Java virtual machine to another Java virtual machine. Rather than making a copy of the implementation object in the receiving Java virtual machine, RMI passes a remote stub for a remote object. The stub acts as the local representative, or proxy, for the remote object and basically is, to the client, the remote reference. The client invokes a method on the local stub, which is responsible for carrying out the method invocation on the remote object.
  • 10. A stub for a remote object implements the same set of remote interfaces that the remote object implements. This property enables a stub to be cast to any of the interfaces that the remote object implements. However, only those methods defined in a remote interface are available to be called from the receiving Java virtual machine.
  • 11. Creating Distributed Applications by Using RMI Using RMI to develop a distributed application involves these general steps: 1. Designing and implementing the components of your distributed application. 2. Compiling sources. 3. Making classes network accessible. 4. Starting the application.
  • 12. Designing and Implementing the Application Components First, determine your application architecture, including which components are local objects and which components are remotely accessible. Defining the remote interfaces: A remote interface specifies the methods that can be invoked remotely by a client. Clients program to remote interfaces, not to the implementation classes of those interfaces. The design of such interfaces includes the determination of the types of objects that will be used as the parameters and return values for these methods.
  • 13. Writing an RMI Server The compute engine server accepts tasks from clients, runs the tasks, and returns any results. The server code consists of an interface and a class. The interface defines the methods that can be invoked from the client. Essentially, the interface defines the client's view of the remote object. The class provides the implementation.
  • 14. Designing a Remote Interface At the core of the compute engine is a protocol that enables tasks to be submitted to the compute engine, the compute engine to run those tasks, and the results of those tasks to be returned to the client This protocol is expressed in the interfaces that are supported by the compute engine.
  • 15. Each interface contains a single method. The compute engine's remote interface, Compute, enables tasks to be submitted to the engine. The client interface, Task, defines how the compute engine executes a submitted task. The compute Compute interface defines the remotely accessible part, the compute engine itself. Here is the source code for the Compute interface:
  • 16. package compute; import java.rmi.Remote; import java.rmi.RemoteException; public interface Compute extends Remote { <T> T executeTask(Task<T> t) throws RemoteException; }
  • 17. As a member of a remote interface, the executeTask method is a remote method. Therefore, this method must be defined as being capable of throwing a java.rmi.RemoteException. This exception is thrown by the RMI system from a remote method invocation to indicate that either a communication failure or a protocol error has occurred. A RemoteException is a checked exception, so any code invoking a remote method needs to handle this exception by either catching it or declaring it in its throws clause. The second interface needed for the compute engine is the Task interface, which is the type of the parameter to the executeTask method in the Compute interface. The compute.Task interface defines the interface between the compute engine and the work that it needs to do, providing the way to start the work. Here is the source code for the Task interface: package compute;
  • 18. public interface Task<T> { T execute(); } The Task interface defines a single method, execute, which has no parameters and throws no exceptions. Because the interface does not extend Remote, the method in this interface doesn't need to list java.rmi.RemoteException in its throws clause. The Task interface has a type parameter, T, which represents the result type of the task's computation. This interface's execute method returns the result of the computation and thus its return type is T.
  • 19. The Compute interface's executeTask method, in turn, returns the result of the execution of the Task instance passed to it. Thus, the executeTask method has its own type parameter, T, that associates its own return type with the result type of the passed Task instance. RMI uses the Java object serialization mechanism to transport objects by value between Java virtual machines. For an object to be considered serializable, its class must implement the java.io.Serializable marker interface. Therefore, classes that implement the Task interface must also implement Serializable, as must the classes of objects used for task results.
  • 20. Different kinds of tasks can be run by a Compute object as long as they are implementations of the Task type. The classes that implement this interface can contain any data needed for the computation of the task and any other methods needed for the computation. Here is how RMI makes this simple compute engine possible. Because RMI can assume that the Task objects are written in the Java programming language, implementations of the Task object that were previously unknown to the compute engine are downloaded by RMI into the compute engine's
  • 21. Java virtual machine as needed. This capability enables clients of the compute engine to define new kinds of tasks to be run on the server machine without needing the code to be explicitly installed on that machine. The compute engine, implemented by the ComputeEngine class, implements the Compute interface, enabling different tasks to be submitted to it by calls to its executeTask method. These tasks are run using the task's implementation of the execute method and the results, are returned to the remote client.
  • 22. Implementing a Remote Interface In general, a class that implements a remote interface should at least do the following: 1. Declare the remote interfaces being implemented. 2. Define the constructor for each remote object. 3. Provide an implementation for each remote method in the remote interfaces.
  • 23. An RMI server program needs to create the initial remote objects and export them to the RMI runtime, which makes them available to receive incoming remote invocations. This setup procedure can be either encapsulated in a method of the remote object implementation class itself or included in another class entirely. The setup procedure should do the following: 1. Create and install a security manager 2. Create and export one or more remote objects 3. Register at least one remote object with the RMI registry (or with another naming service, such as a service accessible through the Java Naming and Directory Interface) for bootstrapping purposes
  • 24. Declaring the Remote Interfaces Being Implemented The implementation class for the compute engine is declared as follows: public class ComputeEngine implements Compute This declaration states that the class implements the Compute remote interface and therefore can be used for a remote object. The ComputeEngine class defines a remote object implementation class that implements a single remote interface and no other interfaces. The ComputeEngine class also contains two executable program elements that can only be invoked locally. The first of these elements is a constructor for ComputeEngine instances. The second of these elements is a main method that is used to create a ComputeEngine instance and make it available to clients.