Java Reference
In-Depth Information
The RMI Implementation
To create a version of our server that acts as a factory, we first define the factory and its only
method,
getClient()
, which allows us to obtain a unique instance of the
DvdDatabase
class
(see Listing 6-4).
■
Note
In this section we are talking about using the Factory pattern with our RMI solution, but it would
work equally well with a sockets solution. However, the socket server already works essentially as a factory,
creating a unique thread for each connected client, so we don't really need to explicitly create a unique
object for each connected client. The very nature of a multithreaded socket server addresses the issue of
thread reuse in an RMI thread pool.
Listing 6-4.
DvdDatabaseFactory.java
interface DvdDatabaseFactory extends Remote {
public DvdDatabaseRemote getClient() throws RemoteException;
}
class DvdDatabaseFactoryImpl extends UnicastRemoteObject
implements DvdDatabaseFactory {
/**
* A version number for this class so that serialization can occur
* without worrying about the underlying class changing between
* serialization and deserialization.
*/
private static final long serialVersionUID = 5165L;
public DvdDatabaseFactoryImpl() throws RemoteException {
//do nothing constructor
}
public DvdDatabaseRemote getClient() throws RemoteException {
return new DvdDatabaseImpl();
}
}
Now we define the
DvdDatabase
remote class, which like any other Remote object, extends
java.rmi.Remote
in the interface and extends
UnicastRemoteObject
in the
Implementation
class:
public interface DvdDatabaseRemote extends Remote, DBClient {
}
public class DvdDatabaseImpl extends UnicastRemoteObject implements
DvdDatabaseRemote {
... refer to code base for the rest of implementation...
}