Java Reference
In-Depth Information
We now have a unique instance of the DvdDatabaseImpl class for each connected client. If
each DvdDatabaseImpl has its own instance of DvdDatabase , then the instance of DvdDatabase
can be used to identify the client to the ReservationManager .
Since using a factory will work for both a socket solution and an RMI solution, and since
we are building both socket connectivity and RMI connectivity in this topic, we will present
the ReservationManager code as though a factory solution is being used.
Let's demonstrate the use of a factory and compare it to a nonfactory scenario. Included
in the downloaded code are two classes, RmiFactoryExample.java and RmiNoFactoryExample.
java . As you can probably surmise from the names, one class serves as a factory implementa-
tion test case, while the other serves as a test case for a nonfactory implementation. The
RmiFactoryExample class demonstrates how multiple instances of the DvdDatabase is instanti-
ated, and the RmiNoFactoryExample class demonstrates how multiple requests result in reuse
of our DvdDatabase instance.
Both sample programs extend Thread and implement the run method. The main in each
sample test program also spawns two threads to simulate a multiuser environment. The only
difference between the two test programs involves which remote object gets registered. The
RmiNoFactoryExample registers a DvdDatabase remote object, while the RmiFactoryExample reg-
isters the DvdDatabaseFactory remote object. Correspondingly, the run methods must cast the
appropriate remote interface returned from the lookup method. Refer to Listings 6-5 and 6-6.
Listing 6-5. The Main Method in the RmiNoFactoryExample Class
public static void main(String[] args) throws Exception {
LocateRegistry.createRegistry(1099);
Naming.rebind("RmiNoFactoryExample", new DvdDatabaseImpl());
Thread a = new RmiNoFactoryExample("A");
a.start();
Thread.sleep(1000);
Thread b = new RmiNoFactoryExample("B");
b.start();
a.join();
b.join();
System.exit(0);
}
public void run() {
try {
System.out.println("getting a remote handle to a DvdDatabase."
+ this.hashCode());
DvdDatabaseRemote remote
= (DvdDatabaseRemote)Naming.lookup("RmiNoFactoryExample");
}
catch (Exception e) {
System.err.println(e);
e.printStackTrace();
}
}
Search WWH ::




Custom Search