Two interfaces are of utter importance to distributed programming in Java:
- the Remote interface
- the Serializable interface
The Remote interface specifies that an object implementing this interface
is an object in a different process (but of course it is technically possible to be in the same process),
most likely on a different machine.
For example:
public interface ComputeServer extends Remote {
public double integrate(String function, double min, double max) throws RemoteException;
}
Remote objects must implements this interface and extends the UnicastRemoteObject class:
public class ComputeServerImpl extends UnicastRemoteObject implements ComputeServer {
public double integrate(String function, double min, double max) throws RemoteException {
...
}
}
All methods in a class implementing the Remote interface must throw the RemoteException (This exception
will occurs if the network is down).
Notice the signature of the above method:
public double integrate(String function, double min, double max) throws RemoteException {
Suppose now that you want to group the parameters to the function to integrate in an object:
class FunctionToIntegrate {
String function;
int min;
int max;
}
and pass an object of this class such as:
public double integrate(FunctionToIntegrate function);
to identicate and marshall the object over the wire (e.g, IP) it is necessary to have the object
implement the Serializable interface:
class FunctionToIntegrate extends Serializable {
...
}
So, while you will typically pass a Serializable object in a remote call, is this possible to pass a Remote object?
Yes! This is typically used when the server needs to call back the client such as:
public class ComputeServerImpl extends UnicastRemoteObject implements ComputeServer {
public void integrate(String function, Remote callback) throws RemoteException {
...
}
}
Suppose the computation takes a while. The above method returns right away, but calls back the client
when the result is ready.