Module 7 Networking and RMI Updated
Module 7 Networking and RMI Updated
SampleServer remoteObject;
int s;
…
s = remoteObject.sum(5);
5
public int findsum(int n) {
int sum=0;
System.out.println(s);
15 for(int
i=1;i<=n;i++)
sum+=i;
return sum;
}
The General RMI Architecture
Remote Machine
• The server must first bind
its name to the registry bind
RMI Server
• The client lookup the
server name in the Registry
remote references.
• The Stub serializing the return call lookup
parameters to skeleton,
the skeleton invoking the
remote method and stub
Local Machine
Architecture
Client Server
Stubs Skeletons
Transport
The Stub and Skeleton
call
skeleton
Stub
RMI Client RMI Server
return
Number.java
• How can an object be designated as a Remote object?
A method is defined as a remote method, by
declaring it in an interface which extends 'Remote'
interface.
Each method of the interface should throw
RemoteException, in addition to any application-
specific exceptions.
The interface 'Remote' and the class
'RemoteException' are defined in java.rmi package.
Number.java
import java.rmi.*;
public interface Number extends Remote {
public int findFactorial(int n) throws RemoteException;
public int findSum(int n) throws RemoteException;
}
NumberImplementation.java
• Next, implement the interface to help create remote
objects.
• The class should not only implement the interface but
also extend UnicastRemoteObject.
A remote method may accept arguments of primitive
data types or objects. To send and receive objects,
serialization and deserialization of objects are
essential. If the class is defined to be extended from
UnicastRemoteObject, serialization of the objects will
be done automatically.
NumberImplementation.java
import java.rmi.*;
import java.io.*;
import java.rmi.server.UnicastRemoteObject;
public class NumberImplementation extends UnicastRemoteObject
implements Number
{
public NumberImplementation() throws RemoteException {
super();
}
public int findFactorial(int n) throws RemoteException {
int fact=1;
for(int i=1;i<=n;i++)
fact*=i;
return fact;
}
PMCA502L: Thilagavathi M, AP(Sr.), SITE
RMI – Working with a Sample Application
Naming Class
• Naming class provides methods for storing and obtaining references
to remote objects in a remote object registry.
• A remote object can be associated with a name using the Naming
class's bind or rebind methods.
• Once a remote object is registered (bound) with the RMI registry on
the local host, callers on a remote (or local) host can lookup the
remote object by name, obtain its reference, and then invoke
remote methods on the object.
• Methods
static void bind(String name, Remote obj)
throws AlreadyBoundException
static void rebind(String name, Remote obj)
static void unbind(String name) throws NotBoundException
static Remote lookup(String name)
NumberServer.java
import java.rmi.*;
import java.io.*;
public class NumberServer {
public static void main(String[] args) {
try{
NumberImplementation ni=new NumberImplementation();
Naming.rebind("rmi://localhost//NumServer",ni);
//NumServer is the name bound to the object - ni
}
catch(Exception e) {
e.printStackTrace();
}
}
}
NumberClient.java
import java.rmi.*; import java.util.Scanner;
public class NumberClient {
public static void main(String[] args) {
try {
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int n=sc.nextInt();
Number n1 =
(Number)Naming.lookup("rmi://localhost//NumServer");
System.out.println("Factorial is: "+n1.findFactorial(n));
System.out.println("Sum of n Numbers is: "+n1.findSum(n));
}
catch(Exception e) { e.printStackTrace(); }
}
}