At the core of the compute engine is a protocol that enables tasks to be submitted to the compute engine, the compute engine to run those tasks, and the results of those tasks to be returned to the client. This protocol is expressed in the interfaces that are supported by the compute engine. The remote communication for this protocol is illustrated in the following figure.Each interface contains a single method. The compute engine's remote interface,
Compute, enables tasks to be submitted to the engine. The client interface,Task,defines how the compute engine executes a submitted task.The
interface defines the remotely accessible part, the compute engine itself. Here is the source code for thecompute.ComputeComputeinterface:package compute; import java.rmi.Remote; import java.rmi.RemoteException; public interface Compute extends Remote { <T> T executeTask(Task<T> t) throws RemoteException; }By extending the interface
java.rmi.Remote, theComputeinterface identifies itself as an interface whose methods can be invoked from another Java virtual machine. Any object that implements this interface can be a remote object.As a member of a remote interface, the
executeTaskmethod is a remote method. Therefore, this method must be defined as being capable of throwing ajava.rmi.RemoteException. This exception is thrown by the RMI system from a remote method invocation to indicate that either a communication failure or a protocol error has occurred. ARemoteExceptionis a checked exception, so any code invoking a remote method needs to handle this exception by either catching it or declaring it in itsthrowsclause.The second interface needed for the compute engine is the
Taskinterface, which is the type of the parameter to theexecuteTaskmethod in theComputeinterface. Theinterface defines the interface between the compute engine and the work that it needs to do, providing the way to start the work. Here is the source code for thecompute.TaskTaskinterface:package compute; public interface Task<T> { T execute(); }The
Taskinterface defines a single method,execute, which has no parameters and throws no exceptions. Because the interface does not extendRemote, the method in this interface doesn't need to listjava.rmi.RemoteExceptionin itsthrowsclause.The
Taskinterface has a type parameter,T, which represents the result type of the task's computation. This interface'sexecutemethod returns the result of the computation and thus its return type isT.The
Computeinterface'sexecuteTaskmethod, in turn, returns the result of the execution of theTaskinstance passed to it. Thus, theexecuteTaskmethod has its own type parameter,T, that associates its own return type with the result type of the passedTaskinstance.RMI uses the Java object serialization mechanism to transport objects by value between Java virtual machines. For an object to be considered serializable, its class must implement the
java.io.Serializablemarker interface. Therefore, classes that implement theTaskinterface must also implementSerializable, as must the classes of objects used for task results.Different kinds of tasks can be run by a
Computeobject as long as they are implementations of theTasktype. The classes that implement this interface can contain any data needed for the computation of the task and any other methods needed for the computation.Here is how RMI makes this simple compute engine possible. Because RMI can assume that the
Taskobjects are written in the Java programming language, implementations of theTaskobject that were previously unknown to the compute engine are downloaded by RMI into the compute engine's Java virtual machine as needed. This capability enables clients of the compute engine to define new kinds of tasks to be run on the server machine without needing the code to be explicitly installed on that machine.The compute engine, implemented by the
ComputeEngineclass, implements theComputeinterface, enabling different tasks to be submitted to it by calls to itsexecuteTaskmethod. These tasks are run using the task's implementation of theexecutemethod and the results, are returned to the remote client.