Professional Documents
Culture Documents
Java RMI
RMI Interface
RMI Architecture
Transparency
Java RMI needs a naming service (like Sun RPC’s port mapper)
Servers register contact address information
Clients can locate servers
This is called RMI Registry
Unlike in RPC, you must start the RMI Registry yourself
It is a program called rmiregistry
By default runs on port 1099 (but you can specify another port
number: rmiregistry < port nb >)
Programs access the registry via the java.rmi.Naming class
An RMI Example
p u b l i c i n t e r f a c e C a l c u l a t o r e x t e n d s j a v a . r m i . Remote {
p u b l i c l o n g add ( l o n g a , l o n g b ) t h r o w s j a v a . r m i . R e m o t e E x c e p t i o n ;
p u b l i c l o n g sub ( l o n g a , l o n g b ) throws j a v a . rmi . RemoteException ;
}
If you are using Java version earlier than 5.0, generate the stub and
skeleton:
rmic C a l c u l a t o r I m p l
i m p o r t j a v a . r m i . Naming ;
public class CalculatorServer {
public CalculatorServer () {
try {
C a l c u l a t o r c = new C a l c u l a t o r I m p l ( ) ;
Naming . r e b i n d ( ” r m i : / / l o c a l h o s t / C a l c u l a t o r S e r v i c e ” , c ) ;
} catch ( Exception e ) {
System . o u t . p r i n t l n ( ” T r o u b l e : ” + e ) ;
}
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
new C a l c u l a t o r S e r v e r ( ) ;
}
}
Before invoking the server, the client must lookup the registr
It must provide the URL for the remote service
It gets back a stub which has exactly the same interface as the server
It can use it as a local object: long x = c.add(4,5)
Compile your client:
javac CalculatorClient . java
java CalculatorClient