The compute engine is a relatively simple program: it runs tasks that are handed to it. The clients for the compute engine are more complex. A client needs to call the compute engine, but it also has to define the task to be performed by the compute engine.Two separate classes make up the client in our example. The first class,
ComputePi, looks up and invokes aComputeobject. The second class,Pi, implements theTaskinterface and defines the work to be done by the compute engine. The job of thePiclass is to compute the value ofto some number of decimal places.
The non-remote
interface is defined as follows:Taskpackage compute; public interface Task<T> { T execute(); }The code that invokes a
Computeobject's methods must obtain a reference to that object, create aTaskobject, and then request that the task be executed. The definition of the task classPiis shown later. APiobject is constructed with a single argument, the desired precision of the result. The result of the task execution is ajava.math.BigDecimalrepresentingcalculated to the specified precision.
Here is the source code for
, the main client class:client.ComputePipackage client; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.math.BigDecimal; import compute.Compute; public class ComputePi { public static void main(String args[]) { if (System.getSecurityManager() == null) { System.setSecurityManager(new SecurityManager()); } try { String name = "Compute"; Registry registry = LocateRegistry.getRegistry(args[0]); Compute comp = (Compute) registry.lookup(name); Pi task = new Pi(Integer.parseInt(args[1])); BigDecimal pi = comp.executeTask(task); System.out.println(pi); } catch (Exception e) { System.err.println("ComputePi exception:"); e.printStackTrace(); } } }Like the
ComputeEngineserver, the client begins by installing a security manager. This step is necessary because the process of receiving the server remote object's stub could require downloading class definitions from the server. For RMI to download classes, a security manager must be in force.After installing a security manager, the client constructs a name to use to look up a
Computeremote object, using the same name used byComputeEngineto bind its remote object. Also, the client uses theLocateRegistry.getRegistryAPI to synthesize a remote reference to the registry on the server's host. The value of the first command-line argument,args[0], is the name of the remote host on which theComputeobject runs. The client then invokes thelookupmethod on the registry to look up the remote object by name in the server host's registry. The particular overload ofLocateRegistry.getRegistryused, which has a singleStringparameter, returns a reference to a registry at the named host and the default registry port, 1099. You must use an overload that has anintparameter if the registry is created on a port other than 1099.Next, the client creates a new
Piobject, passing to thePiconstructor the value of the second command-line argument,args[1], parsed as an integer. This argument indicates the number of decimal places to use in the calculation. Finally, the client invokes theexecuteTaskmethod of theComputeremote object. The object passed into theexecuteTaskinvocation returns an object of typeBigDecimal, which the program stores in the variableresult. Finally, the program prints the result. The following figure depicts the flow of messages among theComputePiclient, thermiregistry, and theComputeEngine.The
Piclass implements theTaskinterface and computes the value ofto a specified number of decimal places. For this example, the actual algorithm is unimportant. What is important is that the algorithm is computationally expensive, meaning that you would want to have it executed on a capable server.
Here is the source code for
, the class that implements theclient.PiTaskinterface:package client; import compute.Task; import java.io.Serializable; import java.math.BigDecimal; public class Pi implements Task<BigDecimal>, Serializable { private static final long serialVersionUID = 227L; /** constants used in pi computation */ private static final BigDecimal FOUR = BigDecimal.valueOf(4); /** rounding mode to use during pi computation */ private static final int roundingMode = BigDecimal.ROUND_HALF_EVEN; /** digits of precision after the decimal point */ private final int digits; /** * Construct a task to calculate pi to the specified * precision. */ public Pi(int digits) { this.digits = digits; } /** * Calculate pi. */ public BigDecimal execute() { return computePi(digits); } /** * Compute the value of pi to the specified number of * digits after the decimal point. The value is * computed using Machin's formula: * * pi/4 = 4*arctan(1/5) - arctan(1/239) * * and a power series expansion of arctan(x) to * sufficient precision. */ public static BigDecimal computePi(int digits) { int scale = digits + 5; BigDecimal arctan1_5 = arctan(5, scale); BigDecimal arctan1_239 = arctan(239, scale); BigDecimal pi = arctan1_5.multiply(FOUR).subtract( arctan1_239).multiply(FOUR); return pi.setScale(digits, BigDecimal.ROUND_HALF_UP); } /** * Compute the value, in radians, of the arctangent of * the inverse of the supplied integer to the specified * number of digits after the decimal point. The value * is computed using the power series expansion for the * arc tangent: * * arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + * (x^9)/9 ... */ public static BigDecimal arctan(int inverseX, int scale) { BigDecimal result, numer, term; BigDecimal invX = BigDecimal.valueOf(inverseX); BigDecimal invX2 = BigDecimal.valueOf(inverseX * inverseX); numer = BigDecimal.ONE.divide(invX, scale, roundingMode); result = numer; int i = 1; do { numer = numer.divide(invX2, scale, roundingMode); int denom = 2 * i + 1; term = numer.divide(BigDecimal.valueOf(denom), scale, roundingMode); if ((i % 2) != 0) { result = result.subtract(term); } else { result = result.add(term); } i++; } while (term.compareTo(BigDecimal.ZERO) != 0); return result; } }Note that all serializable classes, whether they implement the
Serializableinterface directly or indirectly, must declare aprivatestaticfinalfield namedserialVersionUIDto guarantee serialization compatibility between versions. If no previous version of the class has been released, then the value of this field can be anylongvalue, similar to the227Lused byPi, as long as the value is used consistently in future versions. If a previous version of the class has been released without an explicitserialVersionUIDdeclaration, but serialization compatibility with that version is important, then the default implicitly computed value for the previous version must be used for the value of the new version's explicit declaration. Theserialvertool can be run against the previous version to determine the default computed value for it.The most interesting feature of this example is that the
Computeimplementation object never needs thePiclass's definition until aPiobject is passed in as an argument to theexecuteTaskmethod. At that point, the code for the class is loaded by RMI into theComputeobject's Java virtual machine, theexecutemethod is invoked, and the task's code is executed. The result, which in the case of thePitask is aBigDecimalobject, is handed back to the calling client, where it is used to print the result of the computation.The fact that the supplied
Taskobject computes the value ofPiis irrelevant to theComputeEngineobject. You could also implement a task that, for example, generates a random prime number by using a probabilistic algorithm. That task would also be computationally intensive and therefore a good candidate for passing to theComputeEngine, but it would require very different code. This code could also be downloaded when theTaskobject is passed to aComputeobject. In just the way that the algorithm for computingis brought in when needed, the code that generates the random prime number would be brought in when needed. The
Computeobject knows only that each object it receives implements theexecutemethod. TheComputeobject does not know, and does not need to know, what the implementation does.