aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird Server & DB shutdown Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird Server & DB shutdown" Watch "URLyBird Server & DB shutdown" New topic
Author

URLyBird Server & DB shutdown

Cathal Mullan
Ranch Hand

Joined: May 20, 2011
Posts: 118
Hey Guys,

wondering about how people who successfully passed the URLyBird project closed their Server & DB?

I am planning on extending the DB interface by another interface & add a shutdown() method. This method will close my DB's RandomAccessFile & it will be a synchronized method.

I am planning on having console input that will allow the user to type "shutdown" if they want to shutdown the server. Stop the DB & remove the server object from my RMI registry.

How does that sound? Similar to what you guys did?

cheers
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

I added similar method to my custom interface (which extends the given interface). I added a shutdown hook which calls this shutdown-method. And for my server I have a small gui with some input fields (for configuration settings) and a start and stop button. But that's not required, just showing a popup to enter configuration settings is fine according to instructions.
How you let the user stop the server is up to you: small gui like me, Ctrl+C, typing "shutdown",... are all ok (because you have a user guide which explains the process).


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Cathal Mullan
Ranch Hand

Joined: May 20, 2011
Posts: 118
Oh, you used a GUI for your server.

Don't know why but I thought I had to write a console application to start my server.

Reusing my GUI components to configure the properties in the properties file would save me time.

Did everyone else use a GUI to start/stop their server?

cheers
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

I reused the configuration settings panel in my server gui

I know Roberto Perillo used a dialog for entering the configuration settings and then started the server without any gui. So you just see the console window with maybe a user message like "Server is running... Press Ctrl+C to stop server."
Oladeji Oluwasayo
Ranch Hand

Joined: Sep 10, 2010
Posts: 101

Using pure console for the server will definitely save time (especially for those of us with extremely limited time and still want to make the 31st July deadline). But mistakenly closing the console hosting our server will automatically close the server without the "Do you really want to exit?" message. Doesn't really sound so nice to me; or what do you think?


OCPJP 6, OCMJD 6
Oladeji Oluwasayo
Ranch Hand

Joined: Sep 10, 2010
Posts: 101

Oh, I just saw this in the question.
All configuration must be done via a GUI

So the approach of configuring in GUI and starting the server in the console will be a good choice. Roberto Perillo
Cathal Mullan
Ranch Hand

Joined: May 20, 2011
Posts: 118
Hey Guys,

yes you are absolutely right, I overlooked that point. I will now start & stop my server via a Swing GUI & re-use my UI code from my client,

cheers for your advice
Dennis Grimbergen
Ranch Hand

Joined: Nov 04, 2009
Posts: 140

Cathal Mullan wrote:
I am planning on extending the DB interface by another interface & add a shutdown() method. This method will close my DB's RandomAccessFile & it will be a synchronized method.

Why do you make that method synchronized? It will either be called by the stand-alone client thread or by the server thread. And it will only be called once (on exit).

Maybe I make a mistake here. I implemented all access via the RandomAccessFile in a non-synchonized way. The caller starts the database and the records are read and the closer will cause the database to write the records back to file and close.


SCJP, SCWCD, SCJD
Cathal Mullan
Ranch Hand

Joined: May 20, 2011
Posts: 118
Hey Dennis,

I'm going to do all my thread synchronisation in my Data class. Any methods modifying the DB file or locks will be synchronised.

The shutdown() method will be synchronised because if a thread is writing to a record & another thread shuts down the DB file at the same time I will get IOExceptions for trying to write to a closed file.

Wondering if any1 who passed the OCJD did it this way also?

cheers
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Dennis Grimbergen wrote:Why do you make that method synchronized?

My guess would be to make your Data class thread-safe

Dennis Grimbergen wrote:Maybe I make a mistake here. I implemented all access via the RandomAccessFile in a non-synchonized way.

And how did you make sure your Data class is able to handle multiple concurrent request by different clients?

I imagine you have following pseudo-code (and both methods will work on a shared instance of RandomAccessFile):

read
// 1. position file pointer at the correct position to read the given record number
// 2. read record (bytes)
// 3. turn bytes into String[]

update
// 1. turn String[] into bytes
// 2. position file pointer at the correct position to update the given record number
// 3. write record (bytes)


Let's imagine the following little story about 2 clients: ClientA wants to update record 1 and ClientB wants to read record 2. ClientA (represented by Thread-15) enters the update-method, executes the 1st and 2nd step. So the file pointer points to the location of record 1 at this moment, ready to write the new content. But unfortunately the thread scheduler makes a harsh decision: Thread-15 has to go back to running state, because Thread-23 (the representative of ClientB) gets a bit of CPU-time. So Thread-23 enters the read-method and executes the 1st step (the file pointer now points to location of record 2). The thread scheduler realizes he was a bit harsh a few moments ago and decides to give the CPU-time back to Thread-15 (and Thread-23 goes back to the running state). Thread-15 starts where it was left before going to running state and executes step 3. So it updates record 2 instead of record 1 (because the file pointer was moved by another thread). The end!
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Back on the shutdown() question, I decided to close the data file after
each transaction, in a finally block. This keeps things very reliable and
simple. As data volumes are low, and mostly GUI driven, this seemed
efficient enough. So shutdown became a non-issue. Just exit(1) the
program. Comments?

Jim ... ...


BEE MBA PMP SCJP-6
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Jim Hoglund wrote:Comments?

No. I only hope you open the data file before each transaction
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: URLyBird Server & DB shutdown