Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
101 views22 pages

Remote Method Invocation

The document describes Remote Method Invocation (RMI) in Java. RMI allows objects running in one Java Virtual Machine (JVM) to invoke methods on objects running in another JVM, enabling the development of distributed Java applications. The key components of an RMI application are the remote interface, server implementation, client implementation, RMI registry, stub, and skeleton. The document then provides examples of how to define a remote interface, implement a server class, implement a client class, and register the remote object with the RMI registry so it can be looked up and invoked by clients. It also describes a network file locking server application built using RMI that allows locking of shared resources on remote machines.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views22 pages

Remote Method Invocation

The document describes Remote Method Invocation (RMI) in Java. RMI allows objects running in one Java Virtual Machine (JVM) to invoke methods on objects running in another JVM, enabling the development of distributed Java applications. The key components of an RMI application are the remote interface, server implementation, client implementation, RMI registry, stub, and skeleton. The document then provides examples of how to define a remote interface, implement a server class, implement a client class, and register the remote object with the RMI registry so it can be looked up and invoked by clients. It also describes a network file locking server application built using RMI that allows locking of shared resources on remote machines.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Remote Method Invocation

What is RMI ?
 RMI is a mechanism for communicating (only)
between two machines running Java Virtual
Machines.
 It allows an object residing in one system (JVM) to
access/invoke an object running on another JVM.
 RMI is used to build distributed applications; it
provides remote communication between Java
programs.
 It is provided in the package java.rmi.
Architecture of an RMI Application

• In an RMI application, we write two programs,


a server program (resides on the server) and
a client program (resides on the client).
• Inside the server program, a remote object is
created and reference of that object is made
available for the client (using the registry).
• The client program requests the remote
objects on the server and tries to invoke its
methods.
Components of RMI architecture
• Transport Layer − This layer connects the client and the
server. It manages the existing connection and also sets
up new connections.
• Stub − A stub is a representation (proxy) of the remote
object at client. It resides in the client system; it acts as
a gateway for the client program.
• Skeleton − This is the object which resides on the
server side. stub communicates with this skeleton to
pass request to the remote object.
• RRL(Remote Reference Layer) − It is the layer which
manages the references made by the client to the
remote object.
How RMI works ?
To develop an RMI program
• Define the remote interface
• Implement the server
• Implement the client
• Compile the source files
• Start the Java RMI registry, server, and client
start rmiregistry
Define the remote interface
A remote object is an instance of a class that implements
a remote interface.
A remote interface extends the interface java.rmi.Remote and
declares a set of remote methods.
Each remote method throws java.rmi.RemoteException in
addition to any application-specific exceptions
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
Save as : Hello.java - a remote interface
Implement the server
A "server" is a java class that: Create and export a remote object & Register the remote object with a Java RMI registry
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello
{
public Server() {}
public String sayHello()
{
return "Hello, world!";
}
public static void main(String args[])
{
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString()); e.printStackTrace();
}
}
}
Implement the client
The client program obtains a stub for the registry on the server's host, looks up the
remote object's stub by name in the registry, and then invokes method on the
remote object using the stub.
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client
{
private Client() {}
public static void main(String[] args)
{
String host = “address of server”
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString()); e.printStackTrace();
}
}
}
A Network File-Locking Server
The file-locking utility enables to lock resources
on a remote machine.

local file locking for files shared by another


application running on the same computer
global file locking for remote files shared by
applications on multiple computers
Local Shared Locks
FileLock Interface
import java.rmi.*;
public interface FileLock extends Remote
{
public boolean isLocked()
throws RemoteException;
public void lock() throws RemoteException,FileLockingException;
public void unlock() throws RemoteException,FileLockingException;
}
FileLockingException
public class FileLockingException extends Exception
{
public FileLockingException(String msg)
{
super(msg);
}
}
When an exception occurs in the locking machinery,
FileLocking exceptions are used to distinguish them from
normal RMI or run-time exceptions.
NetworkLockServer Interface:
import java.rmi.*;
public interface NetworkLockServer extends Remote
{
// Default registry name for bound to the server
public static String NLS_NAME="server.nls";
public FileLock getSharedLockFor(String absPath) throws
RemoteException;
public boolean isFileLocked(String absPath) throws RemoteException;
public void lock() throws RemoteException;
public void unlock() throws RemoteException;
}
NLS: The Lock Server
The NetworkLockServer interface is implemented in a class called NLS.
import java.rmi.*;
import java.rmi.server.*;
public class NLS extends UnicastRemoteObject
implements NetworkLockServer
{
private boolean locked;
public NLS() throws RemoteException
{
}
public FileLock getSharedLockFor(String path) throws RemoteException
{
return SharedFileLock.getLockFor(path);
}
public boolean isFileLocked(String absPath) throws RemoteException
{
FileLock lock;
lock = getSharedLockFor(absPath);
return lock.isLocked();
}
The lock method ensures that multiple requests to lock this file will not collide. The
first request locks the file, and all others must wait until the file lock is released.
public synchronized void lock()
{
while(locked)
{
try
{
wait();
}
catch(Exception exp)
{
}
}
locked = true;
}

public synchronized void unlock()


{
locked = false;
notifyAll();
}
public static void main(String args[])
{
try
{
System.out.println("Creating server.");
nls = new NLS();
System.out.println("Starting to bind server.");
Naming.rebind(“NetworkLockServer.NLS_NAME”, nls);
}catch (Exception e)
{
System.out.println("Network Lock Server: “+
e.getMessage());
System.exit(0);
}
}
SharedFileLock: The FileLock Implementation
import java.rmi.*;
import java.rmi.server.*;
public class SharedFileLock
extends UnicastRemoteObject implements FileLock
{
//static hash table called locks is used to map paths
onto file locks, so that a unique lock can be used
for each path, rather than having multiple locks
per file.
private static Hashtable locks = new Hashtable();
// get lock based on path
public synchronized static SharedFileLock getLockFor(String path)
{
SharedFileLock retVal = null;
File tmp = new File(path);
String absPath = tmp.getAbsolutePath();
try
{
retVal = (SharedFileLock) locks.get(absPath);
if(retVal == null)
{
retVal = new SharedFileLock(tmp);
locks.put(absPath,retVal);
}
}
catch(Exception exp) {
retVal = null;
}
return retVal;

}
protected SharedFileLock(File f) throws RemoteException
{
String parent,name;
String lckPath;
file = f;
parent = file.getParent();
name = file.getName();
lckPath = parent+File.separator+"."+name+".lck";
lockFile = new File(lckPath);
}
public synchronized boolean isLocked()
{
return lockFile.exists();
}
import java.rmi.*;
Client Program
public class SharedLockTester
{
public static void main(String args[])
{
lookup = "//192.168.0.172/"+NetworkLockServer.NLS_NAME;
System.out.println("Looking up server.");
server = (NetworkLockServer) Naming.lookup(lookup);
System.out.println("Got Server.");
if(server.isFileLocked(args[0]))
System.out.println("The file is locked.");
else
System.out.println("The file is not locked.");
System.out.println("Creating lock object.");
lock = server.getSharedLockFor(args[0]);
System.out.println("Trying to acquire lock.");
lock.lock();
System.out.println("Got lock");
//Hold the lock for 30 seconds.
Thread.sleep(30000);
System.out.println("Unlocking.");

You might also like