Java Reference
In-Depth Information
These commands use
javac
to generate the class files for
Location.java
and
LocationImpl.
java
; the final line uses
rmic
to generate the skeleton
LocationImpl_Stub.class
.
■
Note
While the appearance of the command line differs if you're using Mac OS X or Linux, the command
syntax remains the same.
Writing the Remote Service Host Application
The remote service needs to do two things. First, it must create an instance of implemen-
tation of the objects it serves. Second, it must register those objects with the RMI naming
service, specifying where clients can gain access to the remote object. Listing 11-4 shows
how to do this.
Listing 11-4.
A Simple RMI Server Application
import java.rmi.Naming;
public class LocationServer {
public LocationServer() {
try {
Location c = new LocationImpl();
Naming.rebind("rmi://localhost:1099/LocationService", c);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
public static void main(String args[]) {
new LocationServer();
}
}
This is pretty simple stuff, and it demonstrates the power of building a remote appli-
cation using RMI. The server creates a new instance of
LocationImpl
and registers it with
the naming daemon—that's it!
To run the service on a stand-alone workstation for testing, you must first launch the
RMI registry, which is the Java application with which the
LocationServer
registers its
object for clients to find. You can launch
rmiregistry
in one shell and
LocationServer
(using
java
to start the Java VM) in the other.