wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Question about Unreferenced Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Question about Unreferenced" Watch "Question about Unreferenced" New topic
Author

Question about Unreferenced

dennis hu
Ranch Hand

Joined: Aug 27, 2002
Posts: 40
Hi Mark/Michael:
Thanks for your reply!
It is very useful to define a HashSet for every Remote Connection Object(a client has unqiue one).
In my design,every client has a View,client can click the BookFlight button after he put in some necessary informations,then the BookFlight button is setEnabled(false).Client cannot click the button before server deal the request well.
So,the client cannot book two or more flights at a time,and I donot need to use HashSet for every client on server side.
After I readed your answers,I thought my design is different with your designs.
Could you tell me that my design is right or not?
can you give me some suggestions?
Thanks!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Dennis,
Allowing only one booking at a time is going to be fine 95%+ of the time. When the server is really taxed, when there is a "run" on a particular flight, or when a client crashes holding the lock on the flight you want to book will account for the 5% of the time that the user would wish for the ability to book a different flight while waiting on the other one to proceed. For this assignment, I think that allowing only one booking at a time is fine, but you may want to put up a busy cursor on your booking components while the transaction completes.
Now, here is why you still need to keep a set of locked records for each client. You are doing something that no designer should ever do: assuming that all future clients will implement a process the same way. Sure, your client can only lock one record at a time, but what about an automated client that can book multiple flights at once from a web server that may be used in the future. By not providing the ability to lock multiple records (which is a really simple thing to do), you have forced the developers to crack open the server code to allow it. Why not make your server as scalable as possible when it is this simple?
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
dennis hu
Ranch Hand

Joined: Aug 27, 2002
Posts: 40
Hi Micheal:
Thanks for your help,it is very useful to me!
I will change my design on client side to implements the multiple book seat function at a time for one client.
So,I think the multithread is necessary,maybe the thread pool is better.Every time when client clicks the book seat button,a thread is created.
Is right?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Dennis,

... Every time when client clicks the book seat button,a thread is created.
Is right?

That's what I did. It's a little tricky because you must keep a set of pending bookings so that booking will be disabled for those records.
Hope this helps,
Michael Morris
Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
Hi Michael,
multiple book seat function at a time for one client

In my design,the client will be allowed to book a seat at a time.How could I do multiple booking?
Could you explain it in detail?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Ray,
In my design, the DataFacade class implemented Runnable. So whenever a user wanted to book a flight here is how it was accomplished:

DataFacade mapped the boooking requests to the individual threads. DataFacade also exteneded Observable and the calling class implemented Observer. So when a booking completed, update() was called on the observer. Also, my booking panel kept a set of pending bookings so that if a booking took a long time and the user reselected that record then booking would be disabled.
This certainly is not the only way to do this though. You could just create a runnable class specifically for booking.
Hope this helps,
Michael Morris
dennis hu
Ranch Hand

Joined: Aug 27, 2002
Posts: 40
Hi Michael:
It is necessary to implements the multiple book function to a client at a time,is so helpful to improve my design,but there is a question:
I design a bookPanel to book flight,and there are JTextFields to show the suitable flight fields,so it is very clear for client to book flight.After the client click the book button,a message box was occured to tell client is successful or not.
If it is successful,the seat number JTextField will show the remained seat nubmers.
If a client book different flight at a time,it is very difficult to showback the information.Maybe only need to show a message box,but I think it is not friendly,do you think so?
And do you have any suggestions?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Dennis,

If it is successful,the seat number JTextField will show the remained seat nubmers.
If a client book different flight at a time,it is very difficult to showback the information.Maybe only need to show a message box,but I think it is not friendly,do you think so?
And do you have any suggestions?

