Professional Documents
Culture Documents
Lab 15 - Remote Method Invocation
Lab 15 - Remote Method Invocation
Lab 15 - Remote Method Invocation
Lab-15
Remote Method Invocation
Lab 15: Remote Method Invocation
Table of Contents
1. Introduction 154
8. Evaluation Task (Unseen) [Expected time = 60mins for two tasks] 162
1. Introduction
Working of RMI
In Java, fundamentally objects can be categorized into two types i.e. local objects (objects
accessible within local host only) and remote objects (objects accessible from are remote hosts).
Essential components required to implement the functionality of RMI are given as under:
For remote object invocation, it is required for that object to be defined in Java interface,
which must be accessible to server and client machines
User interface must extend the marker interface known as java.rmi.Remote
Server object interface, which is sub-interface of java.rmi.Remote to define method(s) for
server object
A Server Class, which has implemented remote object interface and an instance of the server
class known as Server Object.
To locate objects, a utility known as RMI registry is required to register remote objects for
the provisioning of naming services.
Server stub: An object which resides on the client host and serves as a surrogate for remote
object
Server Skeleton: an object residing on the server host and is responsible for the
communication with stub on client side and the actual server object
Figure 1 has shown the typical interaction between various components of RMI.
Client Server
Stub Skeleton
Network
The working sequence or flow to implement RMI has been described as under:
The implementation of RMI architecture has been simplified by the automatic generation of stub
and skeleton in JDK version 1.5 and onwards.
4. Concept Map
The prime objective of RMI is to facilitate the communication of objects that are distributed in a
network. This distributed object model is an object-oriented counterpart of remote procedure call
(RPC) or simply it is Java version of RPC. Using RMI API, a programmer is able to write a code
in Java for the interaction of objects residing on different computers.
RMI emancipates the programmer to write difficult and lengthy code with the provisioning of
two helper classes called as Stub and Skeleton. All communications between client and server
takes place through Stub and Skeleton. Client computer holds Stub that ultimately contains
reference information about the server object. The main tasks of the Stub include parameter
sending to server and returning of results (received from server) to the client. On the other hand,
Skeleton is responsible for receiving and passing of parameter to the server object and eventually
to return the results to the Stub.
Concerning parameter passing, three types of parameters i.e. parameters of primitive, local and
remote object type are allowed to pass if a client invokes a method with parameters on remote
machine. However, you have to consider the following for each type of parameter:
Parameters of primitive data types (i.e. int, float, long, char, double, boolean) can be
passed by value like function call on a local computer
Parameters of local object type i.e. instance of java.lang.String can also passed by value.
However, it is necessary for passing objects to belong to class(es) that has/have
implemented serializable interface. It is due to the reason that Stub is responsible to send
serialized object parameter in a stream across the network.
In case of method invocation on remote machine with parameter of remote object type, actually
you have to pass the Stub of the remote object.
You must solve the following problems at home before the lab.
After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you.
Concerning RMI application development, write down an example code to explain the following
four steps i.e.
5.2.1 Task-1
5.2.2 Task-2
6. Procedure& Tools
In this section you will study how to send and receive data between two systems using TCP
Sockets.
6.1 Tools
This task is designed to guide you towards implementing a Simple RMI application. Following 5
steps must be followed properly to setup and run the RMI application. The service that we are
going to build is a simple calculator Service which gets numbers from the client and computes
results and sends the results back to the client.
Write an interface as the following containing all the methods that you want to call remotely. All
these methods will throw a remote exception. The remote exception means that this exception
can be handled remotely as it will be handled by the client who has called the service.
import java.rmi.RemoteException;
public interface Interfacermi extends java.rmi.Remote
{
public int get_add(int a,int b) throws RemoteException;
public int get_sub(int a,int b)throws RemoteException;
public int get_mul(int a,int b) throws RemoteException;
public double get_div(double a,double b) throws RemoteException;
}
The purpose of this interface is to provide the basic features of a calculator can be called
remotely.
This step is just to define the class which will implement the interface defined in the previous
step. This class will also be used to generate the client side and server side agents which are Stub
and Skelton.
import java.io.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
class service implements Interfacermi
{
public service() throws RemoteException
{}
public int get_add(int a,int b) throws RemoteException
{
return a+b;
}
public int get_sub(int a,int b) throws RemoteException
{
return a-b;
}
public int get_mul(int a,int b)
{
return a*b;
}
public double get_div(double a,double b)
{
return a/b;
}
Open separate command prompt console and type the following command.
Start RMIRegistry
The new window will open. This shows that RMI Registry has started. The default port on which
RMI registry runs is 1099. Leave the new opened window as it is and go back to the server side
programming and generate a stub and Skelton by compiling the service class by RMIC tool.
After starting the RMI Registry now we have a service which can hold the registry of objects.
The only object that we can register in RMI Registry can be a live object. Java provides built-in
class that can assure the live reference of Objects. That class is known as UniCastRemoteObject.
Following class has the main function which will publish the object of above class in RMI
registry.
class RmiServer
{
public static void main(String []args)
{
try
{
service r=new service();
Interfacermi stub=(Interfacermi) UnicastRemoteObject.exportObject(r, 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind("Calculator", stub);
System.out.println("Calculator Server is ready");
}
catch(RemoteException e)
{}
catch(Exception e)
{
}
}
}
Before this step we have a server side service running. Now the last step is to write a client side
which tries to find the reference of object from RMI Registry and utilizes this service using the
interface that the server has provided. This client and server communication is based on agents
called Stub and Skelton. Stub is a client side agent and Skelton is a server side agent. Following
client implementation utilizes the above service that we have created. Before running this client
side ensure that the client has both the interface and stub in its classpath.
import java.rmi.*;
import java.io.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
class Client
{
public static void main(String []args) throws Exception
{
String host=null;
Registry registry = LocateRegistry.getRegistry(host);
Interfacermi stub = (Interfacermi) registry.lookup("Calculator");
int z=1;
while(z==1)
{
try
{
System.out.println("press 1 for addition:");
System.out.println("press 2 for subtraction");
System.out.println("press 3 for multiplication");
System.out.println("press 4 for division");
System.out.println("press 5 for exit");
BufferedReader b=new BufferedReader(new
InputStreamReader(System.in));
String ch=b.readLine();
int a=Integer.parseInt(ch);
if(a==1)
{ System.out.println("*****Addition*****");
System.out.println("Enter the first value:");
ch=b.readLine();
int x=Integer.parseInt(ch);
System.out.println("Enter the second value:");
ch=b.readLine();
int y=Integer.parseInt(ch);
int response = stub.get_add(x,y);
System.out.println("Addition of :"+x+" and "+y+" :"+response);
System.out.println("************");
}//end of if
else if(a==2)
{
System.out.println("*****Subtraction*****");
System.out.println("Enter the first value:");
ch=b.readLine();
int x=Integer.parseInt(ch);
System.out.println("Enter the second value:");
ch=b.readLine();
int y=Integer.parseInt(ch);
int response = stub.get_sub(x,y);
System.out.println("Subtraction of :"+x+" and "+y+" :"+response);
System.out.println("************");
}
else if(a==3)
{
System.out.println("*****Multplication*****");
System.out.println("Enter the first value:");
ch=b.readLine();
int x=Integer.parseInt(ch);
System.out.println("Enter the second value:");
ch=b.readLine();
int y=Integer.parseInt(ch);
int response = stub.get_mul(x,y);
System.out.println("Multiplication of :"+x+" and
"+y+" :"+response);
System.out.println("************");
}//end of else if
else if(a==4)
{
System.out.println("*****Division*****");
System.out.println("Enter the first value:");
ch=b.readLine();
double x=Integer.parseInt(ch);
System.out.println("Enter the second value:");
ch=b.readLine();
double y=Integer.parseInt(ch);
double response = stub.get_div(x,y);
System.out.println("Division of :"+x+" and "+y+" :"+response);
System.out.println("************");
}//end of else if 4
else if(a==5)
{
System.out.println("Good Bye -:(");
z=2;
}
else
{
System.out.println("Sorry wrong choice:");
}
}//end of try
catch(Exception e)
{
System.out.println("Exception is :"+e.toString());
}
}//end of while
}//end of main
}
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\dataserver\assignments$\Advanced Computer Programming\Lab14
Develop an application for Air Ticket Reservation System using Java RMI. It is assumed that a
passenger can book a number of seats of a specific type. There are two types of tickets i.e.
Business class tickets (having cost Rs. 80,000) and economical class tickets (of cost Rs. 50,000).
It is required that a remote method returns the total cost to the passenger for the number of
reservation seats.
To implement this system, you are required to:
7.2 Testing
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
10.1 Books
Text Book:
Java: How to Program by Paul J. Deitel, Harvey M. Deitel. Eighth Edition
Java Beginners Guide: http://www.oracle.com/events/global/en/java-outreach/resources/java-a-
beginners-guide-1720064.pdf
http://exampledepot.8waytrips.com/ for the package by package examples
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\