This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes About stop the RMI server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "About stop the RMI server" Watch "About stop the RMI server" New topic
Author

About stop the RMI server

WarrenII Pan
Greenhorn

Joined: Jul 17, 2005
Posts: 14
Hello, everyone!
I got 2 problems again. I am designing the server GUI. There is a �Stop� button to stop RMI server. When a user presses the button, the following things should happen:
First, stop RMI service. Using unbind method is available.

Second, track if some certain thread is currently in a write method. I search the forum using the keyword of �RMI stop� and benefit from one of Andrew�s posters which suggested to think carefully about this situation.
But how to detect the right thread which is writing a record? Am I losing some key points here?

Third, broadcast all connecting clients that the server is not acceptable temporarily. I still can�t figure out how to implement it.

I know my instruction doesn�t tell me �must� to achieve the 2nd and 3rd requirements, but this case will really occur when the examiner tests my program and press the stop button. So I must manage it properly.


Any advice will be appreciated!
Best Regards,
WarrenII Pan
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Most people give the examiner instruction to press Ctrl+C, this is good enough for full marks in that section. Why do you want a server GUI?
WarrenII Pan
Greenhorn

Joined: Jul 17, 2005
Posts: 14
Thank you, Ta Ri Ki Sun. I try to use a server GUI to allow user entering the position of db file and the RMI service port, starting and stopping the RMI Server. Without the GUI, how to fully implement these things then?

Best Regards,
WarrenII Pan
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Originally posted by WarrenII Pan:
Thank you, Ta Ri Ki Sun. I try to use a server GUI to allow user entering the position of db file and the RMI service port, starting and stopping the RMI Server. Without the GUI, how to fully implement these things then?

Best Regards,
WarrenII Pan


Oh I see, well I can only give you an idea of how I did it.

Since I read exactly the same props file whether local or server, and since I use exactly the same settings view for both, I implemented a recovery handler type class that would ask the user whether or not they want to reconfigure settings. It is not launched at startup, and obviously it's not something you can F1 at the console to get, I just pop it up when needed. If the user says yes I show the settings screen I would have displayed in local mode, using those settings I try to open the file or connection again and if it fails I go back to starting point which is asking the user if they'd like to try again. That's all really, it's still just a console app for the server, nothing fancy, no stop server button, although I do close the database cleanly on shutdown I did not unbind. What I did have as well was a boolean indicating that the registry was created, trying to do it again will result in an error so based on this static boolean I'd either create the registry or get the registry.
WarrenII Pan
Greenhorn

Joined: Jul 17, 2005
Posts: 14
Thanks a lot! I just read an essay using"java -Djava.rmi.server.logCalls=true yourRMIServer" to compile the file. After trying it, I found all the remote method invocations are really displayed in the server console. Then I studied the code of "java.rmi.server.RemoteServer.java" which imports the package of "sun.rmi.server.UnicastServerRef" for the use of the logCalls field,and this package is not disclosed. However, I don't know how to get those information in my code. Can the methods setLog and getLog in the package of java.rmi.server.RemoteServer do the work? If so, how to use them?

Thanks in advance!
Regards,
WarrenII Pan
[ August 06, 2005: Message edited by: WarrenII Pan ]
WarrenII Pan
Greenhorn

Joined: Jul 17, 2005
Posts: 14
I read one of Andrew�s posters about this topic carefully these days. With the help of it, I finally found some useful hints and designed a solution to my problems. Thank you again, Andrew!

To detect the current writing operation thread, set a flag in the write method, assign initial value at the beginning of the method. After writing operation, assign another value to the flag. When a user presses the �stop� button in Server GUI, it will check the flag first to ensure that there is no writing thread. I also found if the checking process is in multiple threads itself, which implements the run method explicitly, the server GUI can wait for the last writing thread stopping and exit the JVM automatically as expected, but the whole program will run slowly; if not in multiple threads, when there is a writing thread, press �stop� button will not result in exiting JVM automatically. Is it necessary to put the process in multiple threads?

When the client users press the �Reserve� button, if the server has stopped(System.exit(0)), they will catch a RemoteException. In the catch clause, I give users information about the current server mode such as The server is not available now. But I still can�t find a way to broadcast to all the connecting clients when the server stops immediately. Is this beyond the scope?

Any advice will help!
Best Regards,
WarrenII, Pan
Samuel Pessorrusso
Ranch Hand

Joined: Jul 21, 2005
Posts: 164
I made something a little bit different.
I make a "Stats" class that stores Serevr statictics (only the current number of requests). In me remote class, when a request arrives, I increment the counter and when it is answered I decrement it.
My "ShutdownManager" waits until the counter is not 0 and then closes the database.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi WarrenII,

Some things to think about (there may be a solution in here somewhere )

If you turn your logic around it may become easier. Instead of having a flag to say that a thread is writing, you could have a flag to say that writes are currently not allowed. You would only need the one instance of such a flag for all threads.

What happens if you call lock with a "magic number" (say -1)? Can this be used somehow? Do your instructions allow you to do this / use this? Which thread would try to lock this magic number? What could it synchronize on to set (say) a flag?

But I still can’t find a way to broadcast to all the connecting clients when the server stops immediately. Is this beyond the scope?
Yes, this is beyond scope.

If you wanted to do this, you would have to make your clients act as observers of the server (the Observable pattern). The clients would have to be RemoteObjects themselves. Then they can register themselves with the server to get updates whenever something interesting happens server side.

Since this is beyond scope (and since I like to only give hints, not solutions ) I have not provided code for this. Let me know if you have more questions on it though.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
WarrenII Pan
Greenhorn

Joined: Jul 17, 2005
Posts: 14
Thank you, Andrew and Samuel!

Hi, Andrew. After carefully thinking about your guidelines for solving these questions and testing some codes according to your advices, I found my approach is more complex and has some weakness. When there is a waiting sequence in server side, which has a lot of users who in turns want to write the same record in a db file, the advantages of your way will appear after pressing the �Stop� button. For example, the order should be after client A unlocks record No.5, the server stops. Then client C or client B gets the priority from waiting mode and accesses the flag indicating writes are currently not allowed and receives the current server status immediately, which is superior to my solution. Because in my solution, client C/B will not receive the server stop information unless press �Reserve� button again in client side. Here I got another question what if the server wants to stop when client A is writing? I think the server should at least wait for client A�s writing operation done and stop. So, is my writing flag still needed here?

My instruction describes the lock method as public void lock(int recNo) throws RecordNotFoundException. Does this mean it doesn�t allow me to use the magic number?

Now I know broadcasting the server�s status to all connecting clients is beyond the scope, but your suggestion using Observable pattern really sheds light on carrying out this objective. I will consult you about it latter for I have not finished it yet. Another question, if I don�t provide a stop button in the server GUI, should I be punished for this?

Thanks in advance!
Best Regards,
WarrenII Pan
[ August 06, 2005: Message edited by: WarrenII Pan ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi WarrenII
what if the server wants to stop when client A is writing? I think the server should at least wait for client A’s writing operation done and stop. So, is my writing flag still needed here?
As you have noted, the server thread should be waiting for the client. So the question then goes back to "What could [the server thread] synchronize on to set (say) a flag?" (Actually you may want to set the flag before you synchronize on something ... but that could be a design decision )

My instruction describes the lock method as public void lock(int recNo) throws RecordNotFoundException. Does this mean it doesn’t allow me to use the magic number?
Another design decision for you - do you feel you can justify using "-1" to lock the database knowing that your method throws RecordNotFoundException? (If you can justify it, you can put it into your design choices document)

if I don’t provide a stop button in the server GUI, should I be punished for this?
Hmmm, difficult.

Personally I would not accept it if an employee in real life wrote a server application that (a) required a GUI to start, and (b) required a GUI to stop.

However for the Sun assignment it appears that Sun want a GUI for the server. In which case you should probably provide the "usual" shutdown options (button / Alt-F4 / File->Exit). I think using the Runtime.addShutdownHook method (this thread gives an example) is an easy way to have common code for all those "usual" shutdown options and the Ctrl-C / shutdown-signal option.

Regards, Andrew
Lara McCarver
Ranch Hand

Joined: Dec 09, 2003
Posts: 118
From my requirements section. This is B&S v 2.3.3:

Your programs must not require use of command line property specifications. All configuration must be done via a GUI, and must be persistent between runs of the program.


I would classify the location of the database file as part of the configuration, and I would think that server part of the application should be the one to set the database file, so that would mean that the server has to have a GUI to do that. Feel free to tell me the errors of my ways! Maybe I didn't scrutinize the requirements carefully enough, I did not actually see a requirement that you need to be able to specify the location of the database file, so I suppose you could hard-code it and then have no UI to configure it, but this seems like such a confining choice that I don't think I could justify it in my choices.txt.

So there are 2 choices: you can let your GUI not only set the database but also start and stop the server. Or, I guess you could have a normal console app which popped open a dialog at startup to let you specify the database, then after you pick the database, the dialog closes and the server app continues on as a console app that is stopped with Ctrl-C. If you do use a user interface to start and stop the server, I do not see any requirement to be able to press the Start button again after pressing the Stop button; you could choose to end the application after the server is stopped. The user could get the server running again by restarting the application.

I would agree that this is an unusual (to say the least!) way to write a server application... personally, I have never seen a server app which requires button clicking (e.g. closing the database location dialog). one reason is, that prevents you from writing other applications on top of it which start and then monitor the performance of the server.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi WarrenII,

Just to give you an example, in our book The Sun Certified Java Developer Exam with J2SE 5, Second Edition, Jeremy and I do create a server GUI which conforms to the suggestion I mentioned above. Namely, the user can exit by any one of:
  • Clicking the Exit button
  • Using File->Exit
  • Pressing Alt-F4
  • Clicking the close button (the little 'X' in the top right hand corner of the window)
  • Pressing Ctrl-C
  • In all cases the server ensures that it shuts down cleanly by blocking writes before exiting, and all exit methods run the same code as we use the shutdown hook mentioned in my previous post.

    Our server window looks like:


    We do standard things like disable most of the controls once the user has started the server.

    Our server window is deliberately slightly more complex than you will probably need for your assignment - for example we allow the user to choose whether to run a Socket server or an RMI server (we have both), and we allow the port number to be changed, and the server window contains the same panel that we use in a dialog box for our client.

    Regards, Andrew
    [ August 07, 2005: Message edited by: Andrew Monkhouse ]
    WarrenII Pan
    Greenhorn

    Joined: Jul 17, 2005
    Posts: 14
    Thank you very much, Andrew and Lara!
    Hi Lara,
    I would prefer your 1st choice to the 2nd one. Because I think using server GUI just like the one in Andrew�s book can perfectly match requirements of Sun�s B&S assignment.

    Hi Andrew,
    Your Server GUI is wonderful! It completely implements all the functionality in server side and matches the Sun�s requirements perfectly. With the greatest help of your posters, I decide to use a) a flag which indicates whether writes are currently allowed or not and b) the method of addShutdownHook waiting for the current updating thread finishing and then stopping the server just as you have mentioned in your previous post: Shutting down cleanly is something you really should do, no matter what exit/stop manners the user will take in the sever side, even if not use server GUI. BTW, your example of the usage of addShutdownHook method is more concise than Java�s API. Thank you again!

    Best Regards,
    WarrenII Pan
    Lara McCarver
    Ranch Hand

    Joined: Dec 09, 2003
    Posts: 118
    I just had an idea about stopping all the writes... you could lock each record in the database. At the point all the records were locked, then you would be ready to finish. This wouldn't require any extra flags. Of course, for me there is a small complication... My deadlock detection is called from my lock() method and it works by only allowing you to lock one record at a time, so you wouldn't be able to lock all the records in the database. Not sure whether to add a special case somewhere or to have my deadlock prevention just guard against trying to lock a smaller record number...
    Lara McCarver
    Ranch Hand

    Joined: Dec 09, 2003
    Posts: 118
    My instruction describes the lock method as public void lock(int recNo) throws RecordNotFoundException. Does this mean it doesn�t allow me to use the magic number?


    It says in the exam instructions that you need to write a class named Data which implements the DBMain interface, and one of the methods in the DBMain interface is void lock(int recno). What I am wondering is, could you add a new method to Data? Adding a new method would still allow Data to comply with the DBMain interface. If you are allowed to add a new method, then you can add a method, void prepareForShutdown() which does whatever you want.

    Or, put this another way: Suppose you are writing an adaptor class that sits on top of Data / DBMain. Is it OK for your adaptor class to store a Data reference instead of a DBMain reference, and then use Data methods that are not part of DBMain?
    WarrenII Pan
    Greenhorn

    Joined: Jul 17, 2005
    Posts: 14
    Thank you, Lara! Sorry for coming late.
    I just had an idea about stopping all the writes... you could lock each record in the database. At the point all the records were locked, then you would be ready to finish.

    The stopping manner is not friendly enough,I think. Just like in the real world, the server should at least wait for the current writing thread finishing.
    I think we can add new methods to Data definitely. Although the Data class implements the DBMain interface, the definition of implementation doesnt prohibit us adding new methods which aren't provided by DBMain, right?

    Best Regards,
    WarrenII Pan
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    Hi Lara
    Suppose you are writing an adaptor class that sits on top of Data / DBMain. Is it OK for your adaptor class to store a Data reference instead of a DBMain reference, and then use Data methods that are not part of DBMain?
    Have you considered still using your DBMain reference, but in your shutdown code check whether DBMain is an instance of your adapter class that also defines the special method that locks the database?

    Regards, Andrew
    Lara McCarver
    Ranch Hand

    Joined: Dec 09, 2003
    Posts: 118
    Lara: I just had an idea about stopping all the writes... you could lock each record in the database. At the point all the records were locked, then you would be ready to finish.


    Warren: The stopping manner is not friendly enough,I think. Just like in the real world, the server should at least wait for the current writing thread finishing.


    My method would wait for the current writing thread to finish because every method that writes to the database first locks the record it is writing. The only exception is record creation, where I have a separate creation lock... and I almost forgot about that one! Anyway, by grabbing all these locks, I first have to wait until each lock is available, which will only happen after the write is finished.
    Lara McCarver
    Ranch Hand

    Joined: Dec 09, 2003
    Posts: 118
    Have you considered still using your DBMain reference, but in your shutdown code check whether DBMain is an instance of your adapter class that also defines the special method that locks the database?


    Nice idea! A similar idea would be to define a ShutDown interface, and have my Data object implement both DBMain *and* ShutDown. I guess the main thing I was wondering is whether it is OK to add methods to the Data class, and it sounds like it is.

    But one thing I am thinking about... this LockAll method is not going to be called from any of the threads. It is going to be called by a ServerStarter object. I am not talking about the RMI server object which implements UnicodeRemoteObject, but the Server GUI object, or some object it calls, that doesn't actually have a reference to any of the Data objects, so it would have to be a static method, but I think I could still check for the class and then call Data.LockForShutDown() (or whatever).
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: About stop the RMI server
     
    Similar Threads
    B&S: OK to assume lock(), update(), unlock() called from same thread?
    RMI Server
    Cookie Generation
    about the stop rmi
    2 tier architecture