Consuming Java at the MegaCups Company


The people who work at the MegaCups Company love their coffee. In fact, they have gone over the edge and are in need of counseling. During the work day, they incessantly elbow each other around the coffee urn as they seek another cup of coffee to satisfy their never-ending craving for caffeine.

The company has a coffee urn set up in the kitchen that holds 40 cups of coffee. One worker, Mark, adds 20 cups of coffee to the urn every 14 seconds. Four other workers, Frank, Sam, Julie, and Susan, come around for a fresh cup of coffee every 2 seconds. Most real people wait more than 2 seconds before seeking a fresh cup of coffee, but these workers are computer simulations. For that reason, their sense of time is compressed.

Doing the math, you can see that the workers sometimes find that the coffee urn is empty. If this happens too often, they complain to the manager, who either promises to do something about it or ignores the complaint. If he ignores the complaint, the workers quit. Because of the hectic pace at the MegaCups Company, the work day is short, lasting only 1 minute.

In the MegaCups program, there is only one persistent object, the coffee urn in the kitchen. Everyone comes to this coffee urn to either add or draw coffee. Each addition or subtraction is done transactionally and the result is committed. Each worker runs in his own thread and uses a separate persistence manager. As a result, each worker acts on his own CoffeeUrn object in memory that represents in his transaction the persistent state of the coffee urn found in the datastore. The properties file that configures the persistence manager factory for this example specifies a datastore transaction. As the transactions clash, you can see exactly how your selected JDO implementation handles the transactional semantics for datastore transactions.

The com.ysoft.jdo.book.coffee package contains three classes: MegaCups, Worker, and CoffeeUrn. The Worker class is contained in the source file for the MegaCups class. Excluding comments, blank lines, and lines with a solitary brace, there are approximately 200 lines of code in the two source files. Of these, approximately 20 percent have something to do with the explicit use of JDO. Much of the code that uses JDO explicitly is concerned with setting the program's and the datastore's initial state. To start a transaction, draw a cup of coffee from the coffee urn, and commit the transaction requires only two lines of code that explicitly use JDO.

The MegaCups program was created after a prolonged discussion that occurred on JDOCentral.com about the semantics of JDO transactions. It illustrates the behavior of datastore transactions. The behavior that you see depends on the transactional semantics of the datastore and the JDO implementation. Most implementations use some form of pessimistic locking in the datastore. For a detailed description of JDO's transactional semantics, see Chapter 4.

Building and Running the MegaCups Program

To build the MegaCups program, go to the bookants directory and type ant megacups. This build is part of the ant learning-programs build.

The program acquires a persistence manager factory that is configured by the property file. For the reference implementation, the properties file is jdori.properties contained in the com/ysoft/jdo/book/coffee directory. The build copies it to factory.properties at the build/com/ysoft/jdo/book/coffee directory.

Listing 8-4 shows some of the expected output from the build when using the reference implementation.

Listing 8-4: Expected Output from Running ant megacups

start example
 megacups:     [javac] Compiling 2 source files to E:\Bookcode\build      [echo] returned from com/ysoft/jdo/book/coffee/build.xml      [copy] Copying 1 file to E:\Bookcode\build\com\ysoft\jdo\book\coffee      [copy] Copying 1 file to E:\Bookcode\enhanced\com\ysoft\jdo\book      [java] done.      [echo] creating runMegaCups.bat BUILD SUCCESSFUL 
end example

Unlike the previous build targets, the build for the MegaCups program does not run the program. Instead, you must change to the bookcode-home directory and execute the runMegaCups batch file. The MegaCups program does not have a user interface. It takes about a minute to run. The expected output from running the program will start off looking something like the output shown in Listing 8-5. This output was obtained from using the reference implementation. Notice the number that follows "kitchen" within the brackets. This number is incremented each time one cup of coffee is drawn from the urn. It numbers the order of changes to the coffee urn in the kitchen.

Listing 8-5: Sample Output from the MegaCups Program

start example
 E:\Bookcode>runMegaCups Using property file: com/ysoft/jdo/book/coffee/factory.properties This program will end in one minute Mark found: CoffeeUrn [Kitchen-0] contains 0 cups Sam found: CoffeeUrn [Kitchen-0] contains 0 cups Julie found: CoffeeUrn [Kitchen-0] contains 0 cups Susan found: CoffeeUrn [Kitchen-0] contains 0 cups Frank found: CoffeeUrn [Kitchen-0] contains 0 cups Mark added coffee to CoffeeUrn [Kitchen-0] contains 20 cups Sam drank a cup of coffee from CoffeeUrn [Kitchen-1] contains 19 cups Julie drank a cup of coffee from CoffeeUrn [Kitchen-2] contains 18 cups Susan drank a cup of coffee from CoffeeUrn [Kitchen-3] contains 17 cups Frank drank a cup of coffee from CoffeeUrn [Kitchen-4] contains 16 cups Sam drank a cup of coffee from CoffeeUrn [Kitchen-5] contains 15 cups Julie drank a cup of coffee from CoffeeUrn [Kitchen-6] contains 14 cups Susan drank a cup of coffee from CoffeeUrn [Kitchen-7] contains 13 cups Frank drank a cup of coffee from CoffeeUrn [Kitchen-8] contains 12 cups 
end example

If you are using the reference implementation, then the datastore files require a one-time initialization. If you see the following error:

 Using property file: com/ysoft/jdo/book/coffee/factory.properties javax.jdo.JDOFatalDataStoreException: com.sun.jdori.fostore.FOStoreLoginException:       Could not login user JDO to database FOStoreTestDB. NestedThrowables: org.netbeans.modules.mdr.persistence.StorageIOException 

then you want to go to the factory.properties file located in the build/com/ysoft/jdo/book/coffee directory and uncomment the second line, so that it reads as follows:

 #set this to true to create valid datastore files com.sun.jdori.option.ConnectionCreate=true 

After running the MegaCups program again, the files FOStoreTestDB.btd and FOStoreTestDB.btx should exist and be larger than zero bytes in size. To prevent the MegaCups program from continually creating new datastore files, recomment the option string in the factory.properties file after valid datastore files have been created.

The MegaCups program accepts a number of optional command line parameters. You can specify the number of workers and their names by using the following parameters:

 -names Tom Dick Harry 

Pick the names you like and add as many as you want. The first person named is given the responsibility to fill the coffee urn. You can also prevent anyone from filling the coffee urn by specifying this option:

 -nofilling 

Multiple invocations of the MegaCups program can run simultaneously. Most commercial datastores support multiuser access, but the datastore for the reference implementation does not support concurrent access from multiple JVMs.




Using and Understanding Java Data Objects
Using and Understanding Java Data Objects
ISBN: 1590590430
EAN: 2147483647
Year: 2005
Pages: 156
Authors: David Ezzio

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net