The concurrent class method makes heavy use of the classes in java.util.concurrent. It starts by generating a runnable
unit (in my example it will be a java.util.concurrent.Callable, but it could be a java.util.Runnable as well) that loads the data from the database.
In the runnable unit of code you will fill each row into a concurrent collection. The best to use is a form of java.util.concurrent.BlockingQueue where you want to wait for the next value(s) to be added rather than viewing snapshots of the database.
This runnable code is kicked off and run in a new
Thread, and a Future object is used to monitor its progress.
Meanwhile, the JSP gets a reference to the BlockingQueue and retrieves values from it as soon as they become available and fills the page. It knows all the items are retrieved when the Future object says it is done.
So to kick it off, I have a DataTransportObject (DTO) which stores values for a single row on my table. This makes transport easy and understandable. In my example we will be getting a list of People from the DB so my DTO is called Person:
I then have the interesting class. This is my DataAccessObject (DAO) which gets the People from a database. This is a really cut down version of a DAO but I wanted to just show you the important parts. This is the 'concurrent' class with the runnable block of code which does the work (this example it is public void call()). To run this class you do 3 steps:
1) Create a new instance of PersonGetter_DB
2) Call the getPeople() method to get a reference to the BlockingQueue of People
3) Call the startRetrievingData() method to begin the work and get a reference to the Future object for monitoring the progress
The code is as follows:
That is a lot of code, sorry... One last bit though, the JSP. The JSP in this case will create a new PersonGetter_DB object using the jsp:useBean tag. It then uses scriptlets to call the other two methods and to start a while loop which checks the Future object (monitor) to see if the DB is done. In the while loop it populates the table.
Here is the code:
Note that this JSP has the buffer="none" attribute for the page directive, and flushes the output after each loop, so it is pretty much both options I suggested earlier. I like this better because most of your work occurs outside the JSP which makes it easier to code, test, and debug (both the JSP and the DB access code).
I would like to fill most of the scriptlets with JSTL and EL if I could, and if I designed the DAO slightly better I could do pretty well, but there is no JSTL while loop that I am aware of, and using the Queues iterator in a forEach loop wouldn't work so well (would block forever at the end). Given a little motivation you could put this all into a custom tag and be done with all the scriptlets, but until then ...
Anyway, this isn't a definitive answer for a DataAccessObject. But it is a pretty good addition to one I think.
You should search the net for Java DataAccessObject, there are good examples in a lot of places (for example look up BalusC's blog, he has a good one)
[ July 24, 2008: Message edited by: Steve Luke ]