In a real-world scenario in which a service such as the compute engine is deployed, a developer would likely create a Java Archive (JAR) file that contains theComputeandTaskinterfaces for server classes to implement and client programs to use. Next, a developer, perhaps the same developer of the interface JAR file, would write an implementation of theComputeinterface and deploy that service on a machine available to clients. Developers of client programs can use theComputeand theTaskinterfaces, contained in the JAR file, and independently develop a task and client program that uses aComputeservice.In this section, you learn how to set up the JAR file, server classes, and client classes. You will see that the client's
Piclass will be downloaded to the server at runtime. Also, theComputeandTaskinterfaces will be downloaded from the server to the registry at runtime.This example separates the interfaces, remote object implementation, and client code into three packages:
compute–ComputeandTaskinterfacesengine–ComputeEngineimplementation classclient–ComputePiclient code andPitask implementationFirst, you need to build the interface JAR file to provide to server and client developers.
First, you need to compile the interface source files in thecomputepackage and then build a JAR file that contains their class files. Assume that userwaldohas written these interfaces and placed the source files in the directoryc:\home\waldo\src\computeon Windows or the directory/home/waldo/src/computeon Solaris OS or Linux. Given these paths, you can use the following commands to compile the interfaces and create the JAR file:
Microsoft Windows: cd c:\home\waldo\src javac compute\Compute.java compute\Task.java jar cvf compute.jar compute\*.class
Solaris OS or Linux: cd /home/waldo/src javac compute/Compute.java compute/Task.java jar cvf compute.jar compute/*.classThe
jarcommand displays the following output due to the-voption:added manifest adding: compute/Compute.class(in = 307) (out= 201)(deflated 34%) adding: compute/Task.class(in = 217) (out= 149)(deflated 31%)Now, you can distribute the
compute.jarfile to developers of server and client applications so that they can make use of the interfaces.After you build either server-side or client-side classes with the
javaccompiler, if any of those classes will need to be dynamically downloaded by other Java virtual machines, you must ensure that their class files are placed in a network-accessible location. In this example, for Solaris OS or Linux this location is/home/user/public_html/classesbecause many web servers allow the accessing of a user'spublic_htmldirectory through an HTTP URL constructed ashttp://host/~user/. If your web server does not support this convention, you could use a different location in the web server's hierarchy, or you could use a file URL instead. The file URLs take the formfile:/home/user/public_html/classes/on Solaris OS or Linux and the formfile:/c:/home/user/public_html/classes/on Windows. You may also select another type of URL, as appropriate.The network accessibility of the class files enables the RMI runtime to download code when needed. Rather than defining its own protocol for code downloading, RMI uses URL protocols supported by the Java platform (for example, HTTP) to download code. Note that using a full, heavyweight web server to serve these class files is unnecessary. For example, a simple HTTP server that provides the functionality needed to make classes available for downloading in RMI through HTTP can be found at http://java.sun.com/javase/technologies/core/basic/rmi/class-server.zip.
Theenginepackage contains only one server-side implementation class,ComputeEngine, the implementation of the remote interfaceCompute.Assume that user
ann, the developer of theComputeEngineclass, has placedComputeEngine.javain the directoryc:\home\ann\src\engineon Windows or the directory/home/ann/src/engineon Solaris OS or Linux. She is deploying the class files for clients to download in a subdirectory of herpublic_htmldirectory,c:\home\ann\public_html\classeson Windows or/home/ann/public_html/classeson Solaris OS or Linux. This location is accessible through some web servers ashttp://host:port/~ann/classes/.The
ComputeEngineclass depends on theComputeandTaskinterfaces, which are contained in thecompute.jarJAR file. Therefore, you need thecompute.jarfile in your class path when you build the server classes. Assume that thecompute.jarfile is located in the directoryc:\home\ann\public_html\classeson Windows or the directory/home/ann/public_html/classeson Solaris OS or Linux. Given these paths, you can use the following commands to build the server classes:
Microsoft Windows: cd c:\home\ann\src javac -cp c:\home\ann\public_html\classes\compute.jar engine\ComputeEngine.java
Solaris OS or Linux: cd /home/ann/src javac -cp /home/ann/public_html/classes/compute.jar engine/ComputeEngine.javaThe stub class for
ComputeEngineimplements theComputeinterface, which refers to theTaskinterface. So, the class definitions for those two interfaces need to be network-accessible for the stub to be received by other Java virtual machines such as the registry's Java virtual machine. The client Java virtual machine will already have these interfaces in its class path, so it does not actually need to download their definitions. Thecompute.jarfile under thepublic_htmldirectory can serve this purpose.Now, the compute engine is ready to deploy. You could do that now, or you could wait until after you have built the client.
Theclientpackage contains two classes,ComputePi, the main client program, andPi, the client's implementation of theTaskinterface.Assume that user
jones, the developer of the client classes, has placedComputePi.javaandPi.javain the directoryc:\home\jones\src\clienton Windows or the directory/home/jones/src/clienton Solaris OS or Linux. He is deploying the class files for the compute engine to download in a subdirectory of hispublic_htmldirectory,c:\home\jones\public_html\classeson Windows or/home/jones/public_html/classeson Solaris OS or Linux. This location is accessible through some web servers ashttp://host:port/~jones/classes/.The client classes depend on the
ComputeandTaskinterfaces, which are contained in thecompute.jarJAR file. Therefore, you need thecompute.jarfile in your class path when you build the client classes. Assume that thecompute.jarfile is located in the directoryc:\home\jones\public_html\classeson Windows or the directory/home/jones/public_html/classeson Solaris OS or Linux. Given these paths, you can use the following commands to build the client classes:
Microsoft Windows: cd c:\home\jones\src javac -cp c:\home\jones\public_html\classes\compute.jar client\ComputePi.java client\Pi.java mkdir c:\home\jones\public_html\classes\client cp client\Pi.class c:\home\jones\public_html\classes\client
Solaris OS or Linux: cd /home/jones/src javac -cp /home/jones/public_html/classes/compute.jar client/ComputePi.java client/Pi.java mkdir /home/jones/public_html/classes/client cp client/Pi.class /home/jones/public_html/classes/clientOnly the
Piclass needs to be placed in the directorypublic_html\classes\clientbecause only thePiclass needs to be available for downloading to the compute engine's Java virtual machine. Now, you can run the server and then the client.