I just updated the number of seats in the JTable and popped up a dialog to notify the user that the booking had completed sucessfully or failed. My booking panel followed the row that the user selected in the table. So if a row had a pending booking, the booking panel was diabled and busy cursor was set on the table and the booking panel. Any time the user selected a different row that had seats available, my text field simply defaulted to 1. Of course the user could change that to any value up to the total number of seats.
To do it the way you had originally designed for one booking at a time may be too chaotic for multiple bookings. For example, if there is a severe temporary network lag, you have three pending bookings and suddenly the lag is gone, then all three bookings are going to complete bam, bam, bam. But since you presumably pop up a modal dialog to notify the customer, there will be a delay between each notified completion, then that may be acceptable too. If you know what flight has completed, you should be able to re-read that record and get the number of available seats to update your text field.
Hope this helps,
Michael Morris
dennis hu
Ranch Hand

Joined: Aug 27, 2002
Posts: 40
Hi Michael:
Yes,it is difficult to change my originally design to suit the multiple booking,but it's necessary.Thanks for you help!

I just updated the number of seats in the JTable and popped up a dialog to notify the user that the booking had completed sucessfully or failed.

I am looking for a way to refresh the JTable living,maybe you can tell me it clearly.

Also, my booking panel kept a set of pending bookings so that if a booking took a long time and the user reselected that record then booking would be disabled.

It is necessary to deal with the booking atcion that had taken a long time,but how do you deal this situation: clinet create Thread A to book a flight and something forced the client to wait,but when the thread was just modifyed the flight and did not unlock it,the time of the Thread A is over,what should to do?
Thanks!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Dennis,

I am looking for a way to refresh the JTable living,maybe you can tell me it clearly.

As I stated earlier, my DataFacade was an Observable. Whenever a booking completed or failed, it called notifyObservers(new Integer(bookingStatus). bookingStatus was either BOOK_SUCCESS, BOOK_FAILED, BOOK_LOCK_ERROR, BOOK_READ_ERROR, BOOK_WRITE_ERROR, BOOK_UNLOCK_ERROR or INVALID_BOOK_READ_ERROR. The update() method of the Observer then called dataFacade.getBookingInfo() to get the record of the just completed booking thread. It then dispatched a property change that the table model was listening for which updated the affected record and called fireTableDataChanged(). The booking panel listened for the same property and removed the record from the pending bookings set.
Since you are probably not using a property dispatcher like I did, you could have your table model and booking panel implement Observabale and be notified directly from whatever object does the booking.

It is necessary to deal with the booking atcion that had taken a long time,but how do you deal this situation: clinet create Thread A to book a flight and something forced the client to wait,but when the thread was just modifyed the flight and did not unlock it,the time of the Thread A is over,what should to do?

In that case, a RemoteException should be thrown on the call to unlock(). In my implementation, notifyObservers() was called with a value of BOOK_UNLOCK_ERROR. This was considered a warning and not an error since presumably modify() had already been called. I just assumed that the booking was successful and popped up a warning dialog to the user that also stated that the booking had completed.
Hope this helps,
Michael Morris
[ September 13, 2002: Message edited by: Michael Morris ]
dennis hu
Ranch Hand

Joined: Aug 27, 2002
Posts: 40
Hi Michael:
In that case, a RemoteException should be thrown on the call to unlock(). In my implementation, notifyObservers() was called with a value of BOOK_UNLOCK_ERROR. This was considered a warning and not an error since presumably modify() had already been called. I just assumed that the booking was successful and popped up a warning dialog to the user that also stated that the booking had completed.

But my DataFacade class donot know this situation (just modified but not unlock record)on server side at all,do you need the clint to track it to distinguish different exception occured on server side?
Could you tell me some details about it and how to implements it?
Thanks!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Dennis,

But my DataFacade class donot know this situation (just modified but not unlock record)on server side at all, ...

Do you call lock(), read(), modify() and unlock() in your DataFacade's booking method? Here is how I kept up with it:

So in the catch block, according to what status contains, I know where in the process the Exception was thrown.
Hope this helps,
Michael Morris
dennis hu
Ranch Hand

Joined: Aug 27, 2002
Posts: 40
Hi Michael:
Thanks for you reply!
Another question:
I want to define the subthread that want to book flghts only can run a fix times,
if the time is over,the booking thread will be interrupted.
Could you tell me some details?
Thanks!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Dennis,

I want to define the subthread that want to book flghts only can run a fix times,
if the time is over,the booking thread will be interrupted.
Could you tell me some details?

Look at java.util.Timer (not javax.swing.Timer) and java.util.TimerTask. That's the simplest way to set a timeout mechanism. You will probably want to create a Runnable class for booking execution which has a java.util.Timer initialized in the run() method. You can then define an inner class the extends java.util.TimerTask which can interrupt the booking operating upon firing. Be warned that you will now be creating two threads per booking instead of one by doint it thus.
Hope this helps,
Michael Morris
dennis hu
Ranch Hand

Joined: Aug 27, 2002
Posts: 40
Hi Michael:

You can then define an inner class the extends java.util.TimerTask which can interrupt the booking operating upon firing.

Every time a book button was clicked,two threads were created ne is BookingThread extends Thread,the other is MyTimerTask extends TimerTask.
In run() method of BookingThread,I define a Timer and MyTimerTask and scheduled the MyTimerTask should run delay 10 seconds.If the booking is successful in 10 seconds,the bookingThread will cancel the timerTask,otherwise the TimerTask will run and interrupte the BookingThread.
The question is:
when the booking thread had waited for the record lock for a long time,the TimerTask should interrupte the BookingThread,I invoke the BookingThread.interrupte() method in TimerTask,but no affect on BookingThread,it still running.
Michael,could you give me some suggestions?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Dennis,
First off, you may be better off not putting the timer and timer task in the same class as the booking thread. You can create a contorl class that starts the timer and thread. Also, instead of extending thread it is generally better practice to implement Runnable instead. I am assuming that you start the booking thread in the actionPerfomed of a button. You can do something like this in your booking thread:

So when your timer fires, it could call timedOut() one the booking thread and notfiy the client that the booking operation timed out. As stated in the javadocs for Thread, there may be no way to instantly interrupt a thread as you have found out. The problem here is that on a dead connection, the Socket is blocking and will continue to do so until an IOException is thrown. That should not be a problem though if you have a controller class to monitor the booking thread and take care of any clean up.
Hope this helps,
Michael Morris
[ September 20, 2002: Message edited by: Michael Morris ]
dennis hu
Ranch Hand

Joined: Aug 27, 2002
Posts: 40
Hi Michael:
Thanks for reply!
First off, you may be better off not putting the timer and timer task in the same class as the booking thread.

maybe you means: I should create a Timer and a Booking thread in controller class,in the run() method of Booking thread,create a TimerTask object and initialed the timer,after some times,the TimerTask object will run and invoke the timeOut() method in Booking thread.
Why cannot create all Timer and TimerTask in run() method of Booking thread?

So when your timer fires, it could call timedOut() one the booking thread and notfiy the client that the booking operation timed out.

But in this situation:
In my design,if the db locked,client have to wait until the db is unlocked.So maybe the Booking thread would wait for the record's lock for a long time.maybe the Booking thread is just blocking or waiting,donot do any other things,So I think the flag of "isTimeOut" is not useful to this Booking thread.

The problem here is that on a dead connection,the Socket is blocking and will continue to do so until an IOException is thrown.

I am confuse it,can you speak it clearly?
Thanks for your lots help!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Dennis,
Here is basically what I have in mind for a controller:

Here is the skeleton for the BookingThread:

This is certainly incomplete but notice that the BookingThread is only responsible for carrying out the actual booking. The BookingController takes care of the timeout that you want to implement. The booking thread may continue to run after the timeout, but it doesn't matter because the controller can check the state of the booking in progress and notify the client of any problems. The beauty is that you don't have to make dangerous calls like Thread.interrupt.
Hope this helps,
Michael Morris
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Dennis,
One other thing you will probably want to do if you use the skeleton code above is have BookingThread extend Observable and BookingController implement Observer. That way when the BookingThread completes normally in the provided time frame it can call notifyObservers() and BookingController can cancel the timer and notify the client of a successful booking in its update() method.
Hope this helps,
Michael Morris
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about Unreferenced
 
Similar Threads
Client crash problem
FBN Design
I need design advice
where to put bookflight() question
Lock and Unlock methods