Each method in the interface must be declared
to throw a
java.rmi.RemoteException which is the
superclass of many more specific RMI exception classes.
java.rmi.server.UnicastRemoteObjectthat implements your
Remoteinterface. This class represents the remote object (or server object). Other than declaring its remote methods to throw
RemoteExceptionobjects, the remote object does not need to do anything special to allow its methods to be invoked remotely. The
UnicastRemoteObjectand the rest of the RMI infrastructure handle this automatically.
Export the object, making it available for use by clients, by registering the object by name with a registry service.
This is usually
done with the
java.rmi.Naming class and the rmiregistry
A server program may also act as its own registry server by using
LocateRegistry class and
Note: with RMI the client and the server do not communicate directly. On the client side the client's reference to a remote object is implemented as an instance of a 'stub' class. When the client invokes a remote method, it is a method of this stub object that is actually called. The stub does the necessary networking to pass the invocation to a 'skeleton' class on the server. This skeleton translates the networked request into a method invocation on the server object, and passes the returned value back to the stub, which passes it back to the client. This can be a complicated system but fortunately application programmers never have to think about stubs and skeletons; they are generated automatically by the rmic tool.
Namingclass you must run the registry server, if it is not already running.
You can run the registry server by invoking the rmiregistry program.
Note however that, as we mentioned
at step 3 a server program may also act as its own registry server by
LocateRegistry class and registry interface of the
java.rmi.registry package so you need to run the registry
server (or make sure it is running) only if the server uses the default
registry service provided by the
In our two examples we use the default registry service.
The client must first obtain a reference to the remote object exported by the server by using the
Namingclass to look up the object by name.
The name is typically an
The remote remote reference that is returned is an instance of the
Remoteinterface for the object (or more specifically a 'stub' object for the remote object). Once this client has this remote object it can invoke methods on it exactly as it would invoke the methods of a local object.
The only thing that it must be aware of is that all remote methods can throw
RemoteExceptionobjects, and that in the presence of network errors, this can happen at unexpected times.
RMI uses the serializaton mechanism to transfer the stub object from the server to the client. Because the client may load an untrusted stub object, it should have a security manager installed to prevent a malicious (or just buggy) stub from deleting files or otherwise causing harm. The
RMISecurityManagerclass is a suitable security manager that all RMI clients should install.