Java Reference
In-Depth Information
// rather than going through a factory, we are directly calling the connector
// Uncomment the DVDConnector of the protocol you want to use.
import sampleproject.remote.DvdConnector;
//import sampleproject.sockets.DvdConnector;
/**
* A DBTester is the test equivalent of a client who is trying to book one or
* more DVDs. However we know exactly how the DBTester is going to behave,
* therefore we can predict the results of this testing.
*/
public class DBTester extends Thread {
// various status for what can happen when we try to book over the network
public enum Status {
SUCCESS, OUT_OF_STOCK, TIMEOUT
}
private String dvdUpc; // the DVD we are supposed to rent
private int numberOfRentals; // number of times to rent it
private DBClient db; // connection to the remote database
private int successfulRentals = 0; // number of times we rented the DVD
private int outOfStock = 0; // number of times we failed due to no copies left
private int timeouts = 0; // number of times timed out trying to reserve DVD
// To make the screen output easier to read, we are using a pretend logger
// If we chose to convert to the real JDK logger, we could just change this
private PretendLogger log = new PretendLogger();
public DBTester(String title, int numberOfRentals, String dvdUpc)
throws Exception {
super (title);
this.numberOfRentals = numberOfRentals;
this.dvdUpc = dvdUpc;
db = DvdConnector.getRemote();
}
Most of the work is performed in the run method—the client goes into a loop based on
the number of times they are supposed to rent the DVD. Within that loop, they try to rent the
DVD. If they are successful, they watch it for two seconds, then return it. If they are not suc-
cessful because the store is out of stock, they complain to management for a second. If they
are not successful because trying to obtain the lock took longer than five seconds, they just
take note of the fact. No matter which of those events happened, they will then wait for
another two seconds before trying to obtain another lock.
Search WWH ::




Custom Search