|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
In a real-world scenario in which a service like the compute engine is deployed, a developer would likely create a JAR (Java ARchive) file that contains theComputeandTaskinterfaces for server classes to implement and client program 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, theComputeEngine's remote stub will be downloaded from the server to the client at runtime.The example separates the interfaces, remote object implementation, and client code into three packages:
compute(Computeand
Taskinterfaces)
engine(ComputeEngineimplementation class and its stub)
client(ComputePiclient code and
Pitask implementation)
Let's first 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. Let's suppose a user,waldo, has written these particular interfaces and has placed the source files inc:\home\waldo\src\compute(on UNIX:/home/waldo/src/compute). 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 javac compute\Task.java jar cvf compute.jar compute\*.classUNIX: cd /home/waldo/src javac compute/Compute.java javac 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=281) (out=196) (deflated 30%) adding: compute/Task.class (in=200) (out=164) (deflated 18%)Now you can distribute the
compute.jarfile to developers of server and client applications so that they can make use of the interfaces.When you build either server- or client-side classes with the
javacandrmiccompilers, you generally need to specify where the resulting class files should reside so that they are network accessible. In this example this location is, for UNIX,/home/user/public_html/classes, because some web servers allow accessing a user'spublic_htmldirectory via an HTTP URL constructed ashttp://host/~user/. If your web server does not support this convention, you could use a file URL instead. The file URLs take the formfile:/home/user/public_html/classes/on UNIX or, on the Microsoft Windows platform,file:/c:/home/user/public_html/classes/. You may also select another type of URL, as appropriate.The network accessibility of the class files allows 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 a full, heavyweight web server is not needed to accomplish this downloading of class files. In fact, a simple HTTP server that provides all of the functionality needed to make classes available for downloading in RMI via HTTP can be found at ftp://ftp.javasoft.com/pub/jdk1.1/rmi/class-server.zip.
Theenginepackage contains only one server-side implementation class,Com-puteEngine, the remote object implementation of theComputeinterface. SinceComputeEngineis an implementation of a remote interface, you need to generate a stub for the remote object so that clients can contact the remote object.Let's say that
ann, the developer of theComputeEngineclass, has placedComputeEngine.javain thec:\home\ann\src\enginedirectory and is deploying the class files for clients to use in a subdirectory of herpublic_htmldirectory,c:\home\ann\public_html\classes(on UNIX that would be/home/ann/public_html/classes, accessible via some web servers ashttp://host/~ann/classes/).Now let's assume that the
compute.jarfile is located in the directoryc:\home\ann\public_html\classes. To compile theComputeEngineclass, your class path must include thecompute.jarfile and the source directory itself.
Note: Normally we recommend that you set the class path on the command line, using the-classpathoption. However, for several compounding reasons this example uses theCLASSPATHenvironment variable, because bothjavacandrmicrequire a class path and the-classpathoption is treated differently in JDK 1.1 and JDK 1.2. For detailed information onCLASSPATHrefer to:
Solaris:
http://java.sun.com/products/jdk/1.2/docs/tooldocs/solaris/classpath.html![]()
Microsoft Windows:
http://java.sun.com/products/jdk/1.2/docs/tooldocs/win32/classpath.html![]()
We recommend that you do not set
CLASSPATHin a login or startup file and that you remember to unset it when you're finished working with this example.Here's how to set the
CLASSPATHenvironment variable
Microsoft Windows:
set CLASSPATH=c:\home\ann\src;c:\home\ann\public_html\classes\compute.jarUNIX:
setenv CLASSPATH /home/ann/src:/home/ann/public_html/classes/compute.jarNow you compile the
ComputeEngine.javasource file, generate a stub for theComputeEngineclass, and make that stub network accessible. To create stub (and optionally skeleton files), run thermiccompiler on the fully qualified class names of the remote object implementations that must be found in the class path. Thermiccommand takes one or more class names as input and produces as output class files of the formClassName_Stub.classandClassName_Skel.class. A skeleton file will not be generated if you runrmicwith the-v1.2option. This option should be used only if all of your clients will be running JDK 1.2 or compatible versions.
Microsoft Windows: cd c:\home\ann\src javac engine\ComputeEngine.java rmic -d . engine.ComputeEngine mkdir c:\home\ann\public_html\classes\engine cp engine\ComputeEngine_*.class c:\home\ann\public_html\classes\engineUNIX: cd /home/ann/src javac engine/ComputeEngine.java rmic -d . engine.ComputeEngine mkdir /home/ann/public_html/classes/engine cp engine/ComputeEngine_*.class /home/ann/public_html/classes/engineThe
-doption tells thermiccompiler to place the generated class files,ComputeEngine_Stub.classandComputeEngine_Skel.class, in the directory c:\home\ann\src\engine. You also need to make the stubs and the skeletons network accessible, so you must copy the stub and the skeleton class to the area:public_html\classes.Since the
ComputeEngine's stub implements theComputeinterface, which refers to theTaskinterface, you need to make these two interface class files network accessible along with the stub. So the final step is to unpack thecompute.jarfile in the directoryc:\home\ann\public_html\classesto make theComputeand theTaskinterfaces available for downloading.
Microsoft Windows: cd c:\home\ann\public_html\classes jar xvf compute.jarUNIX: cd /home/ann/public_html/classes jar xvf compute.jarThe
jarcommand displays the following output:Now the compute engine is ready to deploy. You could do that now or wait until after you have built the client. While we are on a building spree, let's build the client-side program next.created: META-INF/ extracted: META-INF/MANIFEST.MF extracted: compute/Compute.class extracted: compute/Task.class
Let's assume that userjoneshas created the client code in the directoryc:\home\jones\src\clientand will deploy thePiclass, so that it can be downloaded to the compute engine, in the network-accessible directoryc:\home\jones\public_html\classes, also available via some web servers ashttp://host/~jones/classes/. The two client-side classes are contained in the filesPi.javaandComputePi.javain theclientsubdirectory.In order to build the client code, you need the
compute.jarfile that contains theComputeand theTaskinterfaces that the client uses. Let's say that thecompute.jarfile is located inc:\home\jones\public_html\classes. The client classes can be built as follows:
Microsoft Windows: set CLASSPATH=c:\home\jones\src;c:\home\jones\public_html\classes\compute.jar cd c:\home\jones\src javac client\ComputePi.java javac -d c:\home\jones\public_html\classes client\Pi.javaUNIX: setenv CLASSPATH /home/jones/src:/home/jones/public_html/classes/compute.jar cd /home/jones/src javac client/ComputePi.java javac -d /home/jones/public_html/classes client/Pi.javaOnly the
Piclass needs to be placed in the directorypublic_html\classes\. (The
clientclientdirectory is created byjavacif it does not exist.) The reason is that only thePiclass needs to be available for downloading to the compute engine's virtual machine. Now you can run the server and then the client.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.