Communications in Java RMI: Remote Object (Server) Client
Communications in Java RMI: Remote Object (Server) Client
RMI (Remote Method Invocation) is a java-based high-level communication mechanism. RMI enables us to invoke methods on remote machines (or processes) and get the invocation results. In RMI, a Remote interface is defined, and a Remote object (sometimes referred as the server) implements it. Since it is defined as a remote interface!, an" #ava process (sometimes referred as the client) ma" invoke methods on the Remote object. $fter the remote object is created, it should bind itself to a Name Server (rmiregistr"), so the clients can look it up. %he name server acts as a phone guide& each remote object binds itself 'ith a uni(ue name, and the clients can connect to the name server and ask for that remote object. %he name server returns a stub for the remote object) the stub is an implementation of the remote interface 'hich is automaticall" created and takes care of the communication bet'een the client and the remote object.
Remote Object (server) extends UnicastRemoteObject implements the Remote interface implements Remote Interface extends Remote
Client
kno's
bind
Name Server
lookup
$ t"pical life c"cle of a RMI s"stem is as follo's& %he name server (rmiregistr") is started on the server!s machine. %he server starts, creates a remote object and binds it to the name server. %he client starts, connects to the name server and asks for the server!s remote object. In order to do so, the client must kno' the address of the name server (host * port), and the name of the remote object.
RMI re!istr"
%he RMI registr" listens to a given port on the remote machine. %he default port of RMI is ,566. %o launch the RMI registr" t"pe&
rmiregistry &
or, if "ou 'ish to listen to a port other than the default one, simple provide the port number&
rmiregistry 1234 &
7eep in mind that the RMI registr" 'ill continue to 'ork, even 'hen "our session terminates, so be kind and kill (-6) the process before "ou leave. 8ote& the rmiregistr" provided 'ith the free ( academic!) #9M is limited, and can onl" register Remote :bjects that are running on the local machine. %his is the reason 'e start the rmiregistr" and the server on the same machine. %he commercial version of rmiregistr" can handle remote objects from other machines as 'ell.
%his propert" provides a <R= of the remote classes needed for the application to run. In our case, all the classes are stored on a local file s"stem, but on other s"stems 'here the server is located on a dedicated machine codebase should be specified so the client can do'nload it.
Script files
In order to simplif" the usage of the s"stem, 'e have provided script files that allo' convenient invocation of the registr", server and client. %hese scripts are stored in the scri#ts director".
Secon$ &xample
Source files are located on the stringServer+ director". In this e>ample 'e!ll implement an interface of a string container. %he server holds a string container, and can send it to the client. Is it passed b" value or b" reference? In RMI, objects are seriali@ed on the sender side, sent via the net'ork and deseriali@ed on the receiver side, so the client has a cop" of the original container. $n" methods that the client invokes on this container 'ill act on its cop" onl"A the server!s container remains untouched.
'ir$ &xample
Source files are located on the stringServer-remote+ director". %his e>ample is similar to the second e>ample, but this time 'e 'ish that the methods 'e invoke on the container on the client!s side 'ill take effect on the server!s container as 'ell. Bor this purpose, 'e 'ill make the follo'ing modifications& =et the I0ontainer interface e>tend java.rmi.Remote =et the 0ontainer object e>tend java.rmi.<nicastRemote:bject %hat!s itA the server 'ill no' return a stub to the container. Chen the client invokes the set9alue method, the stub 'ill contact the original container and invoke the method on it. 8ote there is no need to bind the container to an" name server ; it is returns b" the server ('hich is bound to the name server).
#$$itional lin(s
http&++java.sun.com+j.se+,.2.5+docs+guide+rmi+inde>.html RMI tutorial http&++java.sun.com+j.se+,.2.5+docs+guide+rmi+codebase.html codebase description