Java RMI: Stanislav Chachkov Lgl-Epfl

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Java RMI

Stanislav Chachkov
LGL-EPFL
What is RMI?
RMI enables the programmer to create distributed Java
applications, in which the methods of remote Java objects can be
invoked from other Java virtual machines, possibly on different
hosts.
A Java program can make a call on a remote object once it obtains
a reference to the remote object, either by looking up the remote
object in the naming service provided by RMI or by receiving the
reference as an argument or a return value. A client can call a
remote object in a server, and that server can also be a client of
other remote objects.
RMI uses object serialization to marshal and unmarshal parameters.
What is RMI?
• Remote Method Invocation

Object network
network Remote
Client method invocation Object
Server

Host tata.epfl.ch Host titi.epfl.ch


NetBank example

Client
Bank
Office BankMgr

Account

Host ecub.ubs.ch Host serv.ubs.ch


Creating a Remote object

interface java.rmi.server.
java.rmi.Remote UnicastRemoteObject

bank.BankMgrImpl
interface
bank.BankMgr
createClient()
createAccount(Client) bank.BankMgrImpl_Stub

Remote Reference
• Encapsulates network communication
• Same interface as Remote object

Object Remote
Client Object Remote
f() 01101 RMI f() Object
Stub Runtime
true 10010 true

Host tata.epfl.ch Host titi.epfl.ch


Naming Service
• Directory that associates names to remote
objects (bind)
Remote
Naming Object
“X” A Remote
Object
“Y”
Remote C
“Z” Object
B
Host titi.epfl.ch
Naming Service
• Client use Naming Service to find a
particular Server object (lookup)

Naming
Object “X” Remote
Client lookup(“Y”) Object
“Y” Server
Remote ref. “Z”
to Server
tata.epfl.ch Host titi.epfl.ch
Creating Remote object
1. Create an interface
2. Create the implementation
3. Compile
4. Create the Stub
Interface
public interface BankMgr extends Remote{

public createAccount(Client c)
throws RemoteException;

public createClient(String name)


throws RemoteException;
}
Implementation class
public class BankMgrImpl
extends UnicastRemoteObject
implements BankMgr{

public BankMgrImpl()
throws RemoteException{super();}

public createClient(String a){ …code… }


public createAccount(Client b){ …code… }
}
Stub
A. In JBuilder use “JNI/RMI” tab in class
properties to generate it automatically

B. By hand: use rmic tool:


rmic bank.BankMgrImpl
Binding (server side)
• Binding means: register the object in local
Naming service:

Naming.rebind(
“LGLBank”,
new BankMgrImpl());
Locating (client side)
• Lookup operation return the reference
(stub) on a remote object:

BankMgr bank =
(BankMgr)Naming.lookup(
“//titi.epfl.ch/LGLBank”);
Parameters and Return Values
• Remote objects – by reference
• Serializable objects - by copy
• Others – cannot be passed (exception)
Deployment
• On the server host:
1. Launch Naming service
rmiregistry
2. Launch Server program
java bank.BankMgr
• On the client host(s):
1. Launch the client program
References
• Java Tutorial on RMI:
http://java.sun.com/j2se/1.4/docs/guide/rmi

Book on RMI:
William Grosso “Java RMI”, O’Reilly

You might also like