Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Remote Method Invocation

Creating a Distributed System Remote Method Invocation (RMI) is Java’s


with RMI implementation of object-to-object
communication among Java objects to realize
a distributed computing model.
B.Ramamurthy RMI allows us to distribute our objects on
various machines, and invoke methods on the
objects located on remote sites.
Source code for the demo is a modified
version of code in Chapter 20 of Deitel &
Deitel’s text Java : How to Program.

1/24/02 B.Ramamurthy 1 1/24/02 B.Ramamurthy 2

Steps in RMI-based
RMI-based Distributed System Application
1. Design the interface for the service.
4.
2. Implement the methods specified in
5. 3. 3. 2. the interface.
XYZ XYZ
Client
Stub Stub Implementation 3. Generate the stub and the skeleton.
uses 1. 4. Register the service by name and
implements
XYZ location.
interface
5. Use the service in an application.
Client Host Server Host

1/24/02 B.Ramamurthy 3 1/24/02 B.Ramamurthy 4

Compile and Register


Commands More Details
rmiregistry Once the object (or service) is registered, a
Finds object by name rmic Stores object by name
client can look up that service.
A client (application) receives a reference that
5. 3. 3. 2.
XYZ XYZ
allows the client to use the service (call the
Stub Skeleton
Client Implementation method).
uses 1. Syntax of calling is identical to a call to a
XYZ implements method of another object in the same
interface
program.
Client Host Server Host

1/24/02 B.Ramamurthy 5 1/24/02 B.Ramamurthy 6

1
Case Study : Temperature
Parameter Marshalling Service
Transfer of parameters (or marshalling) Lets create a distributed system using
is done by the RMI. RMI model for networking (remote
Complex objects are streamed using access).
Serialization. Basically this program will download the
RMI model of networking for distributed weather (temperature) information
system involves only Java. from the site:
No need to learn IDL or any other http://iwin.nws.noaa.gov/iwin/us/traveler.html
language.

1/24/02 B.Ramamurthy 7 1/24/02 B.Ramamurthy 8

Temperature Client/Server
Distributed Application Defining Remote Interface
import java.rmi.*;
rmic
4
// the interface extends Remote interface
compiles
// any class implementing Remote can be
5. 3. generates 3. 2. accessed remotely security permitting
TempClient TempImpl_Stub uses TempImpl_Stub
public interface TemperatureServer extends
TempServerImpl

Remote
1.
uses
implements { // specify methods that can be called
TempServer
remotely
Client Host Server Host
// each method “throws RemoteException”
}
1/24/02 B.Ramamurthy 9 1/24/02 B.Ramamurthy 10

Implementing the Remote


RemoteException Interface
Any time you depend on outside entities import java.rmi.*;
there is a potential for problems in
communication, networking, server crash etc. import java.rmi.server.*;
Any exception due to these should be import java.net.*;
handled by the services.
// others as needed
This feature imparts robustness to the
application. TemperatureServerImpl
Java mandates this feature for any RMI extends UnicastRemoteObject
service. implements TemperatureServer {

1/24/02 B.Ramamurthy 11 1/24/02 B.Ramamurthy 12

2
TemperatureServerImpl
TemperatureServerImpl (contd.)
This class’s constructor calls a private method It implements the service method
which in turn: getWeatherInfo which simply returns
1. Connects to the url specified the weather data gathered.
2. Streams into a buffer the page referenced. The main method instantiates an object
3. Parses the buffer to get the required data. for the service, and registers it with
4. Creates an array of weather information. rmiregistry.

1/24/02 B.Ramamurthy 13 1/24/02 B.Ramamurthy 14

Streaming URLs Server Object Name


Using the openStream of java.net.URL class Syntax for the server object name is:
you can stream in the file spefied by an //host:port/remoteObjectName
universal resource locator(url).
It can be streamed into a buffer where it can Default port number for rmiregistry is 1099
be analyzed for information. For local host the object name:
Any number of urls can be streamed in. //localhost/TempServer
Unicast Communication : When you are For a remote host
interested in a particular remote site you will //127.0.0.1/TempServer
direct your net connection to that particular
site using unicast.
1/24/02 B.Ramamurthy 15 1/24/02 B.Ramamurthy 16

Name Binding WeatherInfo class


rebind method binds a server’s object It is very traditional class for keeping
name to the object’s name as it is in the the information about the temperature
registry. at a single location.
Clients use the name in the registry. It has data fields : cityName,
temperature, and description and get
There is also a bind() method. methods for these.
But rebind is better since it binds the An array of objects of this class is used
most recently registered object. in the server implementation.

1/24/02 B.Ramamurthy 17 1/24/02 B.Ramamurthy 18

3
Temperature Client Temperature Client (contd.)
import java.rmi.*; The main method in this client can get
// import other packages the IP address of the remote host as a
constructor calls a private method command line argument.
getRemoteTemp which takes care of Command line argument is an array of
lookup of remote object and access. String of items in the command line
In this application it also displays the after the name of the application.
information.

1/24/02 B.Ramamurthy 19 1/24/02 B.Ramamurthy 20

Client Details Preparing the Application


The name of the server object along with the 1. Compile all the class using javac.
IP of the remote location is used in Naming
class’s lookup method to get an object 2. Generate the stub and the skeleton:
reference. rmic -v1.2 TemperatureServerImpl
This object reference is then used for remote
method calls. 3. Then start the registry (this will be
Observe that there is no difference between running as a daemon)
the local and remote call. rmiregistry &
WeatherItem class used in the Graphical
display of the weather information.
1/24/02 B.Ramamurthy 21 1/24/02 B.Ramamurthy 22

Preparing the Application Summary


4. Run the server which will register with the We discussed the various models of
RMI registry. distributes systems.
Java TemperatureServerImpl & Java RMI was used to illustrate the
5. Run the client. distributed system concepts.
Java TemperatureClient & Temperature examples shown
or illustrates some of the distributed
java TemperatureClient {IPAddress} system model discussed and all the
important RMI features.
java TemperatureClient 192.168.0.150

1/24/02 B.Ramamurthy 23 1/24/02 B.Ramamurthy 24

You might also like