Asg 01 RMI
Asg 01 RMI
01
ASSIGNMENT NO. 01
TITLE: RMI
OBJECTIVE: To study:-
Multi-threading
How Remote Method Invocation works.
How RMI allows objects to invoke methods on remote objects.
How to write Distributed Object Application.
PROBLEM STATEMENT:-
Write a program to implement multi-threaded client/server Process communication
using RMI.
TOOLS / ENVIRONMENT:
S/W:
o Can be developed on any platform Windows /Linux.
o Java Language, Java Programming Environment, rmiregistry, jdk
H/W:
o Any basic configuration loaded machine (e.g. P IV )
THEORY:
Thread:-
A thread of execution is the smallest sequence of programmed instructions that can be
managed independently by a scheduler, which is typically a part of the operating system.
The implementation of threads and processes differs between operating systems. A process
consists of an execution environment together with one or more threads. A thread is the
operating system abstraction of an activity (the term derives from the phrase ‘thread of
execution’). An execution environment is the unit of resource management: a collection of
local kernel managed resources to which its threads have access. An execution environment
primarily consists of:
• an address space;
• thread synchronization and communication resources such as semaphores and
communication interfaces (for example, sockets);
• higher-level resources such as open files and windows.
Threads can be created and destroyed dynamically, as needed. The central aim of having
multiple threads of execution is to maximize the degree of concurrent execution between
operations, thus enabling the overlap of computation with input and output, and enabling
concurrent processing on multiprocessors. This can be particularly helpful within servers,
where concurrent processing of clients’ requests can reduce the tendency for servers to
become bottlenecks.
RMI automatically will execute each client in a separate thread. There will only be one
instance of your server object no matter how many clients, so you need to make sure that
shared data is synchronized appropriately. In particular, any data that may be accessed by
more than one client at a time must be synchronized.
Also note that you cannot depend on data in the server to remain the same between two
successive calls from a client. In our rental car example from the first class, a client may get
a list of cars in one call, and in a later call try to reserve the car. In the meantime, another
client may have already reserved that car. The server must double-check all data coming
from the client to protect against this sort of error. Also for this reason, the server should
never pass to the client any direct pointers to its internal data structures (such as an array
index), as that type of data is highly likely to change before the client tries to use it.
Multithreading can also introduce very difficult to find bugs into your program. The types of
bugs introduced because of multithreading are called "race conditions". A race condition is
a bug that is timing sensitive. In other words, the bug only happens when several
conditions happen at exactly the same time. With multithreading, the possibility of race
conditions increases.
Unfortunately, it's almost impossible to know if a multithreading program has bugs or not.
Each thread runs at the same time as the other threads. A computer with a single CPU,
however, cannot really run multiple instructions at the same time. So it runs instructions
from one thread for a while, and then runs some instructions from another thread. You
have no way of knowing exactly when a thread may be interrupted and another thread run.
Thread States
RMI:-
RMI provides communication between java applications that are deployed on different
servers and connected remotely using objects called stub and skeleton. This communication
architecture makes a distributed application seem like a group of objects communicating
across a remote connection. These objects are encapsulated by exposing an interface, which
helps access the private state and behavior of an object through its methods.
The following diagram shows how RMI happens between the RMI client and RMI server with
the help of the RMI registry:
RMI Registry:
It is a remote object registry, a Bootstrap naming service, that is used by RMI SERVER on the
same host to bind remote objects to names. Clients on local and remote hosts then look up
the remote objects and make remote method invocations.
The following diagram demonstrates RMI communication with stub and skeleton involved:
Let's design a project that can sit on a server. After that different client projects interact
with this project to pass the parameters and get the computation on the remote object
execute and return the result to the client components.
The application begins by forming a string that follows the URL syntax. This URL uses the rmi
protocol. The string includes the IP address or name of the server and the string
“AddServer”. The program then invokes the lookup( ) method of the Naming class.
This method accepts one argument, the rmi URL, and returns a reference to an object of
type AddServerIntf. All remote method invocations can then be directed to this object.
The program continues by displaying its arguments and then invokes the remote add()
method. The sum is returned from this method and is then printed.
Use javac to compile the four source files that are created.
2. Generate a Stub
Before using client and server, the necessary stub must be generated. In the context of
RMI, a stub is a Java object that resides on the client machine. Its function is to present the
same interfaces as the remote server. Remote method calls initiated by the client are
actually directed to the stub. The stub works with the other parts of the RMI system to
formulate a request that is sent to the remote machine.
All of this information must be sent to the remote machine. That is, an object passed as an
argument to a remote method call must be serialized and sent to the remote machine. If a
response must be returned to the client, the process works in reverse. The serialization and
deserialization facilities are also used if objects are returned to a client.
To generate a stub the command is RMIcompiler is invoked as follows:
rmic AddServerImpl.
start rmiregistry
The AddServer code instantiates AddServerImpl and registers that object with the
name “AddServer”.
(Students should write here the implementation for their program. Students should attach
printout of their programs with commands used to run the programs. Also attach the proper
outputs of programs.)
Conclusion:
Remote Method Invocation (RMI) allows you to build Java applications that are distributed
among several machines. Remote Method Invocation (RMI) allows a Java object that
executes on one machine to invoke a method of a Java object that executes on another
machine. This is an important feature, because it allows you to build distributed
applications.
FAQ:
1. What is thread? What is difference between thread and process?
2. What are types of thread?
3. What is RMI? Explain the RMI architecture ?
4. What is a remote object ? Why should we extend UnicastRemoteObject ? What is
Unicast and Multicast object?
5. What are the services provided by the RMI Object ?
6. What are the differences between RMI and a socket ?
7. How will you pass parameters in RMI ?
8. What is HTTP tunneling or how do you make RMI calls across firewalls ?
9. Why use RMI when we can achieve the same benefits from EJB ?
10. How many types of protocol implementations does RMI have?
11. Can RMI and Corba based applications interact ?
12. Explain the difference between RPC and RMI.
13. What is the difference between RMI and JMS?
14. What is Registry Service for RMI?
15. Explain how URL convention is used for accessing the registry.
16. Explain how to bind an object to the registry.
17. Explain the various methods of registering and gaining access to the Remote Object.
18. What are Remote callbacks